2016 ACM/ICPC Asia Regional Qingdao Online

天天不知道忙些什么,先把代码留作 回忆(ˇˍˇ) 想~

A
题意:问你 >=n 的最小的 x ,使得x=2a3b4c5d其中 abcd>=0
思路:先预处理统计一下,然后每次二分查询。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <cstdlib>
#define ll o<<1
#define rr o<<1|1
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e4 + 10;
const int INF = 1e9 + 10;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
LL A[300000 + 10];
int top;
LL f[4][40];
const int N = 1e9;
int main()
{
    f[0][0] = 1;
    for(int i = 1; i <= 30; i++) {
        f[0][i] = f[0][i - 1] * 2;
    }

    f[1][0] = 1;
    for(int i = 1; i <= 18; i++) {
        f[1][i] = f[1][i - 1] * 3;
    }

    f[2][0] = 1;
    for(int i = 1; i <= 12; i++) {
        f[2][i] = f[2][i - 1] * 5;
    }

    f[3][0] = 1;
    for(int i = 1; i <= 10; i++) {
        f[3][i] = f[3][i - 1] * 7;
    }

    top = 0;
    for(int a = 0; a <= 30; a++) {
        LL ans = f[0][a];
        if(ans > N) break;

        for(int b = 0; b <= 18; b++) {
            if(ans * f[1][b] > N) break;
            ans *= f[1][b];

            for(int c = 0; c <= 12; c++) {
                if(ans * f[2][c] > N) break;
                ans *= f[2][c];

                for(int d = 0; d <= 10; d++) {
                    if(ans * f[3][d] > N) break;
                    ans *= f[3][d];
                    A[top++] = ans;
                    ans /= f[3][d];
                }
                ans /= f[2][c];
            }
            ans /= f[1][b];
        }
        ans /= f[0][a];
    }
    sort(A, A + top);
    int t; scanf("%d", &t);
    while(t--) {
        int n; scanf("%d", &n);
        int pos = lower_bound(A, A + top, n) - A;
        printf("%lld\n", A[pos]);
    }
    return 0;
}

B
题意:问你 ni=11i
思路: n 足够大时 是可以取极值的。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <cstdlib>
#define ll o<<1
#define rr o<<1|1
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e6 + 10;
const int INF = 1e9 + 10;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
double ans[MAXN];
char str[MAXN];
int main()
{
    double sum = 0;
    for(int i = 1; i <= 1000000; i++) {
        sum = sum + 1.0 / i / i;
        ans[i] = sum;
    }
    while(scanf("%s", str) != EOF) {
        int len = strlen(str);
        if(len >= 7) {
            printf("%.5lf\n", ans[1000000]);
        }
        else {
            int n = 0;
            for(int i = 0; i < len; i++) {
                n = n * 10 + (str[i] - '0');
            }
            printf("%.5lf\n", ans[n]);
        }
    }
    return 0;
}

D

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <cstdlib>
#define ll o<<1
#define rr o<<1|1
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e5 + 10;
const int INF = 1e9 + 10;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
int main()
{
    LL L, R;
    while(scanf("%lld%lld", &L, &R) != EOF) {
        if(R <= 1) {
            printf("0\n");
        }
        else if(R <= 2) {
            printf("1\n");
        }
        else {
            printf("%lld\n", max((R - max(1LL, L) - 2) / 2 + 2, 2LL));
        }
    }
    return 0;
}

E

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <cstdlib>
#define ll o<<1
#define rr o<<1|1
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e5 + 10;
const int INF = 1e9 + 10;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
int main()
{
    int t; scanf("%d", &t);
    while(t--) {
        int n; scanf("%d", &n);
        printf(n & 1 ? "Balanced\n" : "Bad\n");
    }
    return 0;
}

F
题意:每个点的价值是Xi,让你找到一个欧拉路径使得路径上所经过的 X 值异或之和最大。

