Codeforces Round #540 (Div. 3)

http://codeforces.com/contest/1118

 

A. Water Buying

#include <bits/stdc++.h>
using namespace std;

int T;

int main() {
    scanf("%d", &T);
    while(T --) {
        long long N;
        int a, b;
        cin >> N >> a >> b;
        long long ans = 0;

        bool flag;
        if(2 * a <= b) flag = true;
        else flag = false;

        if(flag) ans = N * a;
        else {
            ans = b * (N / 2);
            ans += a * (N % 2);
        }

        cout << ans << endl;
    }
    return 0;
}
View Code

B. Tanya and Candies

#include <bits/stdc++.h>
using namespace std;

const int maxn = 200010;
int N;
int num[maxn];
long long even[maxn], odd[maxn];

int main() {
    scanf("%d", &N);
    long long sum = 0;
    for(int i = 1; i <= N; i ++) {
        scanf("%d", &num[i]);
        sum += num[i];

        if(i % 2) {
            odd[i] = odd[i - 1] + num[i];
            even[i] = even[i - 1];
        }

        else if(i % 2 == 0) {
            even[i] = even[i - 1] + num[i];
            odd[i] = odd[i - 1];
        }
    }

    int ans = 0;
    for(int i = 1; i <= N; i ++) {
        int sum1 = 0, sum2 = 0;
        sum1 = even[i - 1] + odd[N] - odd[i];
        sum2 = sum - num[i];

        if(sum1 * 2 == sum2) ans ++;
    }

    printf("%d\n", ans);
    return 0;
}
View Code

D1. Coffee and Coursework (Easy version)

#include <bits/stdc++.h>
using namespace std;

int N, M;
vector<int> v;

int main() {
    scanf("%d%d", &N, &M);
    v.resize(N);
    for(int i = 0; i < N; i ++)
        scanf("%d", &v[i]);

    sort(v.rbegin(), v.rend());
    for(int i = 1; i <= N; i ++) {
        int sum = 0;
        for(int j = 0; j < N; j ++) {
            sum += max(v[j] - j / i, 0);
        }
        if(sum >= M) {
            printf("%d\n", i);
            return 0;
        }
    }
    printf("-1\n");
    return 0;
}
View Code

D2. Coffee and Coursework (Hard Version)

#include <bits/stdc++.h>
using namespace std;

int N, M;
vector<int> v;

bool isFinish(int x) {
    long long sum = 0;
    for(int i = 0; i < N; i ++)
        sum += max(0, v[i] - i / x);
    if(sum >= M) return true;
    return false;
}

int main() {
    scanf("%d%d", &N, &M);
    v.resize(N);
    for(int i = 0; i < N; i ++)
        scanf("%d", &v[i]);

    int ans = INT_MAX;
    sort(v.rbegin(), v.rend());
    int l = 1, r = N + 1, mid;
    while(l < r) {
        mid = (l + r) / 2;
        if(isFinish(mid)) {
            r = mid;
            ans = min(ans, mid);
        }
        else l = mid + 1;
    }

    if(ans < INT_MAX) printf("%d\n", ans);
    else printf("-1\n");
    return 0;
}
View Code

E. Yet Another Ball Problem

#include <bits/stdc++.h>
using namespace std;

int N, K;

long long rec(int x) {
    long long res = 0;
    for(int i = 1; i <= x; i ++)
        res += i;

    return res;
}

vector<pair<int, int> > v;

int main() {
    scanf("%d%d", &N, &K);
    if(N > rec(K - 1) * 2) printf("NO\n");
    else {
        printf("YES\n");
        for(int i = 1; i < K; i ++) {
            int j = i + 1;
            while(j <= K) {
                v.push_back({i, j});
                v.push_back({j, i});
                j ++;
            }
            if(v.size() > 200000) break;
        }

        for(int i = 0; i < N; i ++)
            printf("%d %d\n", v[i].first, v[i].second);
    }
    return 0;
}
View Code

 F1. Tree Cutting (Easy Version)

#include <bits/stdc++.h>
using namespace std;

const int maxn = 300005;
int N;
int color[maxn], blue[maxn], red[maxn];
vector<int> v[maxn];
int ans = 0;

void dfs(int child, int parent) {
    if(color[child] == 1) red[child] ++;
    if(color[child] == 2) blue[child] ++;

    for(int i = 0; i < v[child].size(); i ++) {
        if(v[child][i] != parent) {
            dfs(v[child][i], child);
            red[child] += red[v[child][i]];
            blue[child] += blue[v[child][i]];

            if((blue[v[child][i]] == blue[0] && !red[v[child][i]]) || (red[v[child][i]] == red[0] && !blue[v[child][i]]))
                ans ++;
        }
    }
}

int main() {
    scanf("%d", &N);
    memset(red, 0, sizeof(red));
    memset(blue, 0, sizeof(blue));
    for(int i = 1; i <= N; i ++) {
        scanf("%d", &color[i]);
        if(color[i] == 1) red[0] ++;
        else if(color[i] == 2) blue[0] ++;
    }

    for(int i = 0; i < N - 1; i ++) {
        int a, b;
        scanf("%d%d", &a, &b);
        v[a].push_back(b);
        v[b].push_back(a);
    }

    dfs(1, 0);
    printf("%d\n", ans);
    return 0;
}
View Code

 

posted @ 2019-03-05 16:29  丧心病狂工科女  阅读(186)  评论(0编辑  收藏  举报