HDU 5063 Operation the Sequence(仔细审题)
http://acm.hdu.edu.cn/showproblem.php?pid=5063
题目大意:
题目意思还是比较简单。所以就不多少了。注意这句话,对解题有帮助。
Type4: Q i query current value of a[i], this operator will have at most
50.
解题思路:
因为给定n和m都是100000,我们每一步都做具体的操作,时间将是O(n*m),肯定超时。最开始的时候
我怎么想都不知道怎么解决。以为是线段树。比赛完了以后,看了解题报告(http://bestcoder.hdu.edu.cn/)
我吓到了,原来是自己没有仔细分析题目的给的条件。Q i query current value of a[i], this operator will
have at most 50.询问Q i最多50次。
所以我们把每部的操作都记下来,每次Q i的时候,就反推回去。自己可以推推。
假设当前位置为x,我们要求操作之前的位置。
fun1:
n = (n + 1) / 2;
x = x <= n / 2 ? (2 * x - 1) : ((x - n) * 2);
fun2:
x = n + 1 - x;
fun3:
我们用flag记录平方次数。
这样处理的时间复杂度O(50*n)
AC代码:
#include<cstdio> #define MAXN 100000+10
#define MOD 1000000007 int op[MAXN], count; int fun_1(int x, int n){
n = (n + ) >> ;
if(x <= n){
return (x << ) - ;
}
return (x - n) << ;
} int fun_2(int x, int n){
return n + - x;
} void solve(int x, int n){
int flag = ;//记录平方次数 for(int i = count - ; i >= ; --i){//逆推回去 找出初始位置
if(op[i] == ){
x = fun_1(x, n);
}else if(op[i] == ){
x = fun_2(x, n);
}else{
flag++;
}
} __int64 num = x;
for( ; flag > ; --flag){
num = num * num % MOD;
}
printf("%I64d\n", num);
} int main(){
char str[];
int t, n, m, i, num; scanf("%d", &t);
while(t--){
scanf("%d%d", &n, &m);
for(count = i = ; i < m; ++i){ scanf("%s%d", str, &num);
if(str[] == 'O'){
op[count++] = num;//记录执行fun1、fun2、fun3
}else{
solve(num, n);
}
}
}
return ;
}
HDU 5063 Operation the Sequence(仔细审题)的更多相关文章
- HDU 5063 Operation the Sequence(暴力)
HDU 5063 Operation the Sequence 题目链接 把操作存下来.因为仅仅有50个操作,所以每次把操作逆回去执行一遍,就能求出在原来的数列中的位置.输出就可以 代码: #incl ...
- hdu 5063 Operation the Sequence(Bestcoder Round #13)
Operation the Sequence Time Limi ...
- hdu 5063 Operation the Sequence
http://acm.hdu.edu.cn/showproblem.php?pid=5063 思路:因为3查询最多50,所以可以在查询的时候逆操作找到原来的位置,然后再求查询的值. #include ...
- HDU 5063 Operation the Sequence(暴力 数学)
题目链接:pid=5063" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=5063 Prob ...
- HDOJ 5063 Operation the Sequence
注意到查询次数不超过50次,那么能够从查询位置逆回去操作,就能够发现它在最初序列的位置,再逆回去就可以求得当前查询的值,对于一组数据复杂度约为O(50*n). Operation the Sequen ...
- HDU 5783 Divide the Sequence (训练题002 B)
Description Alice has a sequence A, She wants to split A into as much as possible continuous subsequ ...
- HDU 5288——OO’s Sequence——————【技巧题】
OO’s Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- 【HDOJ】5063 Operation the Sequence
#include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 100005 #defin ...
- HDU - 5036 Operation the Sequence
Problem Description You have an array consisting of n integers: a1=1,a2=2,a3=3,-,an=n. Then give you ...
随机推荐
- 转-临界区对象TCriticalSection与TRTLCriticalSection的区别
TRTLCriticalSection是一个结构体,在windows单元中定义: 是InitializeCriticalSection, EnterCriticalSection, LeaveCrit ...
- 【转】oracle 中随机取一条记录的两种方法
oracle 中随机取一条记录的两种方法 V_COUNT INT:=0; V_NUM INT :=0; 1:TBL_MYTABLE 表中要有一个值连续且唯一的列FID BEGIN SELECT COU ...
- EXCEL处理大量数据的潜在风险
同事收到几份60几M的xls文件,电脑性能不够,发给我来处理. 处理发现有BUG.简单的vlookup,如果只是实验性的处理几个数据的话,发现没有问题,但批量对全部数据进行处理,就会出现#N/A的问题 ...
- 服务端的GET、POST请求
一.HttpClient方式,程序集 System.Net.Http.dll GET: HttpClient httpClient = new HttpClient(); string result ...
- js 爱恨情仇说 this
this 相信大家在写代码的时候都会遇到,可是怎么样才能用好this,估计这个还是有点困难的,虽然你有时候你会用到,但是他所在的具体的几个场景中所代表的是什么意思了?可能这个你就不是很清楚啊.这个就会 ...
- BZOJ4566: [Haoi2016]找相同字符
Description 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两 个子串中有一个位置不同. Input 两行,两个字符串s1,s2,长度分别 ...
- 转:ibatis的cacheModel
转:ibatis的cacheModel cachemodel是ibatis里面自带的缓存机制,正确的应用能很好提升我们系统的性能. 使用方法:在sqlmap的配置文件中加入 <cacheMode ...
- showModalDialog 刷新问题,在页面中跳转问题
showModalDialog 刷新问题 ,在页面中跳转问题 <html><head> <base target="_self" >< ...
- BZOJ3687 计算子集和的异或和
题不知道怎么不见了,bzoj上已经没了3687这题了 题意:给你一个n 然后输入n个数 求这n个数的所有子集的和的异或和 思路:用bitset记录某个数是否在子集和中出现,利用bitset对二进制位的 ...
- java并发编程(七)synchronized详解
Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码. 一.当两个并发线程访问同一个对象object中的这个synchronized( ...