首页 > 其他分享 >[ABC263G] Erasing Prime Pairs

[ABC263G] Erasing Prime Pairs

时间:2024-09-20 17:36:17浏览次数:1  
标签:Erasing Pairs idx int flow head cost ABC263G dis

题目

image

思路

看到配对,想到网络流。

考虑如果一个点是奇数,那么将源点与其连接,如果是偶数,那么将汇点与其连接,如果一对奇数和偶数的和是质数,那么将它们两对应的点相连。其中,我们要对 1 特殊处理,因为 \(1 + 1 = 2\) 而 \(2\) 是偶数且是质数,所以考虑费用流,尽可能多地保留 \(1\),对所有不是 \(1\) 的奇数,连边不要费用,对于 \(1\),费用为 \(1\)。最后答案为 \(maxflow + \left\lfloor\dfrac{mincost}{2}\right\rfloor\)。

代码

#include <bits/stdc++.h>

#define int long long

using namespace std;

const int N = 5010, M = 100010, INF = 0x3f3f3f3f3f3f3f3f;

struct edge {
    int to, next, w, cost; 
} e[M];

int head[N], idx = 1;

void add(int u, int v, int w, int cost) {
    idx++;
    e[idx].to = v;
    e[idx].next = head[u];
    e[idx].w = w;
    e[idx].cost = cost;
    head[u] = idx;
    
    idx++;
    e[idx].to = u;
    e[idx].next = head[v];
    e[idx].w = 0;
    e[idx].cost = -cost;
    head[v] = idx;
}

int n, S, T;
int dis[N], pre[N], flow[N];
bool st[N];

bool spfa() {
    queue<int> q;
    q.push(S);
    memset(dis, 0x3f, sizeof(dis));
    memset(flow, 0, sizeof(flow));
    dis[S] = 0;
    flow[S] = INF;
    st[S] = true;
    
    while (q.size()) {
        int t = q.front();
        q.pop();
        st[t] = false;
        
        for (int i = head[t]; i; i = e[i].next) {
            int to = e[i].to;
            if (e[i].w && dis[to] > dis[t] + e[i].cost) {
                dis[to] = dis[t] + e[i].cost;
                pre[to] = i;
                flow[to] = min(flow[t], e[i].w);
                if (!st[to]) {
                    q.push(to);
                    st[to] = true;
                }
            }
        }
    }
    return flow[T] > 0;
}

int a[N], b[N];

bool prime(int x) {
    if (x <= 2) return false;
    for (int i = 2; i <= x / i; i++) {
        if (x % i == 0) {
            return false;
        }
    }
    return true;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];
    S = n + 1, T = n + 2;
    int cnt1 = 0;
    for (int i = 1; i <= n; i++) {
        if (a[i] % 2 == 1) {
            add(S, i, b[i], a[i] == 1);
            if (a[i] == 1) cnt1 += b[i];
        }
        else {
            add(i, T, b[i], 0);
        }
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (prime(a[i] + a[j]) && a[i] % 2 == 1 && a[j] % 2 == 0) {
                add(i, j, 0x3f3f3f3f3f3f3f3f, 0);
            }
        }
    }
    
    int maxflow = 0, mincost = 0;
    while (spfa()) {
        maxflow += flow[T];
        mincost += flow[T] * dis[T];
        
        int x = T;
        while (x != S) {
            e[pre[x]].w -= flow[T];
            e[pre[x] ^ 1].w += flow[T];
            x = e[pre[x] ^ 1].to;
        }
    }
    cout << maxflow + (cnt1 - mincost) / 2 << '\n';
    return 0;
}

标签:Erasing,Pairs,idx,int,flow,head,cost,ABC263G,dis
From: https://www.cnblogs.com/Yuan-Jiawei/p/18422925

