Turtles (非纯分块)

本文详细解析了CodeForces比赛中的D题解决方案,通过优化的读取输入方式和动态规划算法,有效地解决了大规模数据处理的问题。代码中使用了C++进行实现,包括快速读取输入、动态规划求解子问题等关键步骤。

http://codeforces.com/contest/103/problem/D

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 #define faster ios::sync_with_stdio(0);cin.tie(0)
 5 
 6 inline ll read()
 7 {
 8     int x=0,f=1;char ch=getchar();
 9     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
10     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
11     return x*f;
12 }
13 
14 /********************************************************************/
15 
16 const int maxn = 3e5+5;
17 ll n, m;
18 ll a[maxn];
19 ll cx[maxn], cy[maxn];
20 int kiss = 600;
21 vector<int> d[600];
22 ll dp[maxn];
23 ll ans[maxn];
24 
25 int main(){
26     n = read();
27     for(int i = 1;i <= n;i++)
28         a[i] = read();
29     m = read();
30     for(int i = 1;i <= m;i++){
31         cx[i] = read(), cy[i] = read();
32         if(cy[i] < kiss)
33             d[cy[i]].push_back(i);
34         else{
35             ll ans1 = 0;
36             for(int j = cx[i];j <= n;j += cy[i]){
37                 ans1 += a[j];
38             }
39             ans[i] = ans1;
40         }
41     }
42     for(int i= 1;i < kiss;i++){
43         if(d[i].size()){
44             for(int j = n;j;j--){       //From the back forward
45                 if(j+i > n)
46                     dp[j] = a[j];
47                 else
48                     dp[j] = dp[j+i] + a[j];
49             }
50             for(int j = 0;j < d[i].size();j++)
51                 ans[d[i][j]] = dp[cx[d[i][j]]];
52         }
53     }
54     for(int i = 1;i <= m;i++)
55         cout << ans[i] << endl;
56     return 0;
57 }

 

转载于:https://www.cnblogs.com/ouyang_wsgwz/p/9610335.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值