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来循环就过 ...
随机推荐
- laravel的学习感受
学习laravel也有一段时间了自己感觉还是不怎么难就是自己的代码不熟悉所以很多的不怎么会.
- UVALive 3971 组装电脑
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- php变量赋值给js
1 2 3 4 5 6 7 8 $(document).ready(function(){ <?php $f="'name'"?> ...
- Lua热更系统
1.介绍 使用脚本开发游戏业务逻辑其中一个好处就是代码可线上热更,不停机修复bug.而热更代码的写法与需要被热更的文件的代码又有着密切的关系,本文介绍一种热更方法. 2.热更原理 Lua提供一个叫re ...
- iOS10 拍照崩溃问题
根据相对应得功能添加相关权限即可,没必要全部添加,后面的描述可以官方点,因为会以弹出框的形式访问的,比如相机权限后面的描述可以为:这个应用需要访问相机:后续如果发现其他iOS10上面的问题会及时更新的
- Scrum Meeting 13-20151221
任务安排 姓名 今日任务 明日任务 困难 董元财 无(数据库) 网络连接框架优化 无 胡亚坤 无(数据库) 优化商品搜索 无 刘猛 无 无 马汉虎 无 无 赖彦俞 无 无 燃尽图 团队照片 暂无 代码 ...
- CCNA网络基础(一)
现在我们使用这一个图来讲解网络的基础知识: 现在只看右边那个图,也就是两台主机和一个交换机 PC1 的配置是 IP 172.16.20.2/24 网关没有配 ...
- ADO.NET 使用通用数据库操作类Database (SQL Server)
一.Web.config配置 <connectionStrings> <add name="constr_name" connectionString=" ...
- TypeError: coercing to Unicode: need string or buffer, ChatRoom found
在用django框架中遇到一个错误,是模型编写中出的错误 TypeError: coercing to Unicode: need string or buffer, ChatRoom found 解 ...
- Eclipse中全局搜索和更替
Eclipse全局搜索步骤 使用快捷键"ctrl+H"打开文件搜索对话框,选择"File Search"标签,在Containing text中输入你需要搜索 ...