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 ...
随机推荐
- CentOS6 下Samba服务器的安装与配置
原地址:http://www.cnblogs.com/mchina/archive/2012/12/18/2816717.html 一.简介 Samba是一个能让Linux系统应用Microsoft网 ...
- 查看,检查,修复pg的命令
标签(空格分隔): ceph,ceph运维,pg 如果集群状态是HEALTH_ERR 并且有pgs inconsistent,需要进行如下操作: 1. 通过下面的命令查看哪些pg状态不一致: # ce ...
- linux命令-rpm查询包
安装了哪些rpm包呢 [root@wangshaojun Packages]# rpm -qa /////查看全部安装的包 [root@wangshaojun Packages]# rpm -qa l ...
- HTML5小知识
1.HTML5一种“妥协的”语法 2.特点: 标签不区分大小写 元素可以省略结束标签 元素属性可以省略属性值 属性的属性值可以不适用引号
- centos6.5安装dubbo管控台教程(四)
阅读此文之前,需要先安装zookeeper. 阅读文章: http://www.cnblogs.com/duenboa/articles/6665169.html 1. 下载文件 dubbo-ad ...
- 阿里巴巴Druid数据库连接池的使用
准备: 创建一个基于SpringBoot的web项目 1 引入相关依赖 jpa.mysql.druid <?xml version="1.0" encoding=" ...
- 杭电acm 1098题
Problem Description Ignatius is poor at math,he falls across a puzzle problem,so he has no choice bu ...
- C#报算术运算导致溢出的错误
如果是在fill datatable时报这个错,是因为所查询的数据中有在数据库中为NUMBER类型的字段,这种类型C#中不能识别,需TO_CHAR转换成字符型,就不会再报这个错了. e.g. SELE ...
- 2017-11-7 NOIP模拟赛
1.数学老师的报复 #include<iostream> #include<cstdio> using namespace std; int cnt; ]; long long ...
- 2017-10-17 NOIP模拟赛2
a [问题描述]你是能看到第一题的 friends 呢.——hja何大爷对字符串十分有研究,于是天天出字符串题虐杀 zhx.何大爷今天为字符串定义了新的权值计算方法.一个字符串由小写字母组成,字符串的 ...