Codeforces Round #503

A. New Building for SIS

ps:是真的烦。没有考虑在同一个塔里面,用了20分钟debug。

#pragma warning(disable:4996)
#include<cstdio>
#include<deque>
#include<vector>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;

inline void upd(int &x, int y) {
    x < y && (x = y);
}

int n, h, a, b, k;

int main()
{
    while (cin >> n >> h >> a >> b >> k) {
        int fa, fb, ta, tb;
        while (k--) {
            scanf("%d %d %d %d", &ta, &fa, &tb, &fb);
            if (ta == tb) {
                printf("%d
", abs(fa - fb));
                continue;
            }
            int res;

            if ((a <= fa && fa <= b) || (a <= fb && fb <= b)) {
                res = abs(ta - tb) + abs(fa - fb);
            }
            else {
                int x = abs(fa - a) + abs(ta - tb) + abs(a - fb);
                int y = abs(fa - b) + abs(ta - tb) + abs(b - fb);
                res = min(x, y);
            }
            printf("%d
", res);
        }

    }
    return 0;
}
View Code

B. Badge

ps:瞎写都能过

#pragma warning(disable:4996)
#include<cstdio>
#include<deque>
#include<vector>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;

inline void upd(int &x, int y) {
    x < y && (x = y);
}

int n, ans;

vector<int> G[1004];
bool use[1005];

void DFS(int u) {
    use[u] = 1;
    for (auto v : G[u]) {
        if (use[v]) {
            ans = v;
            return;
        }
        else DFS(v);
    }
}

int main()
{
    while (cin >> n) {
        for (int i = 1; i <= n; ++i) G[i].clear();
        for (int i = 1; i <= n; ++i) {
            int u;
            scanf("%d", &u);
            G[i].push_back(u);
        }
        for (int i = 1; i <= n; ++i) {
            memset(use, 0, sizeof(use));
            DFS(i);
            printf("%d ", ans);
        }
        printf("
");
    }
    return 0;
}
View Code

C. Elections

ps:想到了解法,枚举票数,为啥能一定能得到正解呢?因为排序后,如果某个政党如果多余party党的票数,那么该政党花费最小的几个人一定会被安排。如果都安排后票数还是不够的话就从小到大收买,这样能确保得到最优解。半个小时没写出来~~~~,why?

#pragma warning(disable:4996)
#include<cstdio>
#include<deque>
#include<vector>
#include<cstring>
#include<algorithm>
#include<iostream>
#define ll long long 
using namespace std;

inline void upd(int &x, int y) {
    x < y && (x = y);
}

const int N = 3004;
const ll INF = 1000000000000000;

int n, m;
ll cnt[N], mnt[N];
bool use[N];
pair<ll,ll> so[N];

int main()
{
    while (cin >> n >> m) {
        memset(mnt, 0, sizeof(mnt));
        int ma = 0;
        for (int i = 1; i <= n; ++i) {
            ll p, c;
            scanf("%I64d %I64d", &p, &c);
            so[i].first = c;
            so[i].second = p;
            mnt[p]++;
            upd(ma, mnt[p]);
        }

        sort(so + 1, so + n + 1);

        ll ans = INF;
        for (int i = 0; i <= ma + 1; ++i) {
            ll res = 0, tot = 0;
            for (int j = 1; j <= n; ++j) cnt[so[j].second] = mnt[so[j].second];
            memset(use, 0, sizeof(use));

            for (int j = 1; j <= n; ++j) if (so[j].second != 1 && cnt[so[j].second] >= cnt[1] + i) {
                res += so[j].first;
                cnt[so[j].second]--;
                tot++;
                use[j] = 1;
            }
            if (tot > i) continue;
            for (int j = 1; j <= n; ++j) if (!use[j] && so[j].second != 1 && tot < i) {
                res += so[j].first;
                tot++;
            }

            ans = min(ans, res);
        }
        cout << ans << endl;
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/zgglj-com/p/9462334.html