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 考场上想到要 ...
随机推荐
- HDU5785 manacher+差分数组
用manacher算法O(n)求出所有的回文半径.有了回文半径后,就可以求出L[i]表示以i结尾的回文串的起始位置的和R[i]表示以i起始的回文串的结尾位置的和,然后就可以求出答案了,这里要注意奇偶长 ...
- 初学CDQ分治-NEU1702
关于CDQ分治,首先需要明白分治的复杂度. T(n) = 2T(n/2)+O(kn), T(n) = O(knlogn) T(n) = 2T(n/2)+O(knlogn), T(n) = O(knlo ...
- 了解OpenStack
OpenStack是一个由NASA(美国国家航空航天局)和Rackspace合作研发并发起的,以Apache许可证授权的自由软件和开放源代码项目. OpenStack支持几乎所有类型的云环境,项目目标 ...
- 【ufldl tutorial】Convolution and Pooling
卷积的实现: 对于每幅图像,每个filter,首先从W中取出对应的filter: filter = squeeze(W(:,:,filterNum)); 接下来startercode里面将filter ...
- 有关使用Maven常见问题总结(Eclipse中使用Maven、Maven项目部署到tomcat等问题)
http://blog.csdn.net/sunitjy/article/details/42709311 ********************************************** ...
- 多线程调用HttpWebRequest并发连接限制
.net 的 HttpWebRequest 或者 WebClient 在多线程情况下存在并发连接限制,这个限制在桌面操作系统如 windows xp , windows 7 下默认是2,在服务器操作 ...
- html list <==> unformatted list
两者的区别很大不得不擦呢,任何两个东西的差别都很大,不得混淆.---一个有ul/ol的选择,一个没有
- bottomNavigationBar 底部导航tab MD
1.先上图: 此底部Tab完全可以满足日常的开发 2.使用: 很简单,使用Gradle构建:compile ‘com.ashokvarma.android:bottom-navigation-bar: ...
- centos7 安装mariaDB 以及 phpmyadmin的安装
centos7 安装mariaDB 以及 phpmyadmin的安装 一:安装mariadb, mariadb 是 mysql 的一个分支,基本和mysql一样的 1. yum -y install ...
- VC++ 中使用 std::string 转换字符串编码
目录 第1章说明 1 1.1 代码 1 1.2 使用 4 第1章说明 VC++中宽窄字符串的相互转换比较麻烦,借助std::string能大大减少代码量. 1.1 代码 函数声明如下 ...