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 ...
随机推荐
- CSS基础范例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Web服务架构
# Web服务架构 ### Web服务模型-- 服务提供者.服务请求者.服务注册中心,服务注册中心是一个可选的角色. 现在的Web服务不仅限于WSDL,还有RESTful. - 服务提供者.即Web服 ...
- 06-matplotlib-饼状图
import numpy as np import matplotlib.pyplot as plt ''' 饼状图显示一个数据系列中各项总和的比例: 饼状图中的数据点显示为整个饼状图的百分比: 如: ...
- 在虚拟机上搭建物理机可访问的web服务(IIS)
0x0 前言 安装webug4.0的时候突发奇想,想学下如何在虚拟机里搭建网站,然后让主机像访问互联网的网站一样访问虚拟机的网站,为以后渗透测试搭建环境做准备 0x1 虚拟机安装win2003[以防万 ...
- ipmitool命令详解
基础命令学习目录首页 原文链接:https://www.cnblogs.com/EricDing/p/8995263.html [root@localhost ~]# yum install -y i ...
- ftp部署及使用
常用软件安装及使用目录 http://www.jb51.net/article/106604.htm ftp部署 本篇文章主要介绍了CentOS7.0下安装FTP服务的方法,小编觉得挺不错的,现在 ...
- [java] jar file
查看 .jar 内的文件 jar tf jarfile.jar maven 项目中, java 读取目标文件 运行 mvn package 打包项目是, src/main/resources 下的文件 ...
- Java第一次试验
北京电子科技学院(BESTI) 实 验 报 告 课程:Java程序设计 班级:1352 姓名:朱国庆 学号:20135237 成绩: ...
- 【搜索】POJ-2718 全排列+暴力
一.题目 Description Given a number of distinct decimal digits, you can form one integer by choosing a n ...
- alphe4
队名:massivehard 组员1:(组长:晓辉) 今天完成了哪些任务: 服务器基本架设完毕 明日计划: 服务器与客户端对接 用户手写记录功能的完善 还剩下哪些计划: 用户手写记录功能 服务器与客户 ...