codeforces 484C Strange Sorting Codeforces Round #276 (Div. 1) C
思路:首先 他是对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的更多相关文章
- Codeforces Round #276 (Div. 1) D. Kindergarten dp
D. Kindergarten Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/proble ...
- 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 ...
- 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 ...
- Codeforces Round #276 (Div. 2) 解题报告
题目地址:http://codeforces.com/contest/485 A题.Factory 模拟.判断是否出现循环,如果出现,肯定不可能. 代码: #include<cstdio> ...
- Codeforces Round #276 (Div. 1) E. Sign on Fence (二分答案 主席树 区间合并)
链接:http://codeforces.com/contest/484/problem/E 题意: 给你n个数的,每个数代表高度: 再给出m个询问,每次询问[l,r]区间内连续w个数的最大的最小值: ...
- CF&&CC百套计划4 Codeforces Round #276 (Div. 1) A. Bits
http://codeforces.com/contest/484/problem/A 题意: 询问[a,b]中二进制位1最多且最小的数 贪心,假设开始每一位都是1 从高位i开始枚举, 如果当前数&g ...
- 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的数 维护 ...
- 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 ...
- Codeforces Round #276 (Div. 2)
A. Factory 题意:给出a,m,第一天的总量为a,需要生产为a%m,第二天的总量为a+a%m,需要生产(a+a%m)%m 计算到哪一天a%m==0为止 自己做的时候,把i开到1000来循环就过 ...
随机推荐
- JDBC三层架构
三层框架: 通常意义上的三层架构就是将整个业务应用划分为:表现层(UI).业务逻辑层(BLL).数据访问层(DAL).区分层次的目的即为了“高内聚,低耦合”的思想. 原理:1:数据访问层:主要是对原始 ...
- 一个简单的物料防错DEMO
前言 快2个月没写过博客了,就算是记流水账似的文章都没时间写,主要是太忙了:太多的bug要修复.太多由于bug引起的异常问题要解决.还有新的项目要开发,不忙怎么行呢?最近利用业余时间在鼓捣一个PDA的 ...
- ahk之路:利用ahk在window7下实现窗口置顶
操作系统:win7 64位 ahk版本:autohotkey_L1.1.24.03 今天安装了AutoHotkey_1.1.24.03.SciTE.PuloversMacroCreator,重新开始我 ...
- 《与小卡特一起学Python》Code1
print "I love pizza!" print "pizza " * 20 print "yum " * 40 print &quo ...
- HDU 5938 Four Operations(四则运算)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- LVS
1.LVS-NAT, DNAT(多目标) 2.LVS-DR(Direct Routing) 返回报文不经过Direct real server 不能跨越路由 调度算法:Scheduling 静态方法 ...
- 1012. The Best Rank (25)
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- Openfire基础
网上很多openfire相关资料,这里做下学习汇总 openfire官网:http://www.igniterealtime.org/ 可以下载openfire.spark.smack安装包及源码,安 ...
- iis设置asp站点
在 IIS 6.0 中,默认设置是特别严格和安全的,这样可以最大限度地减少因以前太宽松的超时和限制而造成的攻击.譬如说默认配置数据库属性实施的最大 ASP 张贴大小为 204,800 个字节,并将各个 ...
- c# 邮件发送功能
//统一由一个邮箱发送录用通知 string strfrom = "";//发件人邮箱地址 string strpow = "";//邮箱密码 string s ...