题解 P2902 【[USACO08MAR]珍珠配对Pearl Pairing】

· · 题解

这道题没有更优的解法?

那我来发一波

分成N/2组(毋庸置疑)

每一种颜色数量肯定小于N/2(毋庸置疑)

读入时将每一种展开,依次配对

这种算法实现的依据是每种颜色不超过N/2,所以第i种颜色和i+n/2配对,就可以保证不重复,且不遗漏,如果用其他的方法我觉得麻烦一些

题解区居然没有这种解法

``` #include<map> #include<cmath> #include<stack> #include<queue> #include<cstdio> #include<vector> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef long long ll;//头文件 template<class AK>void r(AK &x){//快读 x=0; int f=0; char ch=getchar(); while(ch<'0'||ch>'9'){ f|=(ch=='-'); ch=getchar(); } while(ch>='0'&&ch<='9'){ x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); } x=f?-x:x; return; } int n,m,a[1000005],top; int main(){ r(n);r(m); for(int i=1;i<=m;i++){ int x; r(x);//读入个数 while(x--)a[++top]=i;//展开 } for(int i=1;i<=n/2;i++)printf("%d %d\n",a[i],a[i+n/2]);//配对输出,就是那么简单 return 0;//结束 } ```