思路:首先 他是对1到k 元素做一次变换,然后对2到k+1个元素做一次变化。。。。依次做完。

如果我们对1到k个元素做完一次变换后,把整个数组循环左移一个。那么第二次还是对1 到 k个元素做和第一次一样的变换,再左移,再对1 到 k个元素做和第一次一样的变换,依次做完n-k+1即可。

假设题目要求的变换为C    循环左移变换为P。那么对于每次查询 相当于做  n-k+1  (CP) 变换。最后把答案再向右移动n-k+1  回到原来位置即可。

那么问题就解决了   效率    每次查询n log(n-k+1)

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include <iostream>
#include<vector>
#define MAXN 1000050
#define lson i<<1
#define rson i<<1|1
#define LL long long
using namespace std;
char s[MAXN];
char a[MAXN];
int p[MAXN];
int ans[MAXN];
int tmp[MAXN];
int main() {
int n, m;
scanf(" %s", s);
n = strlen(s);
scanf("%d", &m);
for (int i = 0; i < m; ++i) {
int k, d;
scanf("%d%d", &k, &d);
for (int i = 0; i < n; ++i)
p[i] = ans[i] = i;
int cid = 0;
for (int x = 0; x < d; ++x)
for (int j = x; j < k; j += d)
{
p[cid++] = j;
}
int last = p[0];
for (int j = 0; j <n-1;++j)
p[j] = p[j + 1];
p[n-1] = last;
int x = n - k + 1;
while (x) {
if (x & 1) {
for (int j = 0; j < n; ++j)
tmp[j] = ans[p[j]];
for(int j=0;j<n;++j)
ans[j]=tmp[j];
}
for (int j = 0; j < n; ++j)
tmp[j] = p[p[j]];
for (int j = 0; j < n; ++j)
p[j] = tmp[j];
x >>= 1;
}
for (int j = 0; j < n; ++j)
{
a[j] = s[ans[(j + k - 1) % n]];
}
for (int j = 0; j < n; ++j)
s[j] = a[j];
printf("%s\n", s);
}
}

  

codeforces 484C Strange Sorting Codeforces Round #276 (Div. 1) C的更多相关文章

  1. Codeforces Round #276 (Div. 1) D. Kindergarten dp

    D. Kindergarten Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/proble ...

  2. Codeforces Round #276 (Div. 1) B. Maximum Value 筛倍数

    B. Maximum Value Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/prob ...

  3. Codeforces Round #276 (Div. 1) A. Bits 二进制 贪心

    A. Bits Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/problem/A Des ...

  4. Codeforces Round #276 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/485 A题.Factory 模拟.判断是否出现循环,如果出现,肯定不可能. 代码: #include<cstdio> ...

  5. Codeforces Round #276 (Div. 1) E. Sign on Fence (二分答案 主席树 区间合并)

    链接:http://codeforces.com/contest/484/problem/E 题意: 给你n个数的,每个数代表高度: 再给出m个询问,每次询问[l,r]区间内连续w个数的最大的最小值: ...

  6. CF&&CC百套计划4 Codeforces Round #276 (Div. 1) A. Bits

    http://codeforces.com/contest/484/problem/A 题意: 询问[a,b]中二进制位1最多且最小的数 贪心,假设开始每一位都是1 从高位i开始枚举, 如果当前数&g ...

  7. CF&&CC百套计划4 Codeforces Round #276 (Div. 1) E. Sign on Fence

    http://codeforces.com/contest/484/problem/E 题意: 给出n个数,查询最大的在区间[l,r]内,长为w的子区间的最小值 第i棵线段树表示>=i的数 维护 ...

  8. Codeforces Round #276 (Div. 1) E. Sign on Fence 二分+主席树

    E. Sign on Fence   Bizon the Champion has recently finished painting his wood fence. The fence consi ...

  9. Codeforces Round #276 (Div. 2)

    A. Factory 题意:给出a,m,第一天的总量为a,需要生产为a%m,第二天的总量为a+a%m,需要生产(a+a%m)%m 计算到哪一天a%m==0为止 自己做的时候,把i开到1000来循环就过 ...

随机推荐

  1. C 语言

    1, //         char server[] = BaseNetServerIp; //        string  serverStr = _aliNetServerEnabled?Ne ...

  2. Configuring Network in CentOS 6.3 Virtual Box + Screenshots

    Configuring Network in CentOS 6.3 Virtual Box + Screenshots Posted: May 23, 2013 in Uncategorized Ta ...

  3. js 为字符串添加样式

    <html><body> <script type="text/javascript"> var txt="Hello World!& ...

  4. Canvas 与 Paint 类的 使用

    使用canvas画布和paint画笔可以自定义view 案例:fastindexbar 基本用法 public class DrawView extends View{ private Rect mR ...

  5. 如何在IamgeButton上面添加文字

    如何在IamgeButton上面添加文字? 首先要知道,IamgeButton是不可以直接添加文字的.所以我们需要间接制作一个Button按钮 我的代码将会展示另外一个例子,与本文中的代码相似. 本文 ...

  6. asp.net报错“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”的解决办法

    来源:http://ajxfxb.blog.163.com/blog/static/56675086201411634336878/ 作者是:没完没了的工作 asp.net报错“尝试读取或写入受保护的 ...

  7. webform分页

    前端界面: 当前第[<asp:Label ID="Label_nowpage" runat="server" Text="Label" ...

  8. (十五)ioctl、ifreq、ifconf

    ioctl操作   传统上ioctl函数是用于那些普遍使用,但不适合归入其他类别的任何特性的系统接 口.Posix去掉了ioctl,它通过 创建特殊的其功能已被Posix标准化的包裹函数来代替ioct ...

  9. Yii2使用教程

    安装 中文文档:http://www.yiichina.com/doc/guide/2.0/start-installation 1,安装 这里我直接下载归档文件,压缩包安装了.composer各种麻 ...

  10. sprint one

    产品backlog Id Name Imp Est How to demo 1 各个角色账号登录功能 30 20 建立数据库,数据库中存储一些角色的初始账号以便测试,输入账号密码,系统在数据库中查找账 ...