cf C. Inna and Candy Boxes
题意:给你一个长度为n的只含有1和0的字符串,w个询问,每次询问输入l,r;在[l,r]中在l+k-1、l+2*k-1、......r的位置都必须为1,如果不为1的,变成1,记为一次操作,其它的地方的都必须为0,不为0的地方要变成1,也记为一次操作,最后问在区间[l,r]最少几次操作。
思路:可以用树状数组记录在某个地方的右方有多少个1,然后在 预处理出从1,2,..k的为开头的在i+c*k-1的位置上前面有多少个1,最后的答案是拿走的1+添加的1,拿走的1的个数等于现有的1的个数-位置正确的1的个数。 添加的1的个数等于需要添加的个数的总数-位置正确的1的个数。
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 100010
using namespace std; int n,k,w;
char str[maxn];
int a[maxn];
int c[maxn];
int dp[maxn];
int sum[maxn]; int lowbit(int x)
{
return x&-x;
} void insert(int x,int d)
{
while(x<maxn)
{
c[x]+=d;
x+=lowbit(x);
}
} int Getsum(int x)
{
int ans=;
while(x>)
{
ans+=c[x];
x-=lowbit(x);
}
return ans;
} int main()
{
while(scanf("%d%d%d",&n,&k,&w)!=EOF)
{
scanf("%s",str+);
memset(sum,,sizeof(sum));
memset(a,,sizeof(a));
memset(c,,sizeof(c));
for(int i=; i<=n; i++)
{
if(str[i]=='')
{
insert(i,);
}
}
for(int i=; i<=k; i++)
{
int cnt=;
for(int c=; i+c*k-<=n; c++)
{
cnt+=(str[i+c*k-]-'');
dp[i+c*k-]=cnt;
}
}
for(int i=; i<=w; i++)
{
int l,r;
int ans=;
scanf("%d%d",&l,&r);
int num=(r-l+)/k;
ans+=Getsum(r)-Getsum(l-);
ans-=dp[r]-dp[l-];
ans+=num;
ans-=dp[r]-dp[l-];
printf("%d\n",ans);
}
}
return ;
}
cf C. Inna and Candy Boxes的更多相关文章
- cf B Inna and Candy Boxes
题意:输入n,然后输入n个数ai,再输入n个数bi,如果在1-ai中能找到两个数x,y,x和y可以相等,如果x+y=bi,答案加上x*y,否则减去1,让结果尽可能大,输出结果. #include &l ...
- Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s
C. Inna and Candy Boxes Inna loves sweets very much. She has n closed present boxes lines up in a ...
- codeforces 390C Inna and Candy Boxes
这个题目看似不是很好下手,不过很容易发现每次询问的时候总是会问到第r个盒子是否有糖果: 这样的话就很好办事了: 维护两个数组: 一个sum数组:累加和: 一个in数组:如果i位是1的话,in[i]=i ...
- Brute Force - B. Candy Boxes ( Codeforces Round #278 (Div. 2)
B. Candy Boxes Problem's Link: http://codeforces.com/contest/488/problem/B Mean: T题目意思很简单,不解释. ana ...
- Codeforces 488B - Candy Boxes
B. Candy Boxes 题目链接:http://codeforces.com/problemset/problem/488/B time limit per test 1 second memo ...
- Codeforces Round #278 (Div. 2) B. Candy Boxes [brute force+constructive algorithms]
哎,最近弱爆了,,,不过这题还是不错滴~~ 要考虑完整各种情况 8795058 2014-11-22 06:52:58 njczy2010 B - Ca ...
- cf A. Inna and Pink Pony(思维题)
题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...
- cf C. Inna and Dima
http://codeforces.com/contest/374/problem/C 记忆化搜索,题意:求按照要求可以记过名字多少次,如果次数为无穷大,输出Poor Inna!,如果不经过一次输出P ...
- cf B. Inna and Nine
http://codeforces.com/contest/374/problem/B #include <cstdio> #include <cstring> #includ ...
随机推荐
- Datagridview 实现二维表头和行合并【转载】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; u ...
- 利用 SysRq 键排除和诊断系统故障
说白了,SysRq手动触发而不用命令, /proc/sysrq-trigger 这个是通过命令接口 实验:LINUX窗口下 ,CTRL+ALT+F1,切换到TTY1,在文本模式下,按下 ALT+Sys ...
- Driving the Activity Lifecycle
Before Robolectric 2.2, most tests created Activities by calling constructors directly, (new MyActiv ...
- MVVM之View和ViewModel的关联
概要: 将所有的VM在加载到Application的Static Resource中,然后在View中用标签指定. 实现: 1)采用特性指定要添加到StaticResource中的对象 public ...
- vsftpd的主配置文件详解
anonymous_enable=YES 允许匿名用户登录#local_enable=YES 允许本地用户登录#write_enable=YES 允许写权限#local_umask=022 ##ano ...
- Apache MINA 框架之默认session管理类实现
DefaultSocketSessionConfig 类 extends AbstractSocketSessionConfig extends AbstractIoSessionConfig imp ...
- ASP.NET Boilerplate 工作单元
从上往下说起,框架使用castle拦截器,拦截实现了IApplication.IRepository接口的所有方法,和使用了UnitOfWork 特性的方法,代码如下 internal class U ...
- ASP.NET图片验证码
1. 新建一个Validate.aspx,然后在Validate.aspx.cs编写代码: using System; using System.Collections; using System.C ...
- jsp页面表单的遍历要怎么写
1.传统的方式使用request.getAttribute(“list”);获取表单的值, 2.也可以用struts2提供的标签进行遍历 备注 // 传统的接受参数方法 // String sfz=t ...
- vs2013下的WCFRest 模板开发WCF
在vs2013下使用wcfRestservice40 是安装不成功的,尝试多遍,都是这样.查看以前vs2012做的wcfrest,经过调教,终于在vs2013下也可以了! 1.新建wcf服务应用程序 ...