D. Time to Raid Cowavans 分块暴力,感觉关键在dp
http://codeforces.com/contest/103/problem/D
对于b大于 sqrt(n)的,暴力处理的话,那么算出每个的复杂度是sqrt(n),就是把n分成了sqrt(n)段,
其他的,b小于sqrt(n)的,那么不同 b值最多只有sqrt(n)个,但是询问可能达到q个。考虑到,如果b相同的话,放在一起,能不能记录一个结果呢?如果能,那么就最多是sqrt(n)个不同的询问。
用dp[pos]表示从pos这个位置开始,间隔为b的答案值。
那么需要从后面往前dp,每次转移dp[i] = dp[i + b] + a[i]即可
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
int magic;
int n;
const int maxn = 3e5 + ;
int a[maxn];
struct node {
int a, b;
int id;
bool operator < (const struct node &rhs) const {
return b < rhs.b;
}
}query[maxn];
LL ans[maxn];
LL dp[maxn];
void work() {
scanf("%d", &n);
magic = (int)sqrt(n * 1.0);
for (int i = ; i <= n; ++i) {
scanf("%d", &a[i]);
}
int q;
scanf("%d", &q);
for (int i = ; i <= q; ++i) {
scanf("%d%d", &query[i].a, &query[i].b);
query[i].id = i;
}
sort(query + , query + + q);
int last = -;
for (int i = ; i <= q; ++i) {
if (query[i].b >= magic) {
for (int j = query[i].a; j <= n; j += query[i].b) {
ans[query[i].id] += a[j];
}
} else {
if (query[i].b == last) {
ans[query[i].id] = dp[query[i].a];
} else {
// cout << query[i].a << " " << query[i].b << endl;
last = query[i].b;
for (int j = n; j >= ; --j) {
if (j + query[i].b > n) {
dp[j] = a[j];
} else {
dp[j] = dp[j + query[i].b] + a[j];
}
}
ans[query[i].id] = dp[query[i].a];
}
}
}
// for (int i = 1; i <= n; ++i) {
// printf("%I64d ", dp[i]);
// }
// cout << endl;
for (int i = ; i <= q; ++i) {
printf("%I64d\n", ans[i]);
}
return;
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
work();
return ;
}
D. Time to Raid Cowavans 分块暴力,感觉关键在dp的更多相关文章
- 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 ...
- CodeForces 103D Time to Raid Cowavans 分块+dp
先对b从小到大sort,判断b是不是比sqrt(n)大,是的话就直接暴力,不是的话就用dp维护一下 dp[i]表示以nb为等差,i为起点的答案,可以节省nb相同的情况 #include<bits ...
- 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 103D Time to Raid Cowavans 询问分块
Time to Raid Cowavans 题意: 询问 下标满足 a + b * k 的和是多少. 题解: 将询问分块. 将b >= blo直接算出答案. 否则存下来. 存下来之后,对于每个b ...
- Codeforces Beta Round #13 E. Holes 分块暴力
E. Holes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/13/problem/E Des ...
- Codeforces103D - Time to Raid Cowavans
Portal Description 给出长度为\(n(n\leq3\times10^5)\)的序列\(\{a_n\}\),进行\(q(q\leq3\times10^5)\)次询问:给出\(x,y\) ...
- Codeforces#86D Powerful array(分块暴力)
Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...
- 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 1290D - Coffee Varieties(分块暴力+完全图的链覆盖)
Easy version:Codeforces 题面传送门 & 洛谷题面传送门 Hard version:Codeforces 题面传送门 & 洛谷题面传送门 发现自己交互题烂得跟 s ...
随机推荐
- 问题5:如何快速找到多个字典中的公共键(key)
方法一:for in循环 from random import randint, sample a1 = {k; randint(1, 4) for k in 'abcdefg'} a2 = {k; ...
- nginx用cookie控制访问权限实现方法
自己的一个需求需要对a.b.com 下的 /c 这个目录下,cookie d=e 才能访问,如果不是,就重定向到f.html 下面看代码. 代码如下 复制代码 server{ serve ...
- Golang Channel用法简编
转自:http://tonybai.com/2014/09/29/a-channel-compendium-for-golang/ 在进入正式内容前,我这里先顺便转发一则消息,那就是Golang 1. ...
- 问题:request.Headers;结果:HttpWebRequest.Headers 属性
指定构成 HTTP 标头的名称/值对的集合. Headers 集合包含与请求关联的协议标头.下表列出了由系统或由属性或方法设置但未存储在 Headers 中的 HTTP 标头. 标头 设置方 Ac ...
- [原创] 新人分享--ORA-01012:not logged on的解决办法 [复制链接]
转自:http://f.dataguru.cn/thread-82530-1-1.html
- 项目一:项目第二天 Jquery ztree使用展示菜单数据 2、 基础设置需求分析 3、 搭建项目框架环境--ssh(复习) 4、 SpringData-JPA持久层入门案例(重点) 5、 Easyui menubutton菜单按钮使用 6、 Easyui messager消息框使用
1. Jquery ztree使用展示菜单数据 2. 基础设置需求分析 3. 搭建项目框架环境--ssh(复习) 4. SpringData-JPA持久层入门案例(重点) 5. Easyui menu ...
- ARC097D Equals
传送门 题目 We have a permutation of the integers from 1 through N, p1, p2, .., pN. We also have M pairs ...
- final注意事项
final修饰的类为终态类,不能被继承,而 抽象类是必须被继承的才有其意义的,因此,final是不能用来修饰抽象类的. final修饰的方法为终态方法,不能被重写.而继承抽象类,必须重写其方法. 抽象 ...
- 截止JDK1.8版本,java并发框架支持锁包括?
读写锁 自旋锁 乐观锁
- CSS中position的absolute和relative用法
static: HTML元素的默认定位方式 absolute: 将对象从文档流中拖出,使用left,right,top,bottom等属性进行绝对定位.而其层叠通过z-index属性定义.绝对定位的元 ...