Educational Codeforces Round 49 (Rated for Div. 2)
Educational Codeforces Round 49 (Rated for Div. 2)
题目链接:https://codeforces.com/contest/1027
A题题解
- 题意:根据一个字符串,经过变化后是否能成为一个回文串(变成相邻的字母,a只能变成b,z只能变成x,其他的有两种可能)(都要变)
- 很简单,只要不满足两个条件比较ASCII码1.相差不为1 2.相差大于2就不行,其他就行
- AC代码
-
#include<bits/stdc++.h> using namespace std; typedef long long ll; //ll a ,b ,c ,d; char a[200]; const ll inff = 0x3f3f3f3f3f3f3f3fLL; //18 int main(){ int n; cin >> n; while(n --){ int t; cin >> t; memset(a,0,sizeof(a)); cin >> a; bool flag = 1; if(t % 2 == 0){ for(int i = 0 ; i < t; i++){ if(a[i] != a[t-i-1]){ if(abs(a[i] - a[t-i-1] ) > 2 || abs(a[i] - a[t-i-1] ) == 1 ) { flag = 0; break; } //cout << "NO" << endl; } } } if(!flag) cout << "NO" << endl; else cout << "YES" << endl; } }#include<bits/stdc++.h> using namespace std; typedef long long ll; //ll a ,b ,c ,d; char a[200]; const ll inff = 0x3f3f3f3f3f3f3f3fLL; //18 int main(){ int n; cin >> n; while(n --){ int t; cin >> t; memset(a,0,sizeof(a)); cin >> a; bool flag = 1; if(t % 2 == 0){ for(int i = 0 ; i < t; i++){ if(a[i] != a[t-i-1]){ if(abs(a[i] - a[t-i-1] ) > 2 || abs(a[i] - a[t-i-1] ) == 1 ) { flag = 0; break; } //cout << "NO" << endl; } } } if(!flag) cout << "NO" << endl; else cout << "YES" << endl; } }
B题题解
- 题意:在矩阵里面放数,先把横纵坐标之和是偶数的放满,从左往右,从上往下,然后从最小的奇数(横纵坐标之和)开始继续往后放数;直到把n*n这些数放完
- 数学推公式,ans1 = n * (a - 1) + b + 1; a+b为奇数:ans = ans1 + n * n ; 为偶数 :ans1 ; 然后都 / 2;
- 可以分类讨论,讨论可以发现共性
- AC代码
-
#include<bits/stdc++.h> using namespace std; typedef long long ll; //ll a ,b ,c ,d; //char a[200]; const ll inff = 0x3f3f3f3f3f3f3f3fLL; //18 int main(){ ll n , t; cin >> n >> t; ll ans ; while(t--){ ll a , b; cin >> a >> b; ans = 0; ans = n * (a - 1) + b + 1; if((a + b)%2!=0) ans += n * n; cout << ans / 2<< endl; } return 0; }
C题题解
- AC代码
-
#include <cstdio> #include <cctype> #include <map> #define FOR(i,a,b) for(int i=a;i<=b;++i) inline char in() { static char buf[10001],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,10000,stdin),p1==p2)?EOF:*p1++; } template <class Tp> void read(register Tp &s) { s=0; register bool neg=0; register char c; while(!isdigit(c=in())) if(c=='-') neg=1; while(s=(s<<3)+(s<<1)+c-48,isdigit(c=in())); s=(neg?-s:s); } int T,n,a[1000005]; std::map<int,int> cnt; int main() { for(read(T);T;--T) { cnt.clear(); read(n); int t,m=0; FOR(i,1,n) read(t),cnt[t]++; bool flg=0; for(std::map<int,int>::iterator it=cnt.begin();it!=cnt.end();++it) { if((*it).second>=2) a[++m]=(*it).first; if((*it).second>=4) { flg=1; int ans=(*it).first; printf("%d %d %d %d\n",ans,ans,ans,ans); break; } } if(flg) continue; int x=a[1],y=a[2]; FOR(i,3,m) if(x*a[i]<a[i-1]*y) x=a[i-1],y=a[i]; printf("%d %d %d %d\n",x,x,y,y); } return 0; }
D题题解
- 题意:一个寝室里有n个房间和一个老鼠,老鼠一开始可能在任意一个房间,老鼠会从房间i跳到a[i]房间,问在哪些房间下陷阱可以用最小的代价抓到老鼠?
- 分析:遍历所有房间,给从这些可能到达的房间打上标记,每次打标志在花费最小的房间布下陷阱
- AC代码
#include<bits/stdc++.h> using namespace std; typedef long long ll; #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize("Ofast") %:pragma GCC optimize("Ofast") %:pragma GCC optimize("inline") %:pragma GCC optimize("-fgcse") %:pragma GCC optimize("-fgcse-lm") %:pragma GCC optimize("-fipa-sra") %:pragma GCC optimize("-ftree-pre") %:pragma GCC optimize("-ftree-vrp") %:pragma GCC optimize("-fpeephole2") %:pragma GCC optimize("-ffast-math") %:pragma GCC optimize("-fsched-spec") %:pragma GCC optimize("unroll-loops") %:pragma GCC optimize("-falign-jumps") %:pragma GCC optimize("-falign-loops") %:pragma GCC optimize("-falign-labels") %:pragma GCC optimize("-fdevirtualize") %:pragma GCC optimize("-fcaller-saves") %:pragma GCC optimize("-fcrossjumping") %:pragma GCC optimize("-fthread-jumps") %:pragma GCC optimize("-funroll-loops") %:pragma GCC optimize("-fwhole-program") %:pragma GCC optimize("-freorder-blocks") %:pragma GCC optimize("-fschedule-insns") %:pragma GCC optimize("inline-functions") %:pragma GCC optimize("-ftree-tail-merge") %:pragma GCC optimize("-fschedule-insns2") %:pragma GCC optimize("-fstrict-aliasing") %:pragma GCC optimize("-fstrict-overflow") %:pragma GCC optimize("-falign-functions") %:pragma GCC optimize("-fcse-skip-blocks") %:pragma GCC optimize("-fcse-follow-jumps") %:pragma GCC optimize("-fsched-interblock") %:pragma GCC optimize("-fpartial-inlining") %:pragma GCC optimize("no-stack-protector") %:pragma GCC optimize("-freorder-functions") %:pragma GCC optimize("-findirect-inlining") %:pragma GCC optimize("-fhoist-adjacent-loads") %:pragma GCC optimize("-frerun-cse-after-loop") %:pragma GCC optimize("inline-small-functions") %:pragma GCC optimize("-finline-small-functions") %:pragma GCC optimize("-ftree-switch-conversion") %:pragma GCC optimize("-foptimize-sibling-calls") %:pragma GCC optimize("-fexpensive-optimizations") %:pragma GCC optimize("-funsafe-loop-optimizations") %:pragma GCC optimize("inline-functions-called-once") %:pragma GCC optimize("-fdelete-null-pointer-checks") # define IOS ios::sync_with_stdio(false); cin.tie(0);cout.tie(0) ll a[200000+ 10] , c[200000 + 10]; ll vis[200000 + 10];//记录是否走过 int main(){ IOS; ll n; ll sum=0 , v; cin >> n; for(int i = 1 ; i <= n ; i ++){ cin >> a[i]; } for(int i = 1 ; i <= n ; i ++){ cin >> c[i]; } ll x; for(int i = 1 ;i <= n ; i++){ x = i; while( !vis[x]){ vis[x] = i; x = c[x]; } //当有重复时,即已经走过,就没必要再走 if(i != vis[x]) continue ; v = x; ll ans = a[x]; while(v != c[x]) { x = c[x]; ans = min(a[x] ,ans); } // cout << ans << endl; sum += ans ; } cout << sum << endl; }
顺带提一下,用加速挂真的快了很多,足足快了4倍(可以用scanf,还是用吧),不过强制氧化没什么用,只快1ms,应该是cf已经开了氧化