Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes ACM解题报告(预处理)
2015-01-25 16:57
337 查看
这题是预处理的题。
ans=(当前区间内的总糖果数-当前区间内放对位置的糖果数)+(需要放的糖果数-当前区间内放对位置的糖果数)
前者是需要拿走的,后者是需要放入的。
当前区间内的糖果数是需要枚举计算n+count*k-1(1<=n<=k)到n的区间内放对位置的糖果数的累计和。
ans=(当前区间内的总糖果数-当前区间内放对位置的糖果数)+(需要放的糖果数-当前区间内放对位置的糖果数)
前者是需要拿走的,后者是需要放入的。
当前区间内的糖果数是需要枚举计算n+count*k-1(1<=n<=k)到n的区间内放对位置的糖果数的累计和。
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; int a[100005]; bool b[100005]; int d[100005]; int main() { int n,k,w; char c; scanf("%d%d%d",&n,&k,&w); memset(b,0,sizeof(b)); getchar(); int num=0; a[0]=0; for(int i=1;i<=n;i++) { scanf("%c",&c); if(c=='1') { b[i]=true; num++; } a[i]=num; } memset(d,0,sizeof(d)); for(int i=1;i<=k;i++) { int tot=0; for(int count=1;i+count*k-1<=n;count++) { if(b[i+count*k-1]) { tot++; } d[i+count*k-1]=tot; } } //for(int i=1;i<=n;i++) printf("%d ",d[i]); while(w--) { int l,r; scanf("%d%d",&l,&r); int cnt=(r-l+1)/k; printf("%d\n",a[r]-a[l-1]+cnt-2*(d[r]-d[l-1])); } return 0; }
相关文章推荐
- 【ACM菜逼解题报告】Boxes in a Line
- 【Jason's_ACM_解题报告】Boxes in a Line
- Codeforces Round #287 (Div. 2) C. Guess Your Way Out! ACM解题报告(二叉树)
- Codeforces Round #229 (Div. 2)C - Inna and Candy Boxes心得
- 解题报告:Codeforces Round #433 (Div. 2) E. Boredom ( 离线处理+树状数组)
- 解题报告:Codeforces Round #433 (Div. 1) D. Michael and Charging Stations (DP)
- Codeforces Round #372 (Div. 1) B. Complete The Graph 解题报告
- hdu 4763 && 2013 ACM/ICPC 长春网络赛解题报告
- 【解题报告】Codeforces Round #402 (Div. 2)
- ACM/ICPC乌鲁木齐2017解题报告
- 【解题报告】Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)
- codeforces Round #269(div2) C解题报告
- acm-poj1050解题报告
- Codeforces Round #373 (Div. 2) A. Vitya in the Countryside 解题报告
- codeforces Round #271(div2) A解题报告
- 【Jason's_ACM_解题报告】Chinese Mahjong
- Codeforces Round #467 (Div. 2) 解题报告
- acm pku 2234 解题报告(取子问题)
- 华东交通大学2013ACM“双基”程序设计竞赛 解题报告
- codeforces Round #273(div2) D解题报告