2017秦皇岛现场赛H.Prime Set(奇偶二分图 匈牙利算法)

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 65536K,其他语言131072K
64bit IO Format: %lld
题目描述
The 2017 China Collegiate Programming Contest Qinhuangdao Site is coming! There will be n teams participating in the contest, and the contest will be held on a huge round table with m seats numbered from 1 to m in clockwise order around it. The i-th team will be seated on the si-th seat.

BaoBao, an enthusiast for competitive programming, has made p predictions of the contest result before the contest. Each prediction is in the form of (ai,bi), which means the ai-th team solves a problem during the bi-th time unit.

As we know, when a team solves a problem, a balloon will be rewarded to that team. The participants will be unhappy if the balloons take almost centuries to come. If a team solves a problem during the ta-th time unit, and the balloon is sent to them during the tb-th time unit, then the unhappiness of the team will increase by tb-ta. In order to give out balloons timely, the organizers of the contest have bought a balloon robot.

At the beginning of the contest (that is to say, at the beginning of the 1st time unit), the robot will be put on the k-th seat and begin to move around the table. If the robot moves past a team which has won themselves some balloons after the robot’s last visit, it will give all the balloons they deserve to the team. During each unit of time, the following events will happen in order:
1.The robot moves to the next seat. That is to say, if the robot is currently on the i-th(1 ≤ i < m) seat, it will move to the (i + 1)-th seat; If the robot is currently on the m-th seat, it will move to the 1st seat.
2.The participants solve some problems according to BaoBao’s prediction.
3.The robot gives out balloons to the team seated on its current position if needed.

BaoBao is interested in minimizing the total unhappiness of all the teams. Your task is to select the starting position k of the robot and calculate the minimum total unhappiness of all the teams according to BaoBao’s predictions.
输入描述:
There are multiple test cases. The first line of the input contains an integer T, indicating the number of test cases. For each test case:
The first line contains three integers n,m and p(1< n ≤ 105), (n ≤ m ≤ 109), (1 ≤ p ≤ 105),indicating the number of participating teams, the number of seats and the number of predictions.
The second line contains n integers (s1,s2,…, sn) (1 ≤ si ≤ m, and si ≠ sj for all i ≠ j),indicating the seat number of each team.
The following p lines each contains two integers ai and bi (1 ≤ ai ≤ n,1 ≤ bi ≤ 109), indicating that the ai-th team solves a problem at time bi according to BaoBao’s predictions.
It is guaranteed that neither the sum of n nor the sum of p over all test cases will exceed 5×105.
输出描述:
For each test case output one integer, indicating the minimum total unhappiness of all the teams according to BaoBao’s predictions.
示例1
输入

4
2 3 3
1 2
1 1
2 1
1 4
2 3 5
1 2
1 1
2 1
1 2
1 3
1 4
3 7 5
3 5 7
1 5
2 1
3 3
1 5
2 5
2 100 2
1 51
1 500
2 1000
输出

1
4
5
50
说明

For the first sample test case, if we choose the starting position to be the 1st seat, the total unhappiness will be (3-1) + (1-1) + (6-4) = 4. If we choose the 2nd seat, the total unhappiness will be (2-1) + (3-1) + (5-4) = 4. If we choose the 3rd seat, the total unhappiness will be (1-1) + (2-1) + (4-4) = 1. So the answer is 1.
For the second sample test case, if we choose the starting position to be the 1st seat, the total unhappiness will be (3-1) + (1-1) + (3-2) + (3-3) + (6-4) = 5. If we choose the 2nd seat, the total unhappiness will be (2-1) + (3-1) + (2-2) + (5-3) + (5-4) = 6. If we choose the 3rd seat, the total unhappiness will be (1-1) + (2-1) + (4-2) + (4-3) + (4-4) = 4. So the answer is 4.

题意:
给你一个数字序列,在该序列中,如果取出某两个数字,这两个数字的和为素数,那么称其为一个素数对。两个素数对可以有交集。
如果在这个数字序列中取出最多k个素数对,问这k个素数对的并集的最大元素数目为多少?

题解:
首先是一个贪心的想法,我们先假设取出的素数对之间不能产生交集,这样我们能获得最多twos个素数对,然后我们在去掉这个假设,再取k – twos个素数对(这里先假设素数对的数目总数不小于k,实际代码中要注意处理素数对的总数小于k的情况)。那么我们的答案就是Ans = twos * 2 + (k – twos).(因为先取不产生交集的素数对对答案贡献是2,取产生交集的素数对的贡献是1,优先取贡献大的素数对总归是不会错的)

这样我们就能把问题转化,给你一个序列,你在这个序列中产生的不相交的素数对的数目最多为多少?

注意到我们素数对之间一定是两两匹配的,因此,我们想到了二分图的最大匹配问题。我们要想办法把这个问题转化成二分图最大匹配问题。那么,我们首要的问题就是,我们要怎么对每个数字进行二分呢?

答案是把奇数作为二分图的A类,偶数作为二分图的B类,然后N方遍历数组得到A->B的全部边,因为(除了数字2,代码中要加特判)绝大部分素数都是奇数,而我们知道奇数+偶数才能等于奇数。因此,这么做我们就能够得到我们所需要的最大匹配数目。

然后我们要特判获得和为数字2的素数对,这种情况只有1和1进行匹配才能出现。因此,我们希望在之前的奇偶匹配中,数字1尽可能不被匹配进去,由于匈牙利算法的特性,我们可以在匹配的过程中先匹配大的数字,再匹配小的数字即可。

整体思路就在上面, 实现上的细节就不说了。

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

#define sf(a) scanf("%d\n",&a)
#define ll long long
#define Ms(a,b) memset(a,b,sizeof(a))
#define M(a) Ms(a,0)
#define rep(i,a,b) for(int i = (a);i < (b);i++)



const int MAXP = 2e6 + 50;
bool isprime[MAXP];
int prime[MAXP / 10], pricnt = 0;
void shai(){
    Ms(isprime, 1);
    pricnt = 0;
    rep(i, 2, MAXP){
        if (isprime[i])prime[pricnt++] = i;
        for (int j = 0; j < pricnt && 1ll * prime[j] * i < MAXP; j++){//注意乘法的时候要加1ll
            isprime[prime[j] * i] = false;
            if (i % prime[j] == 0)break;
        }
    }
}

const int MAXN = 6000;
vector<int> AarcB[MAXN];//记录A->B边
int BarcA[MAXN];        //记录当前B匹配的A
int vis[MAXN];          //记录是否被访问过,用于剪支

int A[MAXN];


bool Hungry(int A,int Bnot = -1){
    rep(i,0,AarcB[A].size()){
        int B = AarcB[A][i],A2 = BarcA[B];  //B为B类中的一个元素,A2为B已经匹配的元素
        if (B == Bnot || vis[A2])continue;  //如果A2pos == 0,也是false
        vis[A2] = 1;                        //注意这里用vis[0]作为哨兵
        if (A2 <= 0 || Hungry(A2,B)){       //B节点无匹配,或者能找出增广路
            BarcA[B] = A;
            return true;
        }
    }
    return false;
}
//匈牙利匹配算法,并返回最大匹配
int solve(const int N,const int M){
    int ret = 0;M(BarcA);
    for(int i = N;i >= 1;i--){
        if(A[i] & 1){
            M(vis);//DFS时候使用的vis数组
            if (Hungry(i))ret++;
        }
    }
    return ret;
}
bool ChoosedA[MAXN];
int main(){
    ios::sync_with_stdio(false);
    shai();
    int T,N,M;cin >> T;
    rep(Case,0,T){
        cin >> N >> M;
        rep(i,0,N + 1)AarcB[i].clear();
        rep(i,1,N + 1)cin >> A[i];
        sort(A + 1,A + N + 1);
        int onecnt = 0;rep(i,1,N + 1)if(A[i] == 1)onecnt++;
        int setcnt = 0,Whatcnt = 0;
        M(ChoosedA);
        rep(i,1,N + 1)rep(j,i + 1,N + 1){
            if(isprime[A[i] + A[j]]){
                if(!ChoosedA[i])Whatcnt++;
                if(!ChoosedA[j])Whatcnt++;
                ChoosedA[i] = ChoosedA[j] = true;
                setcnt++;
                if(A[i] == A[j])continue;//这种情况A[i] == A[j]
                if(A[j] & 1)AarcB[j].push_back(i);
                else AarcB[i].push_back(j);
            }
        }
        int twos = solve(N,M),choosedone = 0;
        rep(i,1,N + 1)if(A[BarcA[i]] == 1)choosedone++;
        twos += (onecnt - choosedone) / 2;
        int ones = Whatcnt - 2 * twos;
        //cout << ">>Twos Ans Ones " << twos << ' ' << ones << endl;
        if(twos >= M)cout << 2 * M << endl;
        else cout << twos * 2 + min(M - twos,ones) << endl;
    }
    return 0;
}



点赞