CodeForce刷题表
Day8
今天把之前一些比较简单的题做一下……发现我这做题有问题啊,每次都是因为一些奇怪的原因导致一直得不出答案……就比如忘了写输入、写输入的位置错了、没删干净……警示一下自己!
题意:
给一个起始位置,和移动步数,如果所在位置是奇数就跳到所在位置+i(i是现在移动的第几次)
如果是偶数就跳到所在位置-i
思路:
这是个循环节,每四个一循环,手写一下就能看出来了。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| #include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e6+5; const ll mod = 1e9+7; int main(){ int _;cin>>_; while(_--) { ll x,n,res=0; cin>>x>>n; if(x&1){ ll pos = n%4; ll t = (n-1) / 4; if(pos == 0) res = x; else if(pos == 1) res = x+1; else if(pos == 2) res = x-1; else if(pos == 3) res = x-4; if(pos==1) res += 4*t; else if(pos==3) res -= 4*t; } else { ll pos = n%4; ll t = (n-1) / 4; if(pos == 0) res = x; else if(pos == 1) res = x-1; else if(pos == 2) res = x+1; else if(pos == 3) res = x+4; if(pos==1) res -= 4*t; else if(pos==3) res += 4*t; } cout<<res<<endl; } return 0; }
|
总结:
循环节% 0的位置是没有移动。
题意:
给你26个位置存a到z,输出一个字符串,需要移动几步。
思路:
mp存位置,起始位置是s[0],然后从1开始遍历,每次更新位置pos即可。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e6+5; const ll mod = 1e9+7; map<char,int> mp; int main(){ int _;cin>>_; while(_--) { string s; cin>>s; int len = s.size(); for(int i=0;i<len;i++) mp[s[i]] = i; cin>>s; len = s.size(); int pos = mp[s[0]]; int res = 0; for(int i=1;i<len;i++) { res += abs(mp[s[i]] - pos); pos = mp[s[i]]; } cout<<res<<endl; } return 0; }
|
总结:
就是mp的运用,应该五分钟内写完的。。
题意:
有一段ab串,修改串使ab的个数 = ba的个数,输出最少修改的串
思路:
如果ab = ba直接输出,否则把最后一个字母换成第一个字母再输出,写一写很容易找出规律
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e6+5; const ll mod = 1e9+7; int main(){ int _;cin>>_; while(_--) { string s; cin>>s; int len = s.size(); int ab=0,ba=0; for(int i=1;i<len;i++) if(s[i-1]=='a'&&s[i]=='b') ab++; else if(s[i-1]=='b'&&s[i]=='a') ba++; if(ab != ba) s[len-1] = s[0]; cout<<s<<endl; } return 0; }
|
总结:
pass
题意:
给一个数组,可以添加任意k到数组任意位置,让每个位置的a[i] <= i
问最少添加几个数
思路:
找到最大的数,让最大的数的位置 = 这个数
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e6+5; const ll mod = 1e9+7; int a[N]; int main(){ int _;cin>>_; while(_--) { int n; cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; int add = 0; for(int i=1;i<=n;i++) if(i + add < a[i]) add = a[i] - i; cout<<add<<endl; } return 0; }
|
总结:
pass