bzoj2054 疯狂的馒头

bzoj上现在找不到这题,所以目前只是过了样例,没有测

 

2054: 疯狂的馒头

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 715  Solved: 298

Description

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 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 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 }

 

posted @ 2016-06-20 23:04  SilverNebula  阅读(266)  评论(0编辑  收藏  举报
AmazingCounters.com