2 条题解
-
3
倍增
我们考虑预处理出来第 位翻转 后的位置,用 存下来,然后把 二进制拆分来把每个位置上的数都变为答案需要的
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> using namespace std; const int N=1e5+10; int n,m,k,cnt=-1; int a[N],p[30]; int f[N][32]; inline int reads(){ char c=getchar(); int x=0,f=1; while(!isdigit(c)){ if(c=='-') f=-1; c=getchar(); } while(isdigit(c)){ x=(x<<3)+(x<<1)+(c^48); c=getchar(); } return x*f; } signed main(){ // freopen("C.in","r",stdin); // freopen("C.out","w",stdout); n=reads(),m=reads(),k=reads(); for(int i=1;i<=n;i++) a[i]=i; for(int i=1;i<=m;i++){ int l,r; l=reads(),r=reads(); reverse(a+l,a+r+1); } for(int i=1;i<=n;i++) f[i][0]=a[i],a[i]=i; for(int j=1;j<=30;j++){ for(int i=1;i<=n;i++) f[i][j]=f[f[i][j-1]][j-1]; } // for(int i=1;i<=n;i++){ // for(int j=0;j<=3;j++){ // cout<<f[i][j]<<" "; // } // puts(""); // } while(k){ int t=k&1; p[++cnt]=t; k>>=1; } // for(int i=0;i<=cnt;i++){ // cout<<p[i]<<" "; // } // puts("\n---"); for(int i=0;i<=cnt;i++){ if(p[i]){ for(int j=1;j<=n;j++){ a[j]=f[a[j]][i]; } } // cout<<i<<" "<<p[i]<<":"; // for(int j=1;j<=n;j++) cout<<a[j]<<" "; } for(int i=1;i<=n;i++) printf("%d\n",a[i]); return 0; }
- 1
信息
- ID
- 105
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 5
- 标签
- (无)
- 递交数
- 41
- 已通过
- 17
- 上传者