BZOJ2217 [Poi2011]Lollipop 【贪心】
题目链接
题解
如果只判定存不存在方案的话,我倒是想到可以将\(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 【贪心】的更多相关文章
- BZOJ2217 : [Poi2011]Lollipop
若能得到一个和为t的区间,那么至少去掉两端点中任意一个后必定能得到和为t-2的区间. 所以只需要分别找到和最大的和为奇数和偶数的区间,然后$O(n)$完成构造即可. #include<cstdi ...
- BZOJ2217 Poi2011 Lollipop 【思维+模拟】
Description 有一个长度为n的序列a1,a2,...,an.其中ai要么是1("W"),要么是2("T"). 现在有m个询问,每个询问是询问有没有一个 ...
- 【BZOJ2217】[Poi2011]Lollipop 乱搞
[BZOJ2217][Poi2011]Lollipop Description 有一个长度为n的序列a1,a2,...,an.其中ai要么是1("W"),要么是2("T& ...
- bzoj 2217 [Poi2011]Lollipop 乱搞 贪心
2217: [Poi2011]Lollipop Time Limit: 15 Sec Memory Limit: 64 MBSec Special JudgeSubmit: 383 Solved ...
- BZOJ_2529_[Poi2011]Sticks_贪心
BZOJ_2529_[Poi2011]Sticks_贪心 Description Little Johnny was given a birthday present by his grandpare ...
- [bzoj2529][Poi2011]Sticks_贪心
Sticks bzoj-2529 Poi-2011 题目大意:给你n根木棒,每种木棒有长度和颜色,颜色共有k种,求满足条件的3根木棒使得这3根木棒颜色互不相同且可以围成三角形. 注释:$1\le n ...
- 【bzoj2529】[Poi2011]Sticks 贪心
题目描述 给出若干木棍,每根木棍有特定的颜色和长度.问能否找到三条颜色不同的木棍构成一个三角形.(注意这里所说的三角形面积要严格大于0) 输入 第一行给出一个整数k(3<=k<=50),表 ...
- Luogu3514 POI2011 Lollipop 递推、构造
题目传送门:https://www.luogu.org/problemnew/show/P3514 题意:给出一个只有$1$和$2$的长度为$N$的数列,$M$次询问是否存在一段连续子区间和为$K$. ...
- BZOJ 2217: [Poi2011]Lollipop
若sum可行 sum-2一定可行 序列和为ans 找出和ans奇偶性不同的最大的ans,即最靠左或最靠右的1的位置 更新答案 有spj #include<cstdio> using nam ...
随机推荐
- javascript this(上)
javascript的this指向的是一个函数运行时动态绑定对象. this的4种常见的指向: 作为对象的方法调用 var obj={ name:"姚小白", getName:fu ...
- pytorch中的Linear Layer(线性层)
LINEAR LAYERS Linear Examples: >>> m = nn.Linear(20, 30) >>> input = torch.randn(1 ...
- RAID中条带的概念
raid把数据分成条带,一个条带横跨所有数据磁盘,每个磁盘上存储条带的一部分,称为sagment,也称为条带深度.一个条带包含的扇区或块的个数,称为条带长度. raid向操作系统提供的是卷,是连续的扇 ...
- python3去除字符串中括号及括号里面的内容
a = """ <option value="search-alias=arts-crafts-intl-ship">Arts & ...
- trustbox文件破解
常见的破解方式,是要还原内容的二进制文件,删除加密壳部分的对应二进制数值,然后把剩下的内容保存下来,就实现了破解的任务. 淘宝破解链接:https://item.taobao.com/item.ht ...
- 关于手机端h5上传图片配合exif.min.js,processImg.js的使用
首先这里有个new FileReader()的概念,这是h5新增的,用来把文件读入内存,并且读取文件中的数据.FileReader接口提供了一个异步API,使用该API可以在浏览器主线程中异步访问文件 ...
- Notes of Daily Scrum Meeting(11.15)
Notes of Daily Scrum Meeting(11.15) 今天周六我们的主要工作是把这周落下的一些工作补回来,这是写程序的最后阶段,准备进入测试阶段了,所以之前的工作 要补齐,今天大家的 ...
- Java Head First & 多态
package com.cwcec.tag; class Fruit { } class Apple extends Fruit{} class Animal { public Fruit eat(F ...
- charles抓取移动端app数据
pc端为mac 移动端为android pc端 1.下载charles并安装 安利一个超好的良心网站(好多好用的软件都可以在上面找到,并且免费): http://xclient.info/search ...
- #Leetcode# 817. Linked List Components
https://leetcode.com/problems/linked-list-components/ We are given head, the head node of a linked l ...