2.24集训
LICHENYU12345 · · 个人记录
小落的大鹅
题意
-
统计单头赢与双头赢组合数
思路:
-
对
8 条线分别判断:若三个相同,标记单只鹅胜利;否则若有且仅有两个相同,则这两个字母可能组队,标记该对为可能获胜。最后统计标记的单鹅数和对数代码:
#include<bits/stdc++.h> using namespace std; char a[5][5]; int ans1,ans2; bool one[30],two[30][30]; void check(char x,char y,char z) { if(x==y && y==z) { one[x-'A']=1; return ; } if(x==y) two[x-'A'][z-'A']=two[z-'A'][x-'A']=1; if(x==z) two[y-'A'][x-'A']=two[x-'A'][y-'A']=1; if(y==z) two[x-'A'][y-'A']=two[y-'A'][x-'A']=1; } int main() { ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); for(int i=1;i<=3;i++) { for(int j=1;j<=3;j++) { cin>>a[i][j]; } } check(a[1][1],a[1][2],a[1][3]); check(a[2][1],a[2][2],a[2][3]); check(a[3][1],a[3][2],a[3][3]); check(a[1][1],a[2][1],a[3][1]); check(a[1][2],a[2][2],a[3][2]); check(a[1][3],a[2][3],a[3][3]); check(a[1][1],a[2][2],a[3][3]); check(a[1][3],a[2][2],a[3][1]); for(int i=0;i<=25;i++) { if(one[i]) ans1++; } for(int i=0;i<=25;i++) { for(int j=0;j<=25;j++) { if(two[i][j]) ans2++; } } cout<<ans1<<'\n'<<ans2/2; return 0; }数矩形
题意:
-
给
n 和坐标,问能够造成几个矩形思路:
-
枚举所有点对,若满足
x1<x2 且y1<y2 则视为矩形左下和右上,检查另外两个角是否存在,统计矩形就行了代码:
#include<bits/stdc++.h> #define int long long using namespace std; const int N=2e5+10; pair<int,int> a[N]; map<pair<int,int>,bool> mp; int n,ans; signed main() { ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); cin>>n; for(int i=1;i<=n;i++) { cin>>a[i].first>>a[i].second; mp[{a[i].first,a[i].second}]=1; } for(int i=1;i<=n;i++) { for(int j=i;j<=n;j++) { int x1=a[i].first,y11=a[i].second,x2=a[j].first,y2=a[j].second; if(x1<x2 && y11<y2) { if(mp.count({x2,y11}) && mp.count({x1,y2})) { ans++; } } } } cout<<ans; return 0; }大鹅要体检
题意:
-
间反转,使最多位置满足
a[i]==b[i],统计各匹配数的区间个数思路:
-
先统计原匹配数,再枚举所有区间,动态更新反转后匹配数,统计各匹配数的区间个数
代码:
#include<bits/stdc++.h> using namespace std; int n; int a[7505],b[7505],ans[7505],cnt,l,r; int main() { ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<=n;i++) cin>>b[i]; for(int i=1;i<=n;i++) { if(a[i]==b[i]) { cnt++; } } for(int i=1;i<=n;i++) { int tmp=cnt; l=i,r=i; while(l>=1 && r<=n) { if(a[l]==b[l] && a[l]!=b[r]) tmp--; if(a[r]==b[r] && a[r]!=b[l]) tmp--; if(a[l]!=b[l] && a[l]==b[r]) tmp++; if(a[r]!=b[r] && a[r]==b[l]) tmp++; ans[tmp]++; l--; r++; } if(i==n) break; l=i,r=i+1; tmp=cnt; while(l>=1 && r<=n) { if(a[l]==b[l] && a[l]!=b[r]) tmp--; if(a[r]==b[r] && a[r]!=b[l]) tmp--; if(a[l]!=b[l] && a[l]==b[r]) tmp++; if(a[r]!=b[r] && a[r]==b[l]) tmp++; ans[tmp]++; l--; r++; } } for(int i=0;i<=n;i++) { cout<<ans[i]<<'\n'; } return 0; }洛基山脉
题意:
-
求每个长度区间的最小对称差
思路:
-
以每个位置向两边扩展,累加对称位置高度差,更改对应长度区间的最小不对称值
代码:
#include<bits/stdc++.h> using namespace std; int n,a[5005]; int t[5005]; int main() { ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); memset(t,0x3f,sizeof t); cin>>n; t[1]=0; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=2;i<n;i++) { int l=i,r=i,sum=0; while(l>=1 && r<=n) { sum+=abs(a[l]-a[r]); t[r-l+1]=min(t[r-l+1],sum); l--; r++; } } for(int i=1;i<n;i++) { int l=i,r=i+1,sum=0; while(l>=1 && r<=n) { sum+=abs(a[l]-a[r]); t[r-l+1]=min(t[r-l+1],sum); l--; r++; } } t[1]=0; for(int i=1;i<=n;i++) { cout<<t[i]<<' '; } return 0; }散步
题意:
-
## 思路(暴力) -
模拟每秒移动,相遇则停止,
t 秒后输出指定人位置就行了代码:
#include<bits/stdc++.h> #define int long long using namespace std; const int N=1e5+5; int a[N],r[N],pos[N]; bool stop[N]; int n,q,t; signed main() { ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); cin>>n>>t>>q; for(int i=1;i<=n;i++) { cin>>a[i]>>r[i]; pos[i]=a[i]; stop[i]=false; } for(int j=0;j<t;j++) { for(int i=1;i<=n;i++) { if(!stop[i]) { if(r[i]==1) pos[i]++; else pos[i]--; } } for(int i=1;i<n;i++) { if(pos[i]==pos[i+1]) { stop[i]=true; stop[i+1]=true; } } } while(q--) { int x; cin>>x; cout<<pos[x]<<'\n'; } return 0; }