思路:首先存在欧拉路径的话,那么只有两种情况。
如果存在欧拉回路的话枚举起点即可,其他情况路径则是唯一的。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <cstdlib>
#define ll o<<1
#define rr o<<1|1
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e5 + 10;
const int INF = 1e9 + 10;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
int f[MAXN];
int Find(int p) {
    int t, son = p;
    while(p != f[p]) { p = f[p]; }
    while(son != p) { t = f[son]; f[son] = p; son = t; }
    return p;
}
vector<int> G[MAXN];
int d[MAXN], a[MAXN];
void Merge(int x, int y) {
    int fx = Find(x);
    int fy = Find(y);
    if(fx != fy) f[fx] = fy;
}
int main()
{
    int t; scanf("%d", &t);
    while(t--) {
        int n, m; scanf("%d%d", &n, &m);
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            G[i].clear();
            f[i] = i; d[i] = 0;
        }
        while(m--) {
            int u, v; scanf("%d%d", &u, &v);
            G[u].push_back(v);
            G[v].push_back(u);
            d[u]++; d[v]++;
            Merge(u, v);
        }
        int cnt = 0;
        for(int i = 1; i <= n; i++) {
            if(f[i] == i) {
                cnt++;
            }
        }
        if(cnt > 1) {
            printf("Impossible\n");
            continue;
        }
        cnt = 0; int u, v; u = v = -1;
        for(int i = 1; i <= n; i++) {
            if(d[i] & 1) {
                cnt++;
                if(u == -1) {
                    u = i;
                }
                else if(v == -1) {
                    v = i;
                }
            }
        }
        if(cnt == 0 || cnt == 2) {
            int ans = 0;
            for(int i = 1; i <= n; i++) {
                d[i] /= 2;
                if(d[i] > 0 && (d[i] & 1)) {
                    ans ^= a[i];
                }
            }
            if(cnt == 2) {
                ans ^= a[u]; ans ^= a[v];
            }
            else {
                int res = ans;
                ans = (res ^ a[1]);
                for(int i = 2; i <= n; i++) {
                    ans = max(ans, res ^ a[i]);
                }
            }
            printf("%d\n", ans);
        }
        else {
            printf("Impossible\n");
            continue;
        }
    }
    return 0;
}

