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 ...
随机推荐
- bzoj 2648 SJY摆棋子——KDtree
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2648 第一道KDtree! 学习资料:https://blog.csdn.net/zhl30 ...
- django基础PROJECT APP View template
project 和 app 的区别就是一个是配置另一个是代码: 一个project包含很多个Django app以及对它们的配置. 一个project的作用是提供配置文件,比方说哪里定义数据库连接信息 ...
- flask+uswgi+nginx+python3.6的venv发布网站ubuntu14.04
---------------------------nginx--------------- sudo apt-get install nginx sudo apt-get remove nginx ...
- Python:format()方法
转于:https://blog.csdn.net/zhang89xiao/article/details/53818906 博主:张肖的博客 描述: format的格式 replacement_fie ...
- Python:map()、reduce()、filter()的区别
文章转于:https://blog.csdn.net/goupper1991/article/details/49803355 原文博主:https://blog.csdn.net/goupper19 ...
- Python之常用模块(二)
shelve xml处理 configparser hashlib logging shelve模块 shelve是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持 ...
- glusterfs安装配置简单使用
GlusterFS是一种分布式分布式文件系统,默认采用无中心完全对等架构,搭建维护使用十分简单,是很受欢迎的分布式文件系统. 官网https://www.gluster.org/,官网上表示Glust ...
- httpd或Nginx负载均衡tomcat
实验环境:CentOS7 #两台tomcat的基本配置如下: [root@webapps localhost]#setenforce 0 [root@webapps localhost]#iptabl ...
- iOS使用VideoToolbox硬编码录制H264视频
http://blog.csdn.net/shawnkong/article/details/52045894
- 28、cd-hit去除冗余序列
转载:http://blog.sina.com.cn/s/blog_670445240101nidy.html 网址:http://cd-hit.org :http://www.bioinformat ...