题意:用1~9种类尽量少的数字拆分给定正整数n(1≤n≤1000)。
思路:签到题。
#include <bits/stdc++.h> using namespace std; int main() { int n;cin>>n; cout<<n<<"\n"; for(int i=0;i<n;i++) cout<<"1 "; return 0; }
题意:给定一由小写字母组成的字符串(1≤|s|≤100000),两玩家每取次走长为2只含1种字母的子串,无法再取者败,先手是否能胜。
思路:模拟。
#include <bits/stdc++.h> using namespace std; int main() { string s;cin>>s; int cnt=0; for(int i=0;i<int(s.size())-1;i++){ while(i<int(s.size())-1&&s[i]==s[i+1]){ s.erase(i,2); ++cnt; i=max(0,i-1); } } cout<<(cnt&1?"Yes":"No")<<"\n"; return 0; }
题意:你有一个4x4的网格,给定一个01串(1≤|s|≤1000),0为2x1的方块,1为1x2的方块,每一行或列放满即可清空,不同方块不能重叠放置,输出每个方块放置时所在最小行列号(输出任一方案即可)。
思路:可取第一列放置0方块,第二三列放置1方块。
#include <bits/stdc++.h> using namespace std; int main() { string s;cin>>s; int zero=0,one=0; for(char c:s){ if(c==‘0‘) cout<<(zero?1:3)<<‘ ‘<<1<<"\n",++zero%=2; else cout<<one+1<<‘ ‘<<2<<"\n",++one%=4; } return 0; }
题意:交互题,有一数a(1≤a≤109),每次可用x,y询问(0≤x,y≤2x109),若x%a≤y%a,返回‘x‘,否则返回‘y’,最多询问60次,试确定该数。
思路:先确定位于(0,1),(1,2),(2,4),(4,8),…,(229,230)中哪一区间(最多询问31次),之后再对区间进行二分即可(最多询问29次)。
贴两份代码:
一份比较清晰:
#include <bits/stdc++.h> using namespace std; inline bool game() { string str; cin >> str; return str == "start"; } inline bool query(const int& x, const int& y) { cout << "? " << x << ‘ ‘ << y << endl; char ch; cin >> ch; return ch == ‘y‘; } inline void solve(const int& res) {cout << "! " << res << endl;} int main() { while (game()) { int now = 1; while (query(now, now << 1)) now <<= 1; if (now == 1) { solve(query(2, 3) ? 2 : 1); continue; } int left = now, right = now << 1; while (right - left ^ 1) { int mid = left + right >> 1; if (query(left, mid)) left = mid; else right = mid; } solve(right); } }//Author:MAOoo
一份比较精巧:
#include <bits/stdc++.h> using namespace std; char s[20]; int n,i,l,r,mid,ans; int ask(int x,int y){ printf("? %d %d\n",x,y); fflush(stdout); scanf("%s",s); return s[0]==‘x‘; } int solve(){ if(ask(0,1)) return 1; for(i=2;;i<<=1) if(!ask(i,i>>1)) break; for(l=i>>1,r=i,i>>=1;l<=r;) ask(mid=l+r>>1,i)?l=(ans=mid)+1:(r=mid-1); return ans+1; } int main(){ for(;scanf("%s",s),s[0]!=‘e‘;) printf("! %d\n",solve()),fflush(stdout); }//Author:vinayrajmanchala
Codeforces Round #534 (Div. 2)
原文:https://www.cnblogs.com/Kanoon/p/12539478.html