CodeForces 103D Time to Raid Cowavans 分块+dp
先对b从小到大sort,判断b是不是比sqrt(n)大,是的话就直接暴力,不是的话就用dp维护一下
dp【i】表示以nb为等差,i为起点的答案,可以节省nb相同的情况
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; int n,m;
ll ans[N],dp[N],w[N];
struct node{
int a,b,id;
}c[N];
bool comp(node x,node y)
{
if(x.b!=y.b)return x.b<y.b;
return x.a<y.a;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%I64d",&w[i]);
scanf("%d",&m);
for(int i=;i<=m;i++)
{
scanf("%d%d",&c[i].a,&c[i].b);
c[i].id=i;
}
sort(c+,c+m+,comp);
int nb=;
for(int i=;i<=m;i++)
{
if(c[i].b>sqrt(0.5+n))
{
for(int j=c[i].a;j<=n;j+=c[i].b)
ans[c[i].id]+=w[j];
}
else
{
if(c[i].b!=nb||dp[c[i].a]==)
{
for(int j=n;j>=;j--)
{
if(j+c[i].b<=n)dp[j]=dp[j+c[i].b]+w[j];
else dp[j]=w[j];
}
}
ans[c[i].id]=dp[c[i].a];
nb=c[i].b;
}
}
for(int i=;i<=m;i++)
printf("%I64d\n",ans[i]);
return ;
}
/****************
5
3 2 4 5 6
8
4 2
3 1
3 5
3 4
3 5
5 5
4 4
5 3
****************/
CodeForces 103D Time to Raid Cowavans 分块+dp的更多相关文章
- CodeForces 103D Time to Raid Cowavans 询问分块
Time to Raid Cowavans 题意: 询问 下标满足 a + b * k 的和是多少. 题解: 将询问分块. 将b >= blo直接算出答案. 否则存下来. 存下来之后,对于每个b ...
- CodeForces - 103D Time to Raid Cowavans
Discription As you know, the most intelligent beings on the Earth are, of course, cows. This conclus ...
- Codeforces Beta Round #80 (Div. 1 Only) D. Time to Raid Cowavans 分块
D. Turtles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/103/problem/D ...
- D. Time to Raid Cowavans 分块暴力,感觉关键在dp
http://codeforces.com/contest/103/problem/D 对于b大于 sqrt(n)的,暴力处理的话,那么算出每个的复杂度是sqrt(n),就是把n分成了sqrt(n)段 ...
- Codeforces Beta Round #80 (Div. 1 Only) D. Time to Raid Cowavans 离线+分块
题目链接: http://codeforces.com/contest/103/problem/D D. Time to Raid Cowavans time limit per test:4 sec ...
- Codeforces Round #278 (Div. 1) D - Conveyor Belts 分块+dp
D - Conveyor Belts 思路:分块dp, 对于修改将对应的块再dp一次. #include<bits/stdc++.h> #define LL long long #defi ...
- CodeForces 103 D Time to Raid Cowavans
Time to Raid Cowavans 题意:一共有n头牛, 每头牛有一个重量,m次询问, 每次询问有a,b 求出 a,a+b,a+2b的牛的重量和. 题解:对于m次询问,b>sqrt(n) ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- Codeforces103D - Time to Raid Cowavans
Portal Description 给出长度为\(n(n\leq3\times10^5)\)的序列\(\{a_n\}\),进行\(q(q\leq3\times10^5)\)次询问:给出\(x,y\) ...
随机推荐
- C 和 C++ 的标准库分别有自己的 locale 操作方法,C 标准库的 locale 设定函数是 setlocale(),而 C++ 标准库有 locale 类和流对象的 imbue() 方法(gcc使用zh_CN.GBK,或者zh_CN.UTF-8,VC++使用Chinese_People's Republic of China.936或者65001.)
转自:http://zyxhome.org/wp/cc-prog-lang/c-stdlib-setlocale-usage-note/ [在此向原文作者说声谢谢!若有读者看到文章转载时请写该转载地址 ...
- tomcat部署jenkins启动报错:insufficient free space available after evicting expired cache entries-consider increasing the maximum size of the cache.
在tomcat里面部署jenkins,启动tomcat,在jenkins上操作不久之后,jenkins就挂掉了,查看tomcat控制台,报内存溢出信息: 解决该问题方法,修改tomcat/bin目录下 ...
- Java基础—类和对象
基本概念 对象:对象是类的一个实例,有状态和行为.例如,一条狗是一个对象,它的状态有:颜色.名字.品种:行为有:摇尾巴.叫.吃等. 类:类是具有相同属性和方法的一组对象的集合,它为属于该类的所有对象提 ...
- cqlsh script
1.time类型 cqlsh> COPY my_keyspace.typetest from STDIN;Using 1 child processes Starting copy of my_ ...
- junit在idea中的使用(2)--实践篇
目录:(1)普通java项目(2)在web项目中 (1)普通java项目 直接在代码中写上 @Before @Test即可,想执行main方法,直接右击main,选择run as import org ...
- pyhton3 time模块
来自:菜鸟教程 Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间. 时间间隔是以秒为 ...
- 【HackerRank】Find the Median(Partition找到数组中位数)
In the Quicksort challenges, you sorted an entire array. Sometimes, you just need specific informati ...
- webstrom突然不显示文件夹
不知道什么原因,webstrom突然不显示文件夹了,弄得几乎都不能用了... 百度搜了一下解决方案: 问题原因:webstorm自动生成的配置文件 .idea/modules.xml损坏(就是我一开始 ...
- JSON.parse和JSON.stringify的作用
//JSON.parse将字符串格式json转化为json对象 var str='{"name":"lingling","age":&quo ...
- One 的使用(1)
方法一:使用命令提示符 第一步:打开d盘 C:Users\dcf>d; 第二步:打开工作空间 D:\>Cd workspace 第三步:打开the one D:\workspace& ...