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.
题意,给出一个n,有1-n这n个数字。让你给出一个序列,这个序列满足|ai - ai + 1| ≠ 1 for all i from 1 tok - 1.
解析:对于小于4的的数据特判,大于等于4的从大到小输出奇数,然后输出偶数。这样就可以满足条件了。
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <map>
- #include <queue>
- #include <stack>
- #include <algorithm>
- #include <cstdlib>
- using namespace std;
- int main()
- {
- int n;
- cin>>n;
- if(n == 1)
- printf("1\n1\n");
- if(n == 2)
- printf("1\n1\n");
- if(n==3)
- printf("2\n3 1\n");
- if(n > 3)
- {
- printf("%d\n",n);
- int i = n % 2 ? n: n -1;
- for(; i >0; i -= 2)
- printf("%d ",i);
- i = n % 2 ? n-1:n;
- for(; i > 2;i -= 2)
- printf("%d ",i);
- printf("2\n");
- }
- return 0;
- }
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.
如果v1 和v2开始的差值大于d。这个没有考虑。
方法:设两个指针,l,r,分别从两头开始扫。
#include <iostream> #include <cstdio> #include <cstring> #include <map> #include <queue> #include <stack> #include <algorithm> #include <cstdlib> using namespace std; int main() { int v1,v2,t,a; cin>>v1>>v2>>t>>a; if(v1 > v2) { int temp = v1; v1 = v2; v2 =temp; } int data[110]={}; int l = 0; int r = t-1; data[0] = v1;data[t-1] = v2; while(r - l > 1) { if(data[l] < data[r]) { data[l+1] = data[l] + a; l++; } else { data[r-1] = data[r] + a; r--; } } int sum = 0; for(int i = 0; i < t; i++) sum += data[i]; cout<<sum<<endl; return 0; }
回顾一下,C感觉可以写出来的,被智商碾压,就是没搞出来。
补题C
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