codeforce div2 #775
1/A题:
https://codeforces.com/contest/1649/problem/A
这一题其实很简单,就是要求最左边和最右边的最小距离(只能跨一次水,no more than once!!!)
#include <iostream> #include <cstring> using namespace std; const int N = 1110; int q[N]; int t, n; int main() { cin >> t; while(t--) { cin >> n; for(int i = 1; i <= n; i++) cin >> q[i]; int l = 1, r = n; while(q[l] == 1) l++; l--; while(q[r] == 1) r--; r++;; if(l > r) cout << 0 << endl; else cout << r-l << endl; } return 0; }
2/B题:
https://codeforces.com/contest/1649/problem/B
这题:我们要把所有人传球的次数按升序排列,找到传球次数最大的an。我们假设传球次数总和为sum。如果an>=sum-an, 说明了出了an以为的所有人的传球次数加起来都不够an的传球次数,我们可以把除了an以外的所有人都当作同一个人,假设为a1,那么会有这样的结果:
an传球的次数为an,那么a1接球的次数也就为an,再减去a1自己传球给an的次数,就剩下了 an-a1,因为最后一个接球的人不需要再传球,所以如果an大于a1的话,就会有an-a1条路线。
如果an<=a1 那么只需要一个球就行了。 当然如果所有人传球的次数之和为0,那么就是0个球了。
#include <iostream> using namespace std; typedef long long LL; const int N = 100010; int q[N]; int t; int main() { cin >> t; while(t--) { int n; cin >> n; LL sum = 0; int ma = -1; for(int i = 0; i < n; i++) { cin >> q[i]; ma = max(q[i], ma); sum += q[i]; } if(sum == 0) { cout << 0 << endl; continue; } if(ma <= sum - ma) cout << 1 << endl; else { cout << 2*ma - sum << endl; } } return 0; }
3/C题:
https://codeforces.com/contest/1649/problem/C
这题是求同一种数字的所有曼哈顿距离,不过在学会此题解法的同时,我也学会了一种累加一组数字两两之间差值之和的方法(不过要先排序);
根据大佬的思路:我们先把不同颜色的点的x,y左边分别存起来,然后再计算x轴距离之和的和,再计算y轴距离之差的和,然后加起来就是答案了。
这里我用的是map+vector,感觉要经常使用STL,会节省很多时间,和提供很多奇妙的方法。
#include <iostream> #include <map> #include <vector> #include <algorithm> using namespace std; #define pb push_back #define int long long int n, m, ma; signed main(){ map<int,vector<int> >mx, my; cin >> n >> m; for(int i=1;i<=n;i++){ for(int j=1;j <= m;j++){ int x;cin>>x; mx[x].pb(i); my[x].pb(j); ma=max(ma,x); } } int ans = 0; for(int i=1;i<=ma;i++) { int cnt=0,tot=0; vector<int>s=mx[i]; sort(s.begin(), s.end()); for(auto t:s){ ans+=cnt*t+tot; tot-=t,cnt++; } } for(int i=1;i<=ma;i++){ int cnt=0,tot=0; vector<int>s=my[i]; sort(s.begin(), s.end()); for(auto t:s){ ans+=cnt*t+tot; tot-=t,cnt++; } } printf("%lld\n",ans); return 0;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)