Codeforces Round #298 (Div. 2) A、B、C题

题目链接:Codeforces Round #298 (Div. 2)

A. Exam

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.

Input

A single line contains integer n (1 ≤ n ≤ 5000) — the number of students at an exam.

Output

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.

题意描述:给出n个学生,编号为1~n,要求学生坐在一排并且相邻的学生编号相差不能为1,求出满足这样要求的最大学生数量并输出方案。

算法分析:对于一个大于4的数字,我们可以把它的奇数放在前面,把偶数放在后面,这样很显然不会出现相邻的数字差值为1,然后特判一下1,2,3,4的情况即可。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #define inf 0x7fffffff
 8 using namespace std;
 9 
10 int n;
11 
12 int main()
13 {
14     while (scanf("%d",&n)!=EOF)
15     {
16         if (n==1) {printf("1
1
");continue; }
17         if (n==2) {printf("1
1
");continue; }
18         if (n==3) {printf("2
1 3
");continue; }
19         if (n==4) {printf("4
2 4 1 3
");continue; }
20         printf("%d
",n);
21         printf("1");
22         for (int i=3 ;i<=n ;i+=2) printf(" %d",i);
23         for (int i=2 ;i<=n ;i+=2) printf(" %d",i);
24         printf("
");
25     }
26     return 0;
27 }
View Code

B. Covered Path

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.

Input

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.
Output

Print the maximum possible length of the path segment in meters.

题意描述:给出初速度v1和末速度v2、运动时间t和相邻秒数之间速度可以增长的最大值d,每一秒钟我们看作速度不变,在秒数之间的速度变化可以看作是瞬间的(即第一秒的速度5,在允许增长的前提下,我们可以看作第二秒的速度已经是8),求出行驶的最远长度。

算法分析:这道题很简单,但在做的时候还是有点绕的。我们首先分析v1和v2的情况:

v1 < v2 :然后还得分两种情况:v1在t秒刚好增长到v2;v1在s(s<t)秒增长到v2,然后在s~t之间先增长后降低。前者速度曲线是直线向上,后者速度曲线是先增长到一个峰值,然后下降。

v1 == v2 :这种情况直接和v1<v2中第二种情形s~t秒内一样。

v1 > v2 :和第一种情况相反。我们从v2到v1这样看,就是和第一种情况一样了。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #define inf 0x7fffffff
 8 using namespace std;
 9 const int maxn=100+10;
10 
11 int n;
12 int v1,v2,t,d;
13 
14 int main()
15 {
16     while (scanf("%d%d%d%d",&v1,&v2,&t,&d)!=EOF)
17     {
18         int ans=0;
19         int q=1,h=t;
20         if (v1<v2)
21         {
22             while (q<=h)
23             {
24                 ans += v1;
25                 v1 += d;
26                 q++;
27                 if (q==h) v1=v2;
28                 else if (abs(v1-v2)<=d) break;
29             }
30             if (q>=h) {printf("%d
",ans);continue; }
31             for ( ;q<h ;q++,h--)
32             {
33                 ans += v1;
34                 ans += v2;
35                 v1 += d;
36                 v2 += d;
37             }
38             if (q==h) ans += min(v1,v2);
39             printf("%d
",ans);
40         }
41         else if (v1>v2)
42         {
43             while (q<=h)
44             {
45                 ans += v2;
46                 v2 += d;
47                 h--;
48                 if (h==q) v2=v1;
49                 else if (abs(v1-v2)<=d) break;
50             }
51             if (q>h) {printf("%d
",ans);continue; }
52             for ( ;q<h ;q++,h--)
53             {
54                 ans += v1;
55                 ans += v2;
56                 v1 += d;
57                 v2 += d;
58             }
59             if (q==h) ans += min(v1,v2);
60             printf("%d
",ans);
61         }
62         else
63         {
64             for ( ;q<h ;q++,h--)
65             {
66                 ans += v1;
67                 ans += v2;
68                 v1 += d;
69                 v2 += d;
70             }
71             if (q==h) ans += min(v1,v2);
72             printf("%d
",ans);
73         }
74     }
75     return 0;
76 }
View Code

C. Polycarpus' Dice

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.

Input

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.

Output

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.

题意描述:有n个骰子d1,d2,,,,dn,第i个骰子有di面(值为1~di),通过n个骰子摇出一个值A,然后求出每个骰子不可能出现的值的个数。

算法分析:如果有5个骰子,前4个骰子最小值的和为4,最大值的和为10,现在摇出的值A为13,那么第5个骰子出现的最小值必须得是3,最大值为9。根据每个骰子都这样的想法,然后就可以AC了。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #define inf 0x7fffffff
 8 using namespace std;
 9 typedef long long LL;
10 const LL maxn=200000+10;
11 
12 LL n,A;
13 LL an[maxn];
14 
15 int main()
16 {
17     while (scanf("%I64d%I64d",&n,&A)!=EOF)
18     {
19         LL sum=0;
20         for (LL i=1 ;i<=n ;i++)
21         {
22             scanf("%I64d",&an[i]);
23             sum += an[i];
24         }
25         LL l=n-1;
26         for (LL i=1 ;i<=n ;i++)
27         {
28             l=n-1;
29             LL r=sum-an[i];
30             LL right=A-l;
31             LL left= r>=A ? 1 : A-r;
32             LL num=right-left+1;
33             if (i>1) printf(" ");
34             if (right<=an[i]) printf("%d",an[i]-num);
35             else if (left<=an[i] && an[i]<right) printf("%d",left-1);
36             else printf("%d",an[i]);
37         }
38         printf("
");
39     }
40     return 0;
41 }
View Code
原文地址:https://www.cnblogs.com/huangxf/p/4421295.html