题目链接

BZOJ2217

题解

如果只判定存不存在方案的话,我倒是想到可以将\(2\)拆成两个\(1\),其中一个不能作为区间开头,线段树优化计算补集方案数

但是一看这道题要输出方案啊,,,

怎么办?

考虑如果凑不出\(x\),那一定可以凑出\(x + 1\)

我们就找到前缀和为\(x\)的位置,如果没有,就找\(x + 1\)

前缀和为\(x\)当然就得到答案啦

前缀和为\(x + 1\),我们考虑将区间整体右移,如果左端点出去和右端点进来的数相同,区间值不变,如果不同,那我们就可以通过调整使得区间的值减少\(1\)

贪心预处理一下答案即可

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
#define lbt(x) (x & -x)
using namespace std;
const int maxn = 1000005,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int ansl[maxn << 1],ansr[maxn << 1],sum[maxn],R[maxn],n,m,N;
char S[maxn];
int main(){
n = read(); m = read();
scanf("%s",S + 1);
REP(i,n) sum[i] = sum[i - 1] + (S[i] == 'W' ? 1 : 2);
for (int i = n - 1; ~i; i--){
if (sum[i + 1] - sum[i] == 2) R[i] = R[i + 1] + 1;
else R[i] = 0;
}
//REP(i,n + 1) printf("R[%d] = %d\n",i - 1,R[i - 1]);
int pos = 1;
for (int i = 1; i <= sum[n]; i++){
while (sum[pos] != i && sum[pos] != i + 1) pos++;
if (sum[pos] == i) ansl[i] = 1,ansr[i] = pos;
else {
if (R[0] == R[pos]){
ansl[i] = R[0] + 2,ansr[i] = pos + R[pos];
}
else if (R[0] < R[pos]) ansl[i] = R[0] + 2,ansr[i] = pos + R[0];
else if (pos + R[pos] < n) ansl[i] = R[pos] + 2,ansr[i] = pos + R[pos] + 1;
}
}
int len;
while (m--){
len = read();
if (!ansl[len] || ansl[len] > ansr[len]) puts("NIE");
else printf("%d %d\n",ansl[len],ansr[len]);
}
return 0;
}

BZOJ2217 [Poi2011]Lollipop 【贪心】的更多相关文章

  1. BZOJ2217 : [Poi2011]Lollipop

    若能得到一个和为t的区间,那么至少去掉两端点中任意一个后必定能得到和为t-2的区间. 所以只需要分别找到和最大的和为奇数和偶数的区间,然后$O(n)$完成构造即可. #include<cstdi ...

  2. BZOJ2217 Poi2011 Lollipop 【思维+模拟】

    Description 有一个长度为n的序列a1,a2,...,an.其中ai要么是1("W"),要么是2("T"). 现在有m个询问,每个询问是询问有没有一个 ...

  3. 【BZOJ2217】[Poi2011]Lollipop 乱搞

    [BZOJ2217][Poi2011]Lollipop Description 有一个长度为n的序列a1,a2,...,an.其中ai要么是1("W"),要么是2("T& ...

  4. bzoj 2217 [Poi2011]Lollipop 乱搞 贪心

    2217: [Poi2011]Lollipop Time Limit: 15 Sec  Memory Limit: 64 MBSec  Special JudgeSubmit: 383  Solved ...

  5. BZOJ_2529_[Poi2011]Sticks_贪心

    BZOJ_2529_[Poi2011]Sticks_贪心 Description Little Johnny was given a birthday present by his grandpare ...

  6. [bzoj2529][Poi2011]Sticks_贪心

    Sticks bzoj-2529 Poi-2011 题目大意:给你n根木棒,每种木棒有长度和颜色,颜色共有k种,求满足条件的3根木棒使得这3根木棒颜色互不相同且可以围成三角形. 注释:$1\le n ...

  7. 【bzoj2529】[Poi2011]Sticks 贪心

    题目描述 给出若干木棍,每根木棍有特定的颜色和长度.问能否找到三条颜色不同的木棍构成一个三角形.(注意这里所说的三角形面积要严格大于0) 输入 第一行给出一个整数k(3<=k<=50),表 ...

  8. Luogu3514 POI2011 Lollipop 递推、构造

    题目传送门:https://www.luogu.org/problemnew/show/P3514 题意:给出一个只有$1$和$2$的长度为$N$的数列,$M$次询问是否存在一段连续子区间和为$K$. ...

  9. BZOJ 2217: [Poi2011]Lollipop

    若sum可行 sum-2一定可行 序列和为ans 找出和ans奇偶性不同的最大的ans,即最靠左或最靠右的1的位置 更新答案 有spj #include<cstdio> using nam ...

随机推荐

  1. windows c++如何使窗口动态改变位置

    在windows软件中,经常会碰到一个功能:鼠标hover在某个地方时会出现窗口,有时候这个窗口的位置是会动态调整的. 熟悉使用windows API,理解windows中虚拟坐标.工作区坐标.屏幕坐 ...

  2. 在IIS中部署Asp.Net网站

    在IIS中部署Asp.Net网站 1.添加IIS或者删除IIS,在控制面板=>程序和功能=>打开或关闭功能 启动iis,右键计算机=>管理=>服务和应用程序=>Inter ...

  3. Overlay 网络

  4. UVa 10055

    说一下犯错的地方: 1)没有注意数据范围,题目中是The input numbers are not greater than balabalabala. 而这个32位的int类型恰好装不下2^32, ...

  5. DockerCon2017前瞻 - Docker企业版体验

    DockerCon 2017将于四月17号在美国Austin召开.在去年DockerCon上,Docker公司一系列的发布吹响了进军企业市场的号角.今天,容器技术已经愈发成熟,被越来越多的企业所关注和 ...

  6. jumpserver安装与部署

    1.简介 Jumpserver 是一款由Python编写开源的跳板机(堡垒机)系统,实现了跳板机应有的功能.基于ssh协议来管理,客户端无需安装agent.特点:  完全开源,GPL授权   Pyth ...

  7. Java多线程编程之不可变对象模式

           在多线程环境中,为了保证共享数据的一致性,往往需要对共享数据的使用进行加锁,但是加锁操作本身就会带来一定的开销,这里可以使用将共享数据使用不可变对象进行封装,从而避免加锁操作. 1. 模 ...

  8. SpringCloud学习:Eureka、Ribbon和Feign

    Talk is cheap,show me the code , 书上得来终觉浅,绝知此事要躬行.在自己真正实现的过程中,会遇到很多莫名其妙的问题,而正是在解决这些问题的过程中,你会发现自己之前思维的 ...

  9. centos 切换用户显示bash-4.2$,不显示用户名路径的问题

    原文链接: http://blog.csdn.net/testcs_dn/article/details/70482468

  10. oracle安装出错/runInstaller

    http://blog.csdn.net/yabingshi_tech/article/details/48313955 http://www.cnblogs.com/lihaozy/archive/ ...