Codeforces数据结构(水题)小结
最近在使用codeblock,所以就先刷一些水題上上手
使用codeblock遇到的問題
1.無法進行編譯-------從setting中的編譯器設置中配置編譯器
2.建立cpp后無法調試------只建立源文件無法調試,需要建立一個工程后才能調試
3.設置斷點后,調試不會停止------開啟-g模式且工程要建立在一個沒有中文名的文件夾下
4.調試中如何查看變量------打開debug中的watch,右鍵編輯界面的變量可以選擇添加變量
?
水題來源---codeforces(hzwer神犇刷的水題)
570C.Replacement
給定一個長為n的字符串(包含小寫字母和’.’),有m次操作
每次操作可以修改字符,并詢問修改后有多少對相鄰的’.’
1≤n,m≤105
?
427B.Prison Transfer
給定長為 n 的序列,以及 c,t
問你有多少個連續的長度為 c 的子串,且序列中沒有一個超過 t
?
519B.A and B and Compilation Errors
給定三個序列,長度分別為 n,n-1,n-2,每一行是上一行的序列去掉一個元素
要求輸出去掉的元素
題解:直接排序對比即可
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; const int maxn = 100500; int n; int a[maxn], b[maxn], c[maxn]; int main() {cin>>n;for(int i = 1; i <= n; i++) cin>>a[i];for(int i = 1; i <= n-1; i++) cin>>b[i];for(int i = 1; i <= n-2; i++) cin>>c[i];sort(a+1, a+1+n);sort(b+1, b+n);sort(c+1, c+n-1);for(int i = 1; i <= n; i++) if(a[i] != b[i]) { cout<<a[i]<<endl; break; }for(int i = 1; i <= n-1; i++) if(b[i] != c[i]) { cout<<b[i]<<endl; break; }}?
650A.Watchmen
給定二維平面的n個坐標,求滿足曼哈頓距離等于幾何距離的點對
題解:按照x排序,再按照y排序,統計出來即可
*注意需要去掉重復點自身構成的點對(簡單容斥)
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; typedef long long LL; struct Point {int x, y; }p[200050];bool cmp1(const Point &A, const Point &B) { return A.x > B.x; } bool cmp2(const Point &A, const Point &B) { return (A.y == B.y) ? A.x > B.x : A.y > B.y; } int n; LL ans = 0; int main() {cin>>n;for(int i = 1; i <= n; i++) cin>>p[i].x>>p[i].y;sort(p+1, p+1+n, cmp1);LL t = 1, t2 = 1;for(int i = 2; i <=n; i++){if(p[i].x == p[i-1].x) t++;else{ans += (t*(t-1)/2);t = 1;}}ans += (t*(t-1)/2); t = 1;sort(p+1, p+1+n, cmp2);for(int i = 2; i <= n; i++){if(p[i].x == p[i-1].x && p[i].y == p[i-1].y) t2++;else{ans -= (t2*(t2-1)/2);t2 = 1;}if(p[i].y == p[i-1].y) t++;else{ans += (t*(t-1)/2);t = 1;}}ans += (t*(t-1)/2);ans -= (t2*(t2-1)/2);cout<<ans<<endl; }?
466C.Number of Ways
給定長為n的序列aiai,將其分成3個連續子串,要求每個子串的和相同,求劃分的方案數
題解:統計所有滿足sum[i]*3 = sum[n]的點的數量m;然后倒序枚舉,每枚舉到sum[i]*3/2 = sum[n]的點,就加上m,每枚舉到sum[i]*3 = sum[n]的點,m--,最后輸出即可
*注意當m<=0時,及時跳出,不然會使答案減少
#include <iostream> #include <cstdio> using namespace std; int sum[500050], a[500050]; int main() {int n;long long f = 0, ans = 0;cin>>n;for(int i = 1; i <= n; i++) cin>>a[i];sum[1] = a[1];for(int i = 2; i <= n; i++) sum[i] = a[i] + sum[i-1];for(int i = 1; i < n-1; i++) if(sum[i]*3 == sum[n]) f++;for(int i = n-1; i > 1; i--){if(sum[i]*3 == sum[n]*2) ans += f;if(sum[i]*3 == sum[n]) f--;if(f <= 0) break; //f會出現負值的情況 }cout<<ans<<endl; }?
轉載于:https://www.cnblogs.com/Saurus/p/5998598.html
總結
以上是生活随笔為你收集整理的Codeforces数据结构(水题)小结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 站在智能路由的风口,他选择把传统OA放进
- 下一篇: 5个人的晚餐