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\n",i,n); continue; //cntx.size()==1时全在一列,只有maxx,不存在maxx-1 } if(maxx==1&&maxy==1){ //一定是&& printf("Case %d: 2 %lld\n",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\n",i,max_x,cnt1); } else printf("Case %d: %lld %lld\n",i,max_x-1,cnt2); } return 0; }
分类:
思维
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现