[POI2011]LIZ-Lollipop(前缀和)
Posted cjgjh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[POI2011]LIZ-Lollipop(前缀和)相关的知识,希望对你有一定的参考价值。
原题
Solution
我是不会告诉你我一开始写了一个二分答案+rand的算法的。。。
这样子你可以获得80分的好成绩!!!
#pragma GCC diagnostic error "-std=c++11"
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<queue>
#define ll long long
#define file(a) freopen(a".in","r",stdin)//;freopen(a".out","w",stdout)
using namespace std;
inline int gi(){
int sum=0,f=1;char ch=getchar();
while(ch>'9' || ch<'0'){if(ch=='-')f=-f;ch=getchar();}
while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return f*sum;
}
inline ll gl(){
ll sum=0,f=1;char ch=getchar();
while(ch>'9' || ch<'0'){if(ch=='-')f=-f;ch=getchar();}
while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return f*sum;
}
int a[2000010],sum[2000010];
struct node{
int id,val;
bool operator<(const node b)const{
return val<b.val;
}
}d[2000010];
int main(){
srand(time(NULL));
#ifndef ONLINE_JUDGE
file("example");
#endif
int i,j,n,m,k;
n=gi();m=gi();
for(i=1;i<=n;i++){
char ch=getchar();
a[i]=ch=='W'?1:2;
}
for(i=1;i<=n;i++)sum[i]=sum[i-1]+a[i];
for(i=1;i<=n;i++){
d[i]=(node){i,rand()};
}
sort(d+1,d+n+1);
while(m--){
k=gi();int flag=0;
for(int x=1;x<=n;x++){
i=d[x].id;
int l=i,r=n,ans=0;
while(l<=r){
int mid=(l+r)>>1;
if(sum[mid]-sum[i-1]>k)r=mid-1;
else if(sum[mid]-sum[i-1]<k)l=mid+1;
else{ans=mid;break;}
}
if(ans){
printf("%d %d
",i,ans);flag=1;
break;
}
}
if(!flag)puts("NIE");
}
return 0;
}
这是一道比较有思维难度的题目,我们考虑一下如果一段区间有1,那么显然都可以完成对吧。
所以按照这个直接写出每一个和的左端点和右端点然后(Theta(1))的复杂度直接输出就好了。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<queue>
#define ll long long
#define file(a) freopen(a".in","r",stdin)//;freopen(a".out","w",stdout)
using namespace std;
inline int gi(){
int sum=0,f=1;char ch=getchar();
while(ch>'9' || ch<'0'){if(ch=='-')f=-f;ch=getchar();}
while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return f*sum;
}
inline ll gl(){
ll sum=0,f=1;char ch=getchar();
while(ch>'9' || ch<'0'){if(ch=='-')f=-f;ch=getchar();}
while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return f*sum;
}
int a[2000010],sum[2000010],Sum,l[2000010],r[2000010];
int main(){
#ifndef ONLINE_JUDGE
file("example");
#endif
int i,j,n,m,k;
n=gi();m=gi();
for(i=1;i<=n;i++){
char ch=getchar();
if(ch=='T')a[i]=2;
else a[i]=1;
}
for(i=n;i;i--)sum[i]=a[i]==2?(sum[i+1]+1):0;
int Sum=0;
for(i=1;i<=n;i++){
Sum+=a[i];
l[Sum]=1;r[Sum]=i;
if(a[i]==2)
if(sum[i]>sum[1])l[Sum-1]=sum[1]+2,r[Sum-1]=sum[1]+i;
else if(sum[i]!=n-i+1)l[Sum-1]=sum[i]+1,r[Sum-1]=sum[i]+i;
}
while(m--){
k=gi();
if(l[k])printf("%d %d
",l[k],r[k]);
else puts("NIE");
}
return 0;
}
以上是关于[POI2011]LIZ-Lollipop(前缀和)的主要内容,如果未能解决你的问题,请参考以下文章
Luogu P3527 [POI2011]MET-Meteors 整体二分
[POI2009]KON-Ticket Inspector(二维前缀和+DP)
[POI2006][luogu3435] OKR-Periods of Words [kmp+next数组]
P3435 [POI2006]OKR-Periods of Words