An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure.
Your task is to choose the maximum number of students and make such an arrangement of students in the room that no two students with adjacent numbers sit side by side.
A single line contains integer n (1 ≤ n ≤ 5000) — the number of students at an exam.
In the first line print integer k — the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other.
In the second line print k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n), where ai is the number of the student on the i-th position. The students on adjacent positions mustn't have adjacent numbers. Formally, the following should be true: |ai - ai + 1| ≠ 1 for all i from 1 tok - 1.
If there are several possible answers, output any of them.
6
6
1 5 3 6 2 4
3
2
1 3
题意:一排学生,依次编号为1到n,
现在要考试了,老师知道每个学生i都会和相邻的学生(i-1)和(i+1)作弊,
所以老师要重新排列,拆散他们,使得任何一个i都不能和(i-1),(i+1)相邻。
当然啦,这样会有一些学生可能无法同时考试。
输出k:最多能同时有多少个学生去考试
k个数:分别为这k个学生原来的编号。
思路:1到4特判,n>=5时一定可以所有人同时考试(两两搭配)。
wa了2次,因为4时特判错了。
4时4个人可以同时考试,我逗比了,以为只能有3个。
1 #include<iostream> 2 using namespace std; 3 const int maxn=5004; 4 int a[maxn]; 5 int main() 6 { 7 int n; 8 while(cin>>n) 9 { 10 if(n==1) 11 { 12 cout<<1<<endl; 13 cout<<1<<endl; 14 } 15 else if(n==2) 16 { 17 cout<<1<<endl; 18 cout<<1<<endl; 19 } 20 else if(n==3) 21 { 22 cout<<2<<endl; 23 cout<<"1 3"<<endl; 24 } 25 else if(n==4) 26 { 27 cout<<4<<endl; 28 cout<<"3 1 4 2"<<endl; 29 } 30 else{ 31 cout<<n<<endl; 32 for(int i=1;i<=n;i++) 33 a[i]=i; 34 int k=(n+1)/2; 35 int first=0; 36 for(int i=1;i<=k;i++) 37 { 38 if(first) 39 cout<<" "; 40 first++; 41 cout<<a[i]; 42 if(i+k<=n){ 43 cout<<" "<<a[i+k]; 44 } 45 } 46 cout<<endl; 47 } 48 } 49 return 0; 50 }
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
The first line contains two integers v1 and v2 (1 ≤ v1, v2 ≤ 100) — the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 ≤ t ≤ 100) — the time when the car moves along the segment in seconds, d (0 ≤ d ≤ 10) — the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that:
- the speed in the first second equals v1,
- the speed in the last second equals v2,
- the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
Print the maximum possible length of the path segment in meters.
5 6
4 2
26
10 10
10 0
100
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters.
题意:一辆车,第1秒的速度为v1,第t秒的速度为v2,从第一秒到第t秒,每秒变化的速度要求
为v-d和v+d之间。
问,这t秒内最大的位移。
思路:从第1秒开始,尽量增加速度,再中间再减少速度,使得第t秒时刚好为v2。
(位移就是v-t坐标曲线围成的面积)
1.由于对称性,若v1>v2,则swap
2.我刚开始两边同时增加d和减少d,在中间对接,但是这样是错的,若v1和v2差距太大的话
3.先让v1增加,使得v1尽量接近于v2,但同时保证v1<=v2
然后同时改变速度,在中间对接,注意中间的处理情况。
1 #include<iostream> 2 using namespace std; 3 const int maxn=105; 4 int a[maxn]; 5 int main() 6 { 7 int v1,v2; 8 while(cin>>v1>>v2) 9 { 10 int t,d; 11 cin>>t>>d; 12 if(v1>v2) 13 { 14 int mmm=v1; 15 v1=v2; 16 v2=mmm; 17 } 18 a[1]=v1; 19 a[t]=v2; 20 int i=1; 21 while(a[i]+d<v2) 22 { 23 a[i+1]=a[i]+d; 24 i++; 25 } 26 int j=t; 27 while(j-i>1) 28 { 29 a[i+1]=a[i]+d; 30 a[j-1]=a[j]+d; 31 i++; 32 j--; 33 } 34 if(i==j){ 35 i--; 36 } 37 if(a[i]+d<a[j]) 38 a[j]=a[i]+d; 39 int sum=0; 40 for(int i=1;i<=t;i++) 41 sum+=a[i]; 42 cout<<sum<<endl; 43 } 44 return 0; 45 }
Polycarp has n dice d1, d2, ..., dn. The i-th dice shows numbers from 1 to di. Polycarp rolled all the dice and the sum of numbers they showed is A. Agrippina didn't see which dice showed what number, she knows only the sum A and the values d1, d2, ..., dn. However, she finds it enough to make a series of statements of the following type: dice i couldn't show number r. For example, if Polycarp had two six-faced dice and the total sum is A = 11, then Agrippina can state that each of the two dice couldn't show a value less than five (otherwise, the remaining dice must have a value of at least seven, which is impossible).
For each dice find the number of values for which it can be guaranteed that the dice couldn't show these values if the sum of the shown values is A.
The first line contains two integers n, A (1 ≤ n ≤ 2·105, n ≤ A ≤ s) — the number of dice and the sum of shown values where s = d1 + d2 + ... + dn.
The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 106), where di is the maximum value that the i-th dice can show.
Print n integers b1, b2, ..., bn, where bi is the number of values for which it is guaranteed that the i-th dice couldn't show them.
2 8
4 4
3 3
1 3
5
4
2 3
2 3
0 1
In the first sample from the statement A equal to 8 could be obtained in the only case when both the first and the second dice show 4. Correspondingly, both dice couldn't show values 1, 2 or 3.
In the second sample from the statement A equal to 3 could be obtained when the single dice shows 3. Correspondingly, it couldn't show 1, 2, 4 or 5.
In the third sample from the statement A equal to 3 could be obtained when one dice shows 1 and the other dice shows 2. That's why the first dice doesn't have any values it couldn't show and the second dice couldn't show 3.
题意:现在有n个骰子,用这n个骰子摇出的和为A
一次给出n个骰子的范围,d[i],,表示i-th个骰子的范围为1~d[i]
依次输出这n个骰子的各自的范围 ( 1~d[i] ) 中有多少个数是不可能出现的。
思路:当其他骰子同时为最小和最大时这个骰子向上的数就为最大数和最小数,小于最小的,和大于最大的,肯定就不可能出现的。
1 #include<cstdio> 2 #include<iostream> 3 using namespace std; 4 const int maxn=200000+10; 5 long long A; 6 int d[maxn]; 7 int main() 8 { 9 int n; 10 while(scanf("%d",&n)!=EOF) 11 { 12 cin>>A; 13 for(int i=1;i<=n;i++) 14 scanf("%d",&d[i]); 15 if(n==1) 16 { 17 long long sum=d[1]-1; 18 cout<<sum; 19 }else 20 { 21 long long max=0; 22 for(int i=1;i<=n;i++) 23 max+=d[i]; 24 int first=0; 25 for(int i=1;i<=n;i++) 26 { 27 long long sum=0; 28 long long x=A+1-n; 29 if(d[i]>x) 30 sum+=(d[i]-x); 31 long long y=A-(max-d[i]); 32 if(y>0) 33 sum+=(y-1); 34 if(first) 35 cout<<" "; 36 first++; 37 cout<<sum; 38 } 39 } 40 cout<<endl; 41 } 42 return 0; 43 }