Codeforces Round #534 (Div. 2)

Codeforces Round #534 (Div. 2)

A. Splitting into digits

题意:用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;
}
View Code

B. Game with string

题意:给定一由小写字母组成的字符串(1≤|s|≤100000),两玩家每取次走长为2只含1种字母的子串,无法再取者败,问先手玩家能否获胜。

思路:模拟。

Tips:注意s.size()返回值为size_t类型,要先转为int再进行加减。

#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;
}
View Code

C. Grid game

题意:你有一个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;
}
View Code

D. Game with modulo

题意:交互题,有一数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
View Code

一份比较简短:

#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
View Code

 

posted @ 2020-03-21 15:12  Kanoon  阅读(164)  评论(0编辑  收藏  举报