bzoj上现在找不到这题,所以目前只是过了样例,没有测
2054: 疯狂的馒头
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 715 Solved: 298Description
Input
第一行四个正整数N,M,p,q
Output
一共输出N行,第i行表示第i个馒头的最终颜色(如果最终颜色是白色就输出0)。
Sample Input
4 3 2 4
Sample Output
2 2 3 0
HINT
用并查集维护当前馒头之后第一个白馒头的位置,初始f[x]=x
倒着处理,这样一个馒头只会染一次。
详见代码
1 /*by SilverN*/ 2 #include3 #include 4 #include 5 #include 6 #include 7 using namespace std; 8 const int mxn=100000; 9 int f[mxn];10 int co[mxn];11 int n,m,p,q;12 int find(int x){13 return f[x]==x? x:x=find(f[x]);14 }15 int main(){16 int i,j;17 scanf("%d%d%d%d",&n,&m,&p,&q);18 for(i=1;i<=n;i++)f[i]=i;19 for(i=m;i>=1;i--){20 int s=(i*p+q)%n+1;//边界 21 int t=(i*q+p)%n+1;22 if(s>t)swap(s,t);23 for(j=find(s);j<=t;j=find(j)){24 co[j]=i;25 f[j]=j+1;26 }27 }28 for(i=1;i<=n;i++){29 printf("%d\n",co[i]);30 }31 return 0;32 }