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\n1\n");continue; }
17         if (n==2) {printf("1\n1\n");continue; }
18         if (n==3) {printf("2\n1 3\n");continue; }
19         if (n==4) {printf("4\n2 4 1 3\n");continue; }
20         printf("%d\n",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("\n");
25     }
26     return 0;
27 }

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\n",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\n",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\n",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\n",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\n",ans);
73         }
74     }
75     return 0;
76 }

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("\n");
39     }
40     return 0;
41 }

时间: 2024-10-05 06:02:03

Codeforces Round #298 (Div. 2) A、B、C题的相关文章

Codeforces Round #298 (Div. 2)

A - Exam 构造 1 #include <cstdio> 2 3 int ans[5000 + 5]; 4 5 int main() { 6 int n, cnt = 1; 7 scanf("%d", &n); 8 for (int i = 1; i <= n; i++) { 9 if (i&1) ans[i] = cnt; 10 else { 11 ans[i] = n+1-cnt; 12 cnt++; 13 } 14 } 15 ans[0]

Codeforces Round #298 (Div. 2), problem: (A) Exam

A. Exam time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output 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 suspe

Codeforces Round #298 (Div. 2) B. Covered Path

题目大意: 一辆车,每秒内的速度恒定...第I秒到第I+1秒的速度变化不超过D.初始速度为V1,末速度为V2,经过时间t,问最远能走多远. 分析 开始的时候想麻烦了.讨论了各种情况.后来发现每个时刻的最大值都满足一定的约束关系.设此时为时刻i,上一次的速度为p,那么本次的速度应为max(p+d,v2+(t-i)*d),因为要保证最终一定能够返回到v2.这样以来便可以得到每个时刻的最大值,然后累加求和即可. 1 #include<cstdio> 2 #include<iostream>

Codeforces Round #298 (Div. 2) A. Exam(水)

题意:N 个学生 编了号,  然后第i个学生不能和第i+1和第i-1坐在一起,问合法的情况下最多坐多少个人,如何做 题解: 水题YY了一下,打表处理前三种    后面的情况就是在第三种情况下往前后插数字   奇数在后  偶数在前,然后没了 代码: #include<stdio.h> #include<string.h> int main() { int n, mymap[5005]; int mark[3][4] = {{1, 1}, {1, 1}, {2, 1, 3}}; whi

Codeforces Round #426 (Div. 2)A B C题+赛后小结

最近比赛有点多,可是好像每场比赛都是被虐,单纯磨砺心态的作用.最近讲的内容也有点多,即便是点到为止很浅显的版块,刷了专题之后的状态还是~"咦,能做,可是并没有把握能A啊".每场网络赛,我似乎都没用上新学的东西,能用上新学东西的题我A不了...5555555555555555 这场CF,讲真,打的心态爆炸,首先A题无限WA,赛后看下WA的那组数据是输入有一个999999999的样例,死骗子,说好的数据是1e9呢,哪能有数据是1e10-1,于是用long long,一下子Accept接收不

Codeforces Round #320 (Div. 2) &quot;Or&quot; Game(好题,贪心/位运算/前缀后缀或)

1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 using namespace std; 6 typedef long long ll; 7 /* 8 n个数,你最多有k次操作,每次操作可以选择一个数乘以x,问所有数或(|)的最大值 9 贪心思路:选一个数进行k此乘以x操作; 因为x>=2 10 111 ---> 1111 11

Codeforces Round #256 (Div. 2/A)/Codeforces448A_Rewards(水题)解题报告

对于这道水题本人觉得应该应用贪心算法来解这道题: 下面就贴出本人的代码吧: 1 #include<cstdio> 2 #include<iostream> 3 using namespace std; 4 5 int a[3],b[3]; 6 7 int main(void) 8 { 9 int n; 10 int need = 0; 11 int sum1 = 0,sum2 = 0; 12 for(int i=1;i<=3;++i){ 13 scanf("%d&q

Codeforces Round #344 (Div. 2) C. Report 水题

#include<bits/stdc++.h> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) #define rep(i,a,b) for(int i=a;i>=b;i--) #define RI(x) scanf("%d",&x) #define RII(x,y) scanf("%d%d",&x,&y) #d

Codeforces Round #429 (Div. 2) 841B Godsend(签到题)

B. Godsend Leha somehow found an array consisting of n integers. Looking at it, he came up with a task. Two players play the game on the array. Players move one by one. The first player can choose for his move a subsegment of non-zero length with an