2014北邮新生归来赛解题报告d-e
D:
399. Who Is Joyful
KB
题目描述
There are several little buddies standing in a line. We say someone is a joyful little buddy, if there exists a little buddy whose height is smaller than him in the front of him and there exists a little buddy whose
height is bigger than him in the back of him. Now there are N little buddies, numbered from 1 to N. Their heights h are known. Give you two numbers A and B. Query that in the closed interval whose two ends are A and B, if the joyfulness of the little buddies
in the interval have nothing to do with the little buddies out of the interval, how many little buddies are joyful in total in the interval.
输入格式
In the first line there is an integer T (T<=50), indicates the number of test cases. For each case, the first line contains one integer N (1<=N<=1e5). The second line contains N integers hi (1<=hi<=1e9, 1<=i<=N),
indicate the heights of the little buddies from the beginning to the end. The third line contains an integer Q (1<=Q<=1e5), indicates the number of queries. And in the following Q lines, each line contains two integers A and B (1<=A, B<=N).
输出格式
For each case, output the case number as shown, and then print the answers to the queries, every query in a single line.
输入样例
2
6
5 2 3 1 4 5
1
2 5
8
1 4 5 5 1 1 3 2
2
3 6
1 3
输出样例
Case 1:
1
Case 2:
0
1
现在这题先把后继节点求出来,前面节点求出来,对每个点u求满足后继节点是u的所有点,树状数组的应用是对每个满足前驱节点的值进行求和时用到的直接算(n^2)有可能超时,(借鉴了similewsw的代码,不知道是集训队哪位同学,喵哈哈)
这里:
<span style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;"> while(j<ques[i].to){//后继节点满足条件//这是对的</span>
<span style="font-family:Helvetica Neue, Helvetica, Arial, sans-serif;"> j++;
<span style="white-space:pre"> </span>//....</span>
<span style="font-family:Helvetica Neue, Helvetica, Arial, sans-serif;"> while(j<=ques[i].to){//后继节点满足条件//这是错的</span>
<span style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;"><span style="white-space:pre"> </span>//.......</span>
<span style="font-family:Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="white-space:pre"> </span>j++;</span>
原因是:不能加入区间from节点,要先得到区间内非to节点,然后+1得到后面那个点,这些才是当前可标示的点
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
int n,m;
#define MAXN 100011
int hi[MAXN];
int pre[MAXN];
int last[MAXN];
int sum[MAXN];
int heap[MAXN];//用于求解前驱节点/后继节点
vector<int > g[MAXN];
struct query{
int from,to,index;
};
int ans[MAXN];
query ques[MAXN];
bool cmp(query q1, query q2){
return (q1.to<q2.to||(q1.to==q2.to&&q1.from<q2.from));
}
int lowbit(int i){
return i&(-i);
}
void update(int a){//默认增量为1
// printf("update %d\n",a);
if(a==0)return ;
while(a<=n){
sum[a]++;
a+=lowbit(a);
}
}
int getSum(int i){
if(i==0)return 0;
int ans2=0;
while(i!=0){
ans2+=sum[i];
i-=lowbit(i);
}
return ans2;
}
int main(){
int ct;
scanf("%d",&ct);
for(int ci=1;ci<=ct;ci++){
memset(sum,0,sizeof(sum));
memset(pre,0,sizeof(pre));
memset(last,0,sizeof(last)); printf("Case %d:\n",ci);
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",hi+i);//o(n) int heaplen=0;//用于记录堆栈深度
for(int i=1;i<=n;i++){//pre
while(heaplen>0&&hi[heap[heaplen-1]]>=hi[i])heaplen--;
if(heaplen>0) pre[i]=heap[heaplen-1];//o(cn)
else pre[i]=0;
heap[heaplen++]=i;
} heaplen=0;//用于记录堆栈深度
for(int i=n;i>=1;i--){//last
while(heaplen>0&&hi[heap[heaplen-1]]<=hi[i])heaplen--;
if(heaplen>0) last[i]=heap[heaplen-1];//o(cn)
else last[i]=0;
heap[heaplen++]=i;
} for(int i=0;i<=n;i++)g[i].clear(); for(int i=1;i<=n;i++){
g[last[i]].push_back(i);//对每个点u,筛选出所有比u小且在u前的数
}
// for(int i=1;i<=n;i++){
// for(int k=0;k<g[i].size();k++){
// printf("g[%d][%d]:%d\n",i,k,g[i][k]);
// }
// }
scanf("%d ",&m);
for(int i=0;i<m;i++){
int a,b;
scanf("%d %d",&a,&b);//深坑,a,b大小相对不一定
ques[i].from=min(a,b);
ques[i].to=max(a,b);
ques[i].index=i;
}
sort(ques,ques+m,cmp); // printf("Aftersort/n");
// for(int i=0;i<m;i++){
// printf("ques [%d]: from :%d to :%d index :%d\n",i,ques[i].from,ques[i].to,ques[i].index);
// }
// printf("\n"); for(int i=0, j=1;i<m&&j<=n;i++){
// printf("ques %d:\n",i);
while(j<ques[i].to){//后继节点满足条件
j++;
int len=g[j].size();
// printf("len g[%d]:%d\n",j,len);
for(int k=0;k<len;k++){
// printf("pre g[%d][%d]:%d\n",j,k,pre[g[j][k]]);
update(pre[g[j][k]]);
}
}
// printf("getSum[quesfrom-1]:%d\n",getSum(ques[i].from-1));
// printf("getSum[questo]:%d\n",getSum(ques[i].to));
ans[ques[i].index]=getSum(ques[i].to)-getSum(ques[i].from-1);
} for(int i=0;i<m;i++){
printf("%d\n",ans[i]);
}
}
return 0;
}
E:
400. 最小距离查询
KB
题目描述
给定一个由小写字母a到z组成的字符串S,其中第i个字符为S[i](下标从0开始)。你需要完成下面两个操作: INSERT c 其中c是一个待输入的字符。你需要在字符串的末尾添加这个字符。保证输入的字符同样是a到z之间的一个小写字母。 QUERY x 其中x是一个输入的整数下标。对于这个询问,你需要回答在S当中和S[x]相等且与x最近的距离。输入保证x在当前字符串中合法。 例如S = "abcaba",如果我们操作: INSERT
a 则在S的末端加一个字符a,S变成"abcabaa"。 接下来操作 QUERY 0 由于S[0] = a,在S中出现的离他最近的a在下标为3的位置上,距离为3 - 0 = 3。因此应当输出3。 接下来,如果 QUERY 4 S[4] = b,S中离它最近的b出现在下标为1处,距离为4 - 1 = 3。同样应当输出3。 给定初始字符串S和若干操作,对于每个QUERY,你需要求出相应的距离。
输入格式
输入的第一行是一个正整数T(T≤20),表示测试数据的组数。
每组输入数据的第一行是一个初始串S。第二行是一个正整数m(1≤m≤100000),表示总共操作的数量。接下来m行,每行表示一个操作。操作的格式如上所述。
数据保证在任何情况下,S的长度不会超过100000。 OutputFormat 对于每个QUERY,输出所求的最小距离。如果S中其它位置都不存在和它相同的字符,输出-1。
输出格式
对于每个QUERY,输出所求的最小距离。如果S中其它位置都不存在和它相同的字符,输出-1。
输入样例
2
axb
3
INSERT a
QUERY 0
QUERY 1
explore
3
INSERT r
QUERY 7
QUERY 1
输出样例
3
-1
2
-1
思路:就这么一不小心做出来了
/*
USER_ID: test#xuesu
PROBLEM: 400
SUBMISSION_TIME: 2014-07-12 15:08:38
*/
#include <cstdio>
#include <cstring> using namespace std;
int c[26][100000];
int s[100000];
int len[100000];
int ps;
int p[26];
void insertchar(char ch){
int pc=ch-'a';
s[ps]=pc;
c[pc][p[pc]]=ps;
len[ps]=p[pc];
p[pc]++;
ps++;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
char ch;
ps=0;
memset(p,0,sizeof(p));
while((ch=getchar())>'z'||ch<'a'){}
insertchar(ch); while((ch=getchar())<='z'&&ch>='a'){
insertchar(ch);
}
char op[10];
int ct;
scanf("%d",&ct);
while(ct--){
scanf("%s",op);
if(strcmp(op,"INSERT")==0){
while((ch=getchar())>'z'||(ch<'a')){}
insertchar(ch);
}
else if(strcmp(op,"QUERY")==0){
int x;
scanf("%d",&x);
int pc=s[x];
if(p[pc]<=1)printf("-1\n");
else {
int len1=0x3ffffff;
int cc=len[x];
if(cc>0){
len1=x-c[pc][cc-1];
}
if(cc<p[pc]-1&&c[pc][cc+1]-x<len1){
len1=c[pc][cc+1]-x;
} printf("%d\n",len1);
}}
}
}
return 0;
}
while(j<ques[i].to){//后继节点满足条件
j++;
2014北邮新生归来赛解题报告d-e的更多相关文章
- 2014北邮新生归来赛解题报告a-c
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
- 北邮新生排位赛1解题报告d-e
话说cdsn要是前面插入源代码又什么都不放就会出现奇怪的源代码?不知道是哪个网页的 407. BLOCKS 时间限制 1000 ms 内存限制 65536 KB 题目描述 给定一个N∗M的矩阵,求问里 ...
- 北邮新生排位赛1解题报告a-c
<div class="page-header" style="padding-bottom: 9px; margin: 20px 0px 30px; border ...
- 北邮新生排位赛2解题报告a-c
A. 丁神去谷歌 2014新生暑假个人排位赛02 时间限制 1000 ms 内存限制 65536 KB 题目描述 丁神要去Google上班了,去之前丁神想再做一道水题,但时间不多了,所以他希望题目做起 ...
- 北邮新生排位赛2解题报告d-e
<> 427. 学姐逗学弟 时间限制 3000 ms 内存限制 131072 KB 题目描述 学弟们来了之后,学姐每天都非常高兴的和学弟一起玩耍.这一天,学姐想出了这样一个游戏,她画了一棵 ...
- 【百度之星2014~初赛(第二轮)解题报告】Chess
声明 笔者近期意外的发现 笔者的个人站点http://tiankonguse.com/ 的非常多文章被其他站点转载.可是转载时未声明文章来源或參考自 http://tiankonguse.com/ 站 ...
- 【百度之星2014~初赛(第二轮)解题报告】JZP Set
声明 笔者近期意外的发现 笔者的个人站点http://tiankonguse.com/ 的非常多文章被其他站点转载,可是转载时未声明文章来源或參考自 http://tiankonguse.com/ 站 ...
- ZROIDay4-比赛解题报告
ZROIDay4-比赛解题报告 扯闲话 感觉这个出题人的题做起来全都没感觉啊,今天又凉了,T1完全不知道什么意思,T2只会暴力,T3现在还不懂什么意思,真的太菜了 A 题意半天没搞懂爆零GG了,讲了一 ...
- ZROIDay3-比赛解题报告
ZROIDay3-比赛解题报告 瞎扯 从今天开始考试有点不在状态,可能是因为不太适应题目的原因,T1已经接近了思想但是没有想到状态转移,T2思考方向错误,T3不会打LCT,还是太菜了 A 考场上想到要 ...
随机推荐
- AFNetworking请求中含有中文时程序崩溃
出现error: Assertion failure in -[AFHTTPRequestSerializer requestWithMethod:URLString:parameters:error ...
- bootstrap学习笔记<七>(图标,图像)
图像 bootstrap为图像预加载提供了很简洁的样式.(CDN:http://placehold.it/140x140:) PS:该CDN链接后的140x140可以根据网站需要更换合适的尺寸.例如: ...
- for循环与for in循环
json是js里的一种数据格式.var obj={a:15,b:8,c:12} json数组对象 var arr=[15,8,12]; 数组alert(obj.a); ---15alert(obj[' ...
- 转!!!Mysql无法创建外键的原因
在Mysql中创建外键时,经常会遇到问题而失败,这是因为Mysql中还有很多细节需要我们去留意,我自己总结并查阅资料后列出了以下几种常见原因. 1. 两个字段的类型或者大小不严格匹配.例如,如果一个 ...
- css-高度自适应的问题(body高度问题)
css-高度自适应的问题 对象height:100%并不能直接产生效果,是因为跟其父对象有关. #center{ height:100%; } 上面的css样式是无效的,不会产生任何效果. 需要改写: ...
- INTERPRETER(解释器)
1 意图:给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子. 2 动机:如果一种特定类型的问题发生的频率足够高,那么可能就值得将该问题的各个实例表述为一个 ...
- 用RestTemplate碰到的问题
给请求加上头信息 Request request = new Request(); HttpHeaders requestHeaders = new HttpHeaders(); requestHea ...
- (转)ConcurrentHashMap解析
原文地址:http://www.ibm.com/developerworks/cn/java/java-lo-concurrenthashmap/ ConcurrentHashMap 的结构分析 为了 ...
- unity3d WorldComposer1 卫星地图生成地形
http://blog.csdn.net/myarrow/article/details/42709113 1. 简介 1.1 TerrainComposer(TC) 一个Unity扩展工具,可用于创 ...
- 32-HTML辅助方法
顾名思义,HTML辅助方法(HTML Helper)就是用来辅助产生HTML之用,在开发View的时候一定会面对许多HTML标签,处理这些HTML的工作非常繁琐,为了降低View的复杂度,可以使用HT ...