G
题意:给定N个序列,每次合并序列的代价就是所合并序列的元素之和,你需要把 N 个序列合并成一个序列。现在给定一个花费T,让你找到一个最小的 k 使得每次合并不超过k的前提下花费 <=T <script type="math/tex" id="MathJax-Element-14"><= T</script>。
思路:二分,然后用两个队列去维护。最后一次合并 k 个是最优的,那么需要先求出第一次合并的序列个数x,然后每次合并 k <script type="math/tex" id="MathJax-Element-17">k</script>个。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <cstdlib>
#define ll o<<1
#define rr o<<1|1
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e5 + 10;
const int INF = 1e9 + 10;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
int n;
LL m, a[MAXN];
LL Work(int k) {
    queue<LL> X, Y; LL ans = 0;
    int d = (n - 1) % (k - 1);
    if(d != 0) {
        for(int i = 1; i <= d + 1; i++) {
            ans += a[i];
        }
        X.push(ans);
        for(int i = d + 2; i <= n; i++) {
            Y.push(a[i]);
        }
    }
    else {
        for(int i = 1; i <= n; i++) {
            Y.push(a[i]);
        }
    }
    while(X.size() + Y.size() > k) {
        LL res = 0;
        for(int i = 1; i <= k; i++) {
            if(!X.empty() && !Y.empty()) {
                if(X.front() > Y.front()) {
                    res += Y.front(); Y.pop();
                }
                else {
                    res += X.front(); X.pop();
                }
            }
            else if(!X.empty()) {
                res += X.front(); X.pop();
            }
            else if(!Y.empty()) {
                res += Y.front(); Y.pop();
            }
        }
        X.push(res); ans += res;
    }
    while(!X.empty()) {
        ans += X.front(); X.pop();
    }
    while(!Y.empty()) {
        ans += Y.front(); Y.pop();
    }
    return ans;
}
int main()
{
    int t; scanf("%d", &t);
    while(t--) {
        scanf("%d%lld", &n, &m);
        for(int i = 1; i <= n; i++) {
            scanf("%lld", &a[i]);
        }
        sort(a + 1, a + n + 1);
        //printf("%lld\n", Work(3));
        int l = 2, r = n, ans;
        while(r >= l) {
            int mid = l + r >> 1;
            if(Work(mid) <= m) {
                ans = mid;
                r = mid - 1;
            }
            else {
                l = mid + 1;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

K
裸的最短路 + 最小割

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <cstdlib>
#define ll o<<1
#define rr o<<1|1
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e4 + 10;
const int INF = 1e9 + 10;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
struct Map1 {
    struct Edge {
        int from, to, cap, flow, next;
    };
    Edge edge[MAXN * 4];
    int head[MAXN], edgenum, cur[MAXN];
    void init() { CLR(head, -1); edgenum = 0; }
    void addEdge(int u, int v, int w) {
        Edge E1 = {u, v, w, 0, head[u]};
        edge[edgenum] = E1;
        head[u] = edgenum++;
        Edge E2 = {v, u, -w, 0, head[v]};
        edge[edgenum] = E2;
        head[v] = edgenum++;
    }
    bool vis[MAXN]; int dist[MAXN];
    bool BFS(int s, int t) {
        queue<int> Q; CLR(dist, -1); CLR(vis, false); Q.push(s);
        vis[s] = true; dist[s] = 0;
        while(!Q.empty()) {
            int u = Q.front(); Q.pop();
            for(int i = head[u]; i != -1; i = edge[i].next) {
                Edge E = edge[i];
                if(!vis[E.to] && E.cap > E.flow) {
                    dist[E.to] = dist[u] + 1;
                    if(E.to == t) return true;
                    Q.push(E.to); vis[E.to] = true;
                }
            }
        }
        return false;
    }
    int DFS(int x, int a, int t) {
        if(x == t || a == 0) return a;
        int flow = 0, f;
        for(int &i = cur[x]; i != -1; i = edge[i].next) {
            Edge &E = edge[i];
            if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(E.cap - E.flow, a), t)) > 0) {
                edge[i].flow += f;
                edge[i^1].flow -= f;
                flow += f; a -= f;
                if(a == 0) break;
            }
        }
        return flow;
    }
    int Maxflow(int s, int t) {
        int flow = 0;
        while(BFS(s, t)) {
            memcpy(cur, head, sizeof(head));
            flow += DFS(s, INF, t);
        }
        return flow;
    }
};
Map1 dinic;
struct Map2 {
    struct Edge {
        int from, to, val, wood, next;
    };
    Edge edge[MAXN * 4];
    int head[MAXN], edgenum;
    void init() { CLR(head, -1); edgenum = 0; }
    void addEdge(int u, int v, int w1, int w2) {
        Edge E1 = {u, v, w1, w2, head[u]};
        edge[edgenum] = E1;
        head[u] = edgenum++;
        Edge E2 = {v, u, w1, w2, head[v]};
        edge[edgenum] = E2;
        head[v] = edgenum++;
    }
    bool vis[MAXN]; int dist[MAXN];
    void BFS(int s, int t) {
        CLR(vis, false); CLR(dist, INF);
        queue<int> Q; Q.push(s); vis[s] = true; dist[s] = 0;
        while(!Q.empty()) {
            int u = Q.front(); Q.pop(); vis[u] = false;
            for(int i = head[u]; i != -1; i = edge[i].next) {
                int v = edge[i].to;
                if(dist[v] > dist[u] + edge[i].val) {
                    dist[v] = dist[u] + edge[i].val;
                    if(!vis[v]) {
                        vis[v] = true; Q.push(v);
                    }
                }
            }
        }
    }
};
Map2 SPFA;
int main()
{
    int t; scanf("%d", &t);
    while(t--) {
        int n, m; scanf("%d%d", &n, &m); SPFA.init();
        while(m--) {
            int u, v, w; scanf("%d%d%d", &u, &v, &w);
            SPFA.addEdge(u, v, 1, w);
        }
        SPFA.BFS(1, n); dinic.init();
        for(int i = 1; i <= n; i++) {
            for(int j = SPFA.head[i]; j != -1; j = SPFA.edge[j].next) {
                int v = SPFA.edge[j].to;
                if(SPFA.dist[i] + 1 == SPFA.dist[v]) {
                    dinic.addEdge(i, v, SPFA.edge[j].wood);
                }
            }
        }
        printf("%d\n", dinic.Maxflow(1, n));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值