zoukankan      html  css  js  c++  java
  • MGIT校赛

    F. Passing notes

    XL was very happy to see the satisfied faces of his classmates who ate the dog food. This time, he wanted to spread more dog food. The students are still sitting in an (n∗n) matrix, and XL, who is sitting in the top left corner, wants to pass k notes to LCY, who is sitting in the bottom right corner. At the same time, he wants the sum of the "hurt index" of all the students passing the k notes to be the highest.

    Attention! The same student will be immune to dog food after being shown to once and will not be harmed again.

    思路:

    费用流版子,注意多组输入。跑最大费用

    拆点,每个点拆成入点和出点,入点连一条容量 (1) 费用为 (-val) 的边加一条容量 (inf) 费用 (0) 的边

    #include<bits/stdc++.h>
    using namespace std;
    
    const int N = 1e6 + 10, inf = 1e8;
    
    int n, k, s, t;
    
    struct Edge {
    	int to, next, flow, cost;
    }E[N << 1];
    
    int tot, head[N];
    void addEdge(int from, int to, int flow, int cost) {
    	E[tot] = { to,head[from],flow,cost }; head[from] = tot++;
    	E[tot] = { from,head[to],0,-cost }; head[to] = tot++;
    }
    
    int vis[N], dis[N], incf[N], pre[N];
    
    bool spfa() {
    	queue<int>Q;
    	memset(vis, 0, sizeof vis);
    	memset(dis, 0x3f, sizeof dis);
    	Q.push(s); dis[s] = 0; vis[s] = 1; incf[s] = inf;
    	while (!Q.empty()) {
    		int u = Q.front(); Q.pop();
    		vis[u] = 0;
    
    		for (int i = head[u]; ~i; i = E[i].next) {
    			int v = E[i].to;
    			if (E[i].flow and dis[v] > dis[u] + E[i].cost) {
    				dis[v] = dis[u] + E[i].cost;
    				incf[v] = min(incf[u], E[i].flow);
    				pre[v] = i;
    				if (not vis[v])vis[v] = 1, Q.push(v);
    			}
    		}
    	}
    	return dis[t] != 0x3f3f3f3f;
    }
    
    void MCMF(int& flow, int& cost) {
    	flow = cost = 0;
    	while (spfa()) {
    		flow += incf[t];
    		cost += dis[t] * incf[t];
    		for (int i = t; i != s; i = E[pre[i] ^ 1].to) {
    			E[pre[i]].flow -= incf[t];
    			E[pre[i] ^ 1].flow += incf[t];
    		}
    	}
    }
    int main() {
    	while(~scanf("%d%d", &n, &k)){
            s = 0, t = 2 * n * n + 1;
            tot = 0;
            memset(head, -1, sizeof head);
            addEdge(s, 1, k, 0);
            addEdge(2 * n * n, t, k, 0);
    
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    int now = (i - 1) * n + j;
                    int x; scanf("%d", &x);
                    int a = now * 2 - 1, b = now * 2;
                    addEdge(a, b, 1, -x);
                    addEdge(a, b, inf, 0);
    
                    if (j < n)addEdge(b, b + 1, inf, 0);
                    if (i < n)addEdge(b, b + (n * 2) - 1, inf, 0);
                }
            }
    
            int flow, cost;
            MCMF(flow, cost);
            printf("%d
    ", -cost); 
        }
    }
    

    I. Prime

    牛客数列互质

    但是牛客上好像可以暴力过

    莫队,基本也是板子。

    这个链表整的有点技巧。

    这个得有些小技巧

    #include<bits/stdc++.h>
    #pragma GCC optimize(2)
    using namespace std;
    
    const int N = 1e5 + 10;
    
    int cnt[N], A[N], n, m, t, ans[N], mp[N];
    
    int pre[N], nxt[N], head, tail;
    void insert(int u) {
    	if (u <= head)return;
    	nxt[u] = nxt[head];
    	nxt[head] = u;
    	pre[u] = head;
    	pre[nxt[u]] = u;
    }
    
    void remove(int u) {
    	if (u <= head)return;
    	nxt[pre[u]] = nxt[u];
    	pre[nxt[u]] = pre[u];
    }
    
    int gcd(int a, int b) {
    
    	return a % b == 0 ? b : gcd(b, a % b);
    }
    struct node {
    	int l, r, k, id;
    }q[N];
    
    
    void ins(int x) {
    	if (--mp[cnt[x]] == 0)remove(cnt[x]);
    	if (++mp[++cnt[x]] == 1)insert(cnt[x]);
    }
    void del(int x) {
    	if (--mp[cnt[x]] == 0)remove(cnt[x]);
    	if (++mp[--cnt[x]] == 1)insert(cnt[x]);
    }
    
    int main() {
    	scanf("%d%d", &n, &m);
    	head = 0, tail = N - 1;
    	nxt[head] = tail;
    	pre[tail] = head;
    
    	t = sqrt(n);
    	for (int i = 1; i <= n; i++)scanf("%d", A + i);
    
    	for (int i = 1; i <= m; i++) {
    		scanf("%d%d%d", &q[i].l, &q[i].r, &q[i].k);
    		q[i].id = i;
    	}
    	sort(q + 1, q + 1 + m, [](node a, node b) {
    		int x = (a.l - 1) / t, y = (b.l - 1) / t;
            return (x ^ y) ? x < y : (x & 1) ? a.r < b.r : a.r > b.r;
    		//return x == y ? a.r < b.r : x < y;
    		});
    	int l = 1, r = 0;
    	for (int i = 1; i <= m; i++) {
    		int ql = q[i].l, qr = q[i].r, k = q[i].k;
    
    		while (l > q[i].l)
    			ins(A[--l]);
    		while (l < q[i].l)
    			del(A[l++]);
    		while (r > q[i].r)
    			del(A[r--]);
    		while (r < q[i].r)
    			ins(A[++r]);
    
    		int tot = 0;
    		for (int u = nxt[head]; u != tail; u = nxt[u]) {
    			if (gcd(u, k) == 1)tot += mp[u];
    		}
    		ans[q[i].id] = tot;
    	}
    	for (int i = 1; i <= m; i++) {
    		printf("%d
    ", ans[i]);
    	}
    }
    
  • 相关阅读:
    数据的独立同分布检验
    基于密度聚类的DBSCAN和kmeans算法比较
    Python 爬虫笔记、多线程、xml解析、基础笔记(不定时更新)
    多进程之multiprocessing模块、守护进程、互斥锁
    程序与进程的区别,并发与并行的区别,多进程的实现原理
    并发编程之守护进程
    MySQL帮助文档的使用
    MySQL操作之DCL
    MySQL操作之DML
    MySQL操作之DDL
  • 原文地址:https://www.cnblogs.com/sduwh/p/13955351.html
Copyright © 2011-2022 走看看