相关文章

  • Improving Weakly-Supervised Object Localization Using Adversarial Erasing and Ps
    一、背景        CAM的方法通常只定位了对象中最具判别性的部分(训练过程中缺乏详细的位置信息),后续一些先进的方法定位目标区域包括:利用多个特征映射;采用对抗性擦除;合并伪标签;设计替换架构;引入额外处理或者利用单独的网络或者伪标签生成器等    这篇论文专注......
  • RestoreFormer++: Towards Real-World Blind Face Restoration from Undegraded Key-V
    RestoreFormer++:TowardsReal-WorldBlindFaceRestorationfromUndegradedKey-ValuePairs(IEEE,2023,8)PaperGitHub动机:认为之前的模型都只关注了图像的纹理信息,而忽视了人脸的细节信息,本文采用多尺度、交叉注意力的方式引入模型的语义信息.总体可以分为两大部分:......
  • C. Turtle and Good Pairs
    https://codeforces.com/contest/2003/problem/C题意:。。。思路:如果要使满足条件的有序对最多,那么首先如果两个字符相等,那么无论如何排列,最终的贡献值都不会变。再看字符不相等的情况,假如有aabbcc,那么abcabc总是优于aabbcc,因为如果一个字符出现了多次,那么像aab,bcc这种就会没......
  • 【题解】P4648 [IOI2007] pairs 动物对数
    Problem给定模板\(B(1\leB\le3)\),代表\(B\)维空间。其中有\(n\)个点,给出坐标与坐标上限\(M\),求\(n\)个点中曼哈顿距离\(\leD\)的对数。Solve\(B=1\)考虑将问题化简成:求\(\sum\limits_{i=1}^n\sum\limits_{j=1}^{i-1}[dis(i,j)\leqD]\)。其中\(dis(i,j)\)......
  • LeetCode 1530. Number of Good Leaf Nodes Pairs
    原题链接在这里:https://leetcode.com/problems/number-of-good-leaf-nodes-pairs/description/题目:Youaregiventhe root ofabinarytreeandaninteger distance.Apairoftwodifferent leaf nodesofabinarytreeissaidtobegoodifthelengthof thesh......
  • LeetCode 2097. Valid Arrangement of Pairs
    原题链接在这里:https://leetcode.com/problems/valid-arrangement-of-pairs/description/题目:Youaregivena 0-indexed 2Dintegerarray pairs where pairs[i]=[starti,endi].Anarrangementof pairs is valid ifforeveryindex i where 1<=i<pairs.l......
  • E - Remove Pairs(状压dp+博弈论)
     ​思路:容易发现,我们取走一张牌后:如果下一步的人必败,则这一步的人必胜,因为可以走这个状态。反之,这个人必败。代码:#include<bits/stdc++.h>usingnamespacestd;intn,a[21],b[21],f[2000005];intmain(){ios::sync_with_stdio(false);cin.tie(0);cout.t......
  • CF Div. 3 C Beautiful Triple Pairs
    原题链接标签组合数学(combinatorics)数据结构(datastructures)题目大意一个数组\(a\)。对于每个\(j\)(\(1\lej\len-2\))写出一个由\([a_j,a_{j+1},a_{j+2}]\)组成的三元组。满足以下条件之一,那么这对三元组就是美丽的:\(b_1\nec_1\)和\(b_2=c_2\)......
  • ABC354 E - Remove Pairs 做题笔记
    ABC354E-RemovePairs做题笔记题目链接对于这种带有博弈论的dp,考虑这样设计状态:令\(f_s\in\{1,0\}\)表示“游戏局面”为\(s\)时,先手必胜还是必败。本题中,“游戏局面”可以表示为剩余卡牌的编号集合。又因为本题中\(N\)​很小,通过状压,可以直接用一个int表示游戏......
  • Vim插件之auto-pairs
     本文结构:a、简介b、安装auto-pairsc、使用d、注意事项a、jiangmiao/auto-pairs:这个插件可以自动补全括号、引号等符号,提高编程效率。要安装和使用插件,通常需要一个插件管理器,如Vundle或Volt。这些管理器可以帮助你方便地安装、更新和卸载插件。安装插件后,你可能还需要在......