2018 ccpc final I. Cockroaches

I. Cockroaches
time limit per test6.0 s
memory limit per test256 MB
inputstandard input
outputstandard output
There are N cockroaches in the field. Cockroach i is located at coordinate (xi,yi). No two cockroaches are located at the same spot. Boss Luo has a powerful pesticide that can instantly kill all cockroaches on the horizontal and vertical line of the spot where it is used. i.e. cockroaches with either the same x coordinate or y coordinate as the pesticide spot will be killed.

Boss Luo wonders how many cockroaches can be killed at most when the pesticide is used in one spot. He is also interested in the number of different subsets of the annihilated cockroaches when the pesticide kills most cockroaches.

Input
The first line of the input gives the number of test cases, T (1≤T≤100). T test cases follow.

For each test case, the first line contains an integers N (1≤N≤105), the number of cockroaches.

The next N lines each contains two integers x and y (1≤x,y≤109), describing the coordinates of the cockroaches.

For at least 80 test cases, it is guaranteed that N≤5000.

Output
For each test case, output one line containing "Case x: y z", where x is the test case number (starting from 1), y is the maximum number of cockroaches that can be killed with pesticide applied on one spot, and z is the number of different subsets of the annihilated cockroaches when the pesticide kills most cockroaches.

Example
inputCopy
2
5
1 2
1 3
2 3
4 5
6 7
3
1 2
2 3
3 1
outputCopy
Case 1: 3 5
Case 2: 2 3
Note
For test case 1, 3 cockroaches can be killed if the pesticide is used optimally. There are 5 possible subsets: {1,2,3},{1,2,4},{1,2,5},{2,3,4},{2,3,5}.

For test case 2, 2 cockroaches can be kill at best. All subsets with 2 cockroaches are possible: {1,2},{1,3},{2,3}.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include  <map>
#include <utility>
#include <vector>
using namespace std;
#define ll long long 
#define P pair<ll,ll>
int t;
ll n;
ll  maxx,maxy,max_x;
ll cntx1,cntx2,cnty1,cnty2;
ll cnt1,cnt2;
/*
记录每个点的横纵坐标所在行列的点的数目
找到最大的maxx,maxy,max_x = maxx+maxy
其实消灭蟑螂的最大数目只能是max_x,或者max_x-1
cnt1 :  max_x 的集合数目
cnt2 :  max_x -1  集合数目
*/
int  main()
{
    scanf("%d",&t);
    for(int i =1;i<=t;i++)
    {
        scanf("%lld",&n);
        map<ll,ll>cntx,cnty;
        vector<P>ve(n);//必须为 (),
        maxx=0;maxy=0;
        for(auto &it : ve){//前面ve(n)一定要是n,不然输入不完
            scanf("%d%d",&it.first,&it.second);
            maxx =  max(maxx,++cntx[it.first]);
            maxy =  max(maxy,++cnty[it.second]);
        }
        //两个if为特判
        if(cntx.size()==1||cnty.size()==1){
            printf("Case %d: %lld 1
",i,n);
            continue;
            //cntx.size()==1时全在一列,只有maxx,不存在maxx-1
        }
        if(maxx==1&&maxy==1){ //一定是&& 
            printf("Case %d: 2 %lld
",i,n*(n-1)/2);
            continue;
        }
        cntx1=0,cntx2=0;cnty1=0,cnty2=0;
        for(auto &it : cntx){
            if(it.second==maxx) cntx1++;
            else if(it.second==maxx-1) cntx2++;
        }
        for(auto &it : cnty){
            if(it.second==maxy) cnty1++;
            else if(it.second==maxy-1) cnty2++;
        }
         max_x = maxx+maxy;//在最好情况下,下面的两个式子成立
        cnt1=cntx1*cnty1;cnt2=cntx1*cnty2+cnty1*cntx2;
        //只需要遍历输入的所有点,因为cntx[]==0||cnty[]==0的无意义
        for(auto &it : ve){
          ll  num = cntx[it.first]+cnty[it.second];
                if(num==max_x) {
                    cnt1--;//这个点会让max_x变为max_x -1
                    cnt2++;
                }          
                else if(num==max_x-1) cnt2--;//这个点会让max_x -1变为max_x -2
        }
        if(cnt1>0) {
            printf("Case %d: %lld %lld
",i,max_x,cnt1);
        }
        else
        printf("Case %d: %lld %lld
",i,max_x-1,cnt2);
        }    
    return 0;
}
原文地址:https://www.cnblogs.com/tingtin/p/10747199.html