AtCoder Beginner Contest 149

AtCoder Beginner Contest 149

A

Two strings in reverse output

#include <bits/stdc++.h>
using namespace std;
int main() {
    string a,b;
    cin >> a >> b;
    cout << b << a;
    return 0;
}

B

First spent a, and then out of b, the output of the remaining portion of

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main() {
    LL a, b,c;
    cin >> a >> b >> c;
    if(a < c) c -= a,a = 0;
    else a -= c,c = 0;
    if(b < c) c -= b,b = 0;
    else b -= c,c = 0;
    cout << a <<' ' << b;
    return 0;
}

C

Found greater than or equal to \ (X \) the smallest prime number

Binary linear sieve +

Direct violence could go.

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e5 + 100;
bool st[N];
int k,pri[N];

int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    for(int i = 2;i <= N; ++i) {// 线性筛
        if(!st[i]) pri[k ++] = i;
        for(int j = 0;pri[j] <= N / i; ++j) {
            st[pri[j] * i] = 1;
            if(i % pri[j] == 0) break;
        }
    }
    int x;
    cin >> x;
    int l = 0,r = k;
    while(l < r) {// 二分
        int mid = l + r>> 1;
        if(pri[mid] >= x) r = mid;
        else l = mid + 1;
    }
    cout << pri[l];
    return 0;
}

D

Subject to the effect

You play rock paper scissors and a robot, the robot enter a length \ (N \) sequence, \ (R & lt \) representative of stone, \ (S \) a scissors, \ (P \) representative of the cloth

Each \ (K \) times a round, the first round you can out of any gesture, you start from the second round of the gesture and not a round on the same

That \ (Hand [i] = Hand! [I - k] \) , each winning were won \ (r, s, p \ ) a fraction, a draw is not scoring, ask your biggest score wins

Thinking

Observe the subject found that from the beginning of the second round, every time you sign out, must take into account the last round before, can not be repeated.

Well, we first consider, under the current gesture to win this office need, it is not been out of the last round, if turned out, on the same robot gesture to ensure a draw, otherwise it is a victory gesture.

We \ (vis \) array to hold, Current \ (i \) bit is to win out over gesture

With \ (T \) array to hold the robot out of the sequence of gestures

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
char t[N];
bool vis[N];
int r,s,p,n,k,ans = 0;
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin >> n >> k >> r >> s >> p ;
    for(int i = 1;i <= n; ++i) {
        cin >> t[i];
        if(i - k > 0 && vis[i - k] && t[i] == t[i - k]) continue;
        // 如果当前不是第一轮,并且vis[i-k]出过必胜手势,并且t[i]==t[i-k],说明我们只能平局
        if(t[i] == 'r') ans += p;
        if(t[i] == 's') ans += r;
        if(t[i] == 'p') ans += s;
        vis[i] = 1;// 当前局势出过必胜手势
    }
    cout << ans;
    return 0;
}

Guess you like

Origin www.cnblogs.com/lukelmouse/p/12376124.html