#include<bits/stdc++.h>usingnamespace std;#definefifirst#definesesecond#defineIOSstd::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);#defineintlonglongconstint INF =9e18;constint N =2e6+10;constint mod =1e9+7;typedef pair<int,int>PII;int n;
set<PII> x , y , z;signedmain(){
cin >> n;for(int i =1; i <= n ; i ++){int a , b , c;
cin >> a >> b >> c;
x.insert({b , c});
y.insert({a , c});
z.insert({a , b});}int res =max({x.size(), y.size(), z.size()});if(x.size()== res){
cout <<"X";}elseif(y.size()== res){
cout <<"Y";}else{
cout <<"Z";}return0;}//freopen("文件名.in","r",stdin);//freopen("文件名.out","w",stdout);
#include<bits/stdc++.h>usingnamespace std;#definefifirst#definesesecond#defineIOSstd::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);#defineintlonglongconstint INF =9e18;constint N =1e5+10;constint mod =1e9+7;typedef pair<int,int>PII;int n , q;int cnt[N], h[N], pos[N];intnex_num(int x){return(x +1)% n;}intcal(int x){return x * x;}int need[N];signedmain(){
cin >> n >> q;for(int i =0; i < n ; i ++) cin >> h[i];for(int i =0; i < n ; i ++) cin >> pos[i], pos[i]-=1;
vector<int>pre(n +2),nex(n +2),pree(n +2),nexx(n +2);/*n ... 1 - n - 1 ... n + 1*/
pre[0]= n;//[0 - n - 1] -1 n
nex[n]=0;
nex[n -1]= n +1;
pre[n +1]= n -1;for(int i =1; i <= n -1; i ++){
pre[i]= i -1;
nex[i -1]= i;}for(int i =2; i <= q +1; i ++) cin >> cnt[i];int prex =0, lans =0;for(int i =1; i <= q +1; i ++){int now =(prex + lans + cnt[i])% n;
prex = now;for(int j = now , cnt =1; cnt <= n ; j =nex_num(j), cnt +=1){
need[cnt]= pos[j];}
pree = pre;
nexx = nex;int res =0, ans =0;for(int j =1; j < n ; j ++) res +=cal(h[j]- h[j -1]);for(int j = n ; j >=1; j --){
ans += res;int now = need[j];if(pree[now]!= n) res -=cal(h[now]- h[pree[now]]);if(nexx[now]!= n +1) res -=cal(h[now]- h[nexx[now]]);if(pree[now]!= n && nexx[now]!= n +1) res +=cal(h[pree[now]]- h[nexx[now]]);
nexx[pree[now]]= nexx[now];
pree[nexx[now]]= pree[now];}
cout << ans <<"\n";
lans = ans;}return0;}//freopen("文件名.in","r",stdin);//freopen("文件名.out","w",stdout);
J. Let’s Play Jigsaw Puzzles!(二维链表)
根据题意 , 维护一个二维链表即可。
#include<bits/stdc++.h>usingnamespace std;#definefifirst#definesesecond#defineIOSstd::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);#defineintlonglongconstint INF =9e18;constint N =2e6+10;constint mod =1e9+7;typedef pair<int,int>PII;int m , l[N], r[N], u[N], d[N];int a , b , c , dd;//上 下 左 右signedmain(){
IOS
cin >> m;for(int i =1; i <= m * m ; i ++){
cin >> a >> b >> c >> dd;
u[i]= a;if(a !=-1) d[a]= i;
d[i]= b;if(b !=-1) u[b]= i;
l[i]= c;if(c !=-1) r[c]= i;
r[i]= dd;if(dd !=-1) l[dd]= i;}int pos =0;for(int i =1; i <= m * m ; i ++){if(u[i]==-1&& l[i]==-1) pos = i;}for(int i = pos ; i !=-1; i = d[i]){for(int j = i , k =1; j !=-1&& k <= m ; j = r[j], k ++){if(k != m) cout << j <<" ";else cout << j ;}
cout <<"\n";}return0;}//freopen("文件名.in","r",stdin);//freopen("文件名.out","w",stdout);
K. Browser Games(字典树)
大意:对于前 i 个字符串找出一个最小的前缀集,使得前 i 个字符串都能在前缀集中找到前缀但是后 n - i 个字符串无法在前缀集中找到前缀 , 求对于每一个 i 的最小前缀集大小。
#include<bits/stdc++.h>usingnamespace std;#definefifirst#definesesecond#defineIOSstd::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);//#define int long long//const int INF = 9e18;constint N =2e6+5e5;constint mod =1e9+7;typedef pair<int,int>PII;int tree[N][28], cnt[N], siz[N], idx , n , fa[N];
string s;intget(char c){if(c >='a'&& c <='z')return c -'a';if(c =='.')return26;return27;}voidinsert(string s){int now =0, n = s.size();for(int i =0; i < n ; i ++){int c =get(s[i]);if(!tree[now][c]) tree[now][c]=++ idx;
fa[tree[now][c]]= now;
now = tree[now][c];//每一个now代表一个从根到当前节点的不同的前缀
cnt[now]+=1;}}voidquery(string s ,int& res){int now =0, n = s.size();for(int i =0; i < n ; i ++){int c =get(s[i]);
now = tree[now][c];
cnt[now]-=1;if(!cnt[now]){
res = res - siz[now]+1;int val = siz[now];while(fa[now]) siz[fa[now]]+=(1- val), now = fa[now];break;}}}signedmain(){
IOS
cin >> n;
vector<string>ve;for(int i =1; i <= n ; i ++){
cin >> s;insert(s);
ve.push_back(s);}int res =0;for(int i =0; i < n ; i ++){query(ve[i], res);
cout << res <<"\n";}return0;}//freopen("文件名.in","r",stdin);//freopen("文件名.out","w",stdout);