Codeforces Round #298 (Div. 2)
发布日期:2021-06-30 15:14:31 浏览次数:2 分类:技术文章

本文共 1797 字,大约阅读时间需要 5 分钟。

虽然是水题,但反而失误了多次。题意简单,一排考生1~n编号考试,相邻考生会作弊故需要重新安排顺序,问最多能有多少人同时考试。n<=3的时候特判,答案唯一确定;n>3的时候所有考生均能考试,先奇数后偶数输出即可。代码如下:

#include
using namespace std;int main(){ int n; cin >> n; int *a = new int[n]; for (int i = 0; i < n; i++) a[i] = i + 1; if (n <= 2) { cout << 1 << endl; cout << 1 << endl; } else if (n == 3) { cout << 2 << '\n' << 1 << " " << 3 << endl; } else { cout << n << endl; for (int i = 0; i < n; i++) { if (i & 1) cout << a[i] << " "; } for (int i = 0; i < n; i++) { if (!(i & 1)) cout << a[i] << " "; } cout << endl; } return 0;}

题意也不难,就是求规定时间内最远可走的距离。第一秒速度v1,最后一秒速度v2,共t秒,因为d很小,对于中间的每t-2秒,从d到-d枚举下一秒速度的变化量,只要保证在剩下的时间内能够减速到v2,这个速度变化量就是合法的。可以换一种思路思考,在每一秒内我始终把增量加到速度较小的那个v上然后使之变成新的速度v,然后路程加上这个v。有点对称性的意思,有点绕。代码如下:

#include
using namespace std;int main(){ int v1, v2, t, d; cin >> v1 >> v2 >> t >> d; int ans = v1 + v2; for (int i = 0; i < t - 2; i++) { if (v1 < v2) { v1 += d; ans += v1; } else { v2 += d; ans += v2; } } cout << ans << endl; return 0;}

这个C题其实还是很简单的,当然需要注意数据类型设置为long long,代码当中给出详细解释:

#include
using namespace std;#define maxn 200010typedef long long ll;ll a[maxn];int main(){ ll n, A,i,sum=0; cin >> n >> A; for (i = 1; i <= n; i++) { cin >> a[i]; sum += a[i]; } for (i = 1; i <= n; i++) { ll ans = 0; ll x = A - (n - 1);//其余元素均取最小值1,x是a[i]可能的最大取值 if (a[i] > x) //若实际上a[i]>x,比如最大可能是5而a[i]=6 ans += a[i] - x; ll y = A - (sum - a[i]);//sum - a[i]表示其余元素均取最大值,y表示a[i]可能的最小的取值 if (y > 0) ans += y - 1; cout << ans << " "; } cout << endl; return 0;}

转载地址:https://jianzhuwang.blog.csdn.net/article/details/45044703 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:常量和约束访问
下一篇:Codeforces Round #181 (Div. 2)

发表评论

最新留言

很好
[***.229.124.182]2024年04月17日 06时12分12秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章