CodeForces Round #547 Div.3

Posted zlrrrr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces Round #547 Div.3相关的知识,希望对你有一定的参考价值。

A. Game 23

技术图片
#include <bits/stdc++.h>
using namespace std;

int N, M;

int main() {
    int ans = 0;
    int cnt = 0;
    scanf("%d%d", &N, &M);
    if(N == M) ans = 0;
    else if(N > M) ans = -1;
    else {
        if(M % N) ans = -1;
        else {
            int t = M / N;
            while(t % 3 == 0) {
                t /= 3;
                cnt ++;
            }
            while(t % 2 == 0) {
                t /= 2;
                cnt ++;
            }
            if(t == 1) ans = cnt;
            else ans = -1;
        }
    }

    printf("%d
", ans);

    return 0;
}
View Code

B. Maximal Continuous Rest

技术图片
#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 10;
int N;
int a[maxn];

int main() {
    scanf("%d", &N);
    int maxx = 0, cnt = 0;
    for(int i = 1; i <= N; i ++) {
        scanf("%d", &a[i]);
        if(a[i] == 1) {
            cnt ++;
        } else {
            cnt = 0;
        }
        maxx = max(cnt, maxx);
    }
    int num1 = 0;
    for(int i = 1; i <= N; i ++) {
        if(a[i] == 1) num1 ++;
        else break;
    }
    for(int i = N; i >= 1; i --) {
        if(a[i] == 1) num1 ++;
        else break;
    }
    printf("%d
", max(maxx, num1));
    return 0;
}
View Code

C. Polycarp Restores Permutation

技术图片
#include <bits/stdc++.h>
using namespace std;

const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 10;
int N;
long long a[maxn];
map<long long, int> vis;
vector<long long> ans;

int main() {
    scanf("%d", &N);
    for(int i = 1; i < N; i ++)
        scanf("%I64d", &a[i]);
    ans.push_back(1);
    long long minn = 1;
    for(int i = 1; i < N; i ++) {
        ans.push_back(a[i] + ans[i - 1]);
        minn = min(minn, a[i] + ans[i - 1]);
    }

    bool flag = true;
    long long sum;
    if(minn == 1) sum = 0;
    sum = 1 - minn;
    for(int i = 0; i < ans.size(); i ++) {
        ans[i] += sum;
        if(!vis[ans[i]] && ans[i] <= N) vis[ans[i]] ++;
        else {
            flag = false;
            break;
        }
    }

    if(!flag) printf("-1
");
    else {
        for(int i = 0; i < ans.size(); i ++)
            printf("%I64d%s", ans[i], i != ans.size() - 1 ? " " : "
");
    }

    return 0;
}
View Code

D. Colored Boots

技术图片
#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 10;
int N;
string s, t;
int sz = 0, szz = 0;
int vis[30], is[maxn], it[maxn];
map<char, int> mp;
map<char, int> pos, pot;

struct Node{
    char c;
    int num;
    int loc;
}node[maxn], mode[maxn];

vector<Node> v;

bool cmp(const Node &a, const Node &b) {
    if(a.c != b.c) return a.c < b.c;
    else return a.loc < b.loc;
}

int main() {
    scanf("%d", &N);
    cin >> s >> t;
    int cnt = 0, cntt = 0;
    memset(vis, 0, sizeof(vis));
    for(int i = 0; i < N; i ++) {
        if(s[i] >= a && s[i] <= z) mp[s[i]] ++;
        if(s[i] == ?)
            is[sz ++] = i;
    }

    for(int i = 0; i < N; i ++) {
        if(mp[t[i]]) {
            vis[t[i] - a] ++;
            cnt ++;
            node[cnt].loc = i;
            node[cnt].c = t[i];
            mp[t[i]] --;
        } else {
            if(t[i] >= a && t[i] <= z){
                if(sz < 1) continue;
                cnt ++;
                node[cnt].c = t[i];
                node[cnt].loc = i;
                cntt ++;
                mode[cntt].c = t[i];
                mode[cntt].loc = is[sz - 1];
                sz --;
            } else it[szz ++] = i;
        }
    }

    for(int i = 0; i < N; i ++) {
        if(s[i] >= a && s[i] <= z) {
            if(vis[s[i] - a]) {
                cntt ++;
                vis[s[i] - a] --;
                mode[cntt].loc = i;
                mode[cntt].c = s[i];
            } else {
                if(szz >= 1) {
                    cntt ++;
                    mode[cntt].c = s[i];
                    mode[cntt].loc = i;
                    cnt ++;
                    node[cnt].c = s[i];
                    node[cnt].loc = it[szz - 1];
                    szz --;
                }
            }
        }
    }

    if(sz) {
        for(int i = 0; i < sz; i ++) {
            cnt ++;
            node[cnt].loc = it[i];
            node[cnt].c = h;
            cntt ++;
            mode[cntt].loc = is[i];
            mode[cntt].c = h;
        }
    }

    sort(node + 1, node + 1 + cnt, cmp);
    sort(mode + 1, mode + 1 + cntt, cmp);
    printf("%d
", cnt);
    for(int i = 1; i <= cnt; i ++)
        printf("%d %d
", mode[i].loc + 1, node[i].loc + 1);
    return 0;
}
View Code

E. Superhero Battle

技术图片
#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 10;
long long H, N, temp;
long long a[maxn];
long long sum, minn = 0;

int main() {
    cin >> H >> N;
    bool flag = true;
    for(int i = 0; i < N; i ++) {
        cin >> a[i];
        sum += a[i];
        minn = min(minn, sum);
        if(minn + H <= 0 && flag) {
            flag = false;
            temp = i + 1;
        }
    }

    if(!flag) {
        cout << temp <<endl;
        return 0;
    }
    if(sum >= 0) printf("-1
");
    else {
        long long ans = 0;
        if(H + sum < 0) {
            for(int i = 0; i < N; i ++) {
                H += a[i];
                if(H <= 0) {
                    ans = i;
                    break;
                }
            }
            cout << ans + 1 << endl;
        } else {
            long long p = 0;
            long long l = 0, r = H / (-1 * sum) + 1;
            while(l < r) {
                long long mid = (l + r) / 2;
                long long h = H;
                h += mid * sum;
                if(h + minn <= 0) {
                    p = mid;
                    r = mid;
                } else l = mid + 1;
            }

            ans += p * N;
            H += sum * p;

            for(int i = 0; i < N; i ++) {
                H += a[i];
                ans ++;
                if(H <= 0) break;
            }
            cout << ans << endl;
        }
    }

    return 0;
}
View Code

 F1. Same Sum Blocks (Easy)

也太暴力了吧 但是 暴力出奇迹

技术图片
#include <bits/stdc++.h>
using namespace std;

const int inf = 0x3f3f3f3f;
const int maxn = 1e7 + 10;
int N;
int a[maxn], sum[maxn];

struct Node {
    int num;
    vector<pair<int, int> > v;
    int vis[110];
};

map<int, Node> mp;
vector<int> ans;

int main() {
    scanf("%d", &N);
    memset(sum, 0, sizeof(sum));
    for(int i = 1; i <= N; i ++) {
        scanf("%d", &a[i]);
        if(i == 1) sum[i] = a[i];
        else sum[i] = sum[i - 1] + a[i];
    }

    int maxx = -inf, temp = inf;
    for(int i = 1; i <= N; i ++) {
        for(int j = 1; j <= i; j ++) {
            bool flag = true;
            for(int k = j; k <= i; k ++) {
                if(mp[sum[i] - sum[j - 1]].vis[k]) {
                    flag = false;
                    break;
                }
            }
            if(flag) {
                mp[sum[i] - sum[j - 1]].v.push_back({j, i});
                mp[sum[i] - sum[j - 1]].num ++;
                for(int k = j - 1; k <= i; k ++)
                    mp[sum[i] - sum[j - 1]].vis[k] = 1;
            }

            if(mp[sum[i] - sum[j - 1]].num > maxx) {
                temp = sum[i] - sum[j - 1];
                maxx = mp[temp].num;
            }
        }
    }

    printf("%d
", mp[temp].v.size());
    for(int i = 0; i < mp[temp].v.size(); i ++)
        printf("%d %d
", mp[temp].v[i].first, mp[temp].v[i].second);

    return 0;
}
View Code

 FHFHFH QAQ 睡懒觉该醒啦

以上是关于CodeForces Round #547 Div.3的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #547 (Div. 3) F 贪心 + 离散化

Codeforces-Round#547(Div.3)-C-Polycarp Restores Permutation

Codeforces Round# 305 (Div 1)

Codeforces Round #705 (Div. 2)

Codeforces Round #774 (Div. 2)

Codeforces Round #808 (Div. 1)(A~C)