【hdu 6444】Neko's loop
【链接】 我是链接,点我呀:)
【题意】
给你一个序列.
你可以选择起点i。
然后每次往右跳k次。
得到下一个值a[i+k];。
问你跳m次能得到的最大值ma是多少。
如果>=s输出0
否则输出s-ma;
【题解】
最后肯定会形成gcd(n,k)个环的。
对于每个环(长度为cnt。
预处理出从1..2*cnt的前缀和C[2*cnt](当成链处理就好
枚举它从起点i开始。
然后考虑它会怎么走?
1.如果c[cnt]>0,temp1加上m/cnt*C[cnt],然后对于剩余的m%cnt次走的机会。
求出c[i-1..i+m%cnt-1]这一段的最大值get_ma,减去c[i-1]就是剩余的m%cnt次能走出来的最大值了。即temp1+=get_ma-c[i-1];
temp = max(temp,temp1)
2.如果m>=cnt,那么还有一种可能,就是剩余的最后一圈留着不走完整圈,而只取一个最大的值,这个时候
如果c[cnt]>0,temp2+=(m/cnt - 1)*C[cnt],然后我们还留了一圈,也即cnt次机会可以走
则求出c[i-1..i+cnt-1]这一段的最大值get_ma2,然后再减去c[i-1]就是剩余的cnt次能走出来的最大值了,即temp2+=get_ma2-C[i-1]
temp = max(temp,tepm1)
对于每个起点i。都求出temp1,tepm2
最后return temp
就是当前这个环上走m次能得到的最大值了。
枚举所有的环取最大的temp就是答案了
【代码】
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 1e4;
const int M = 15;
int n,m,k;
LL s;
int a[N+10],b[N*2+10],cnt;
LL c[N*2+10],mi[N*2+10][M+5];
int vis[N+10];
LL get_ma(int l,int r){
int len = r-l+1;
len = log2(len);
return max(mi[l][len],mi[r-(1<<len)+1][len]);
}
LL ok(){
c[0] = 0;
for (int i = 1;i <= cnt;i++)
c[i] = b[i],c[i+cnt] = b[i];
for (int i = 1;i <= 2*cnt;i++) c[i]+=c[i-1];
for (int i = 0;i <= 2*cnt;i++) mi[i][0] = c[i];
for (int L = 1;L<=M;L++)
for (int i = 0;i <= 2*cnt;i++){
if (i+(1<<L)-1>2*cnt) break;
mi[i][L] = max(mi[i][L-1],mi[i+(1<<(L-1))][L-1]);
}
LL temp = 0;
for (int i = 1;i <= cnt;i++){
LL temp1 = 0;
//第一种情况.
//如果环的和大于0就尽量用
if (c[cnt]>0) temp1 += 1LL*m/cnt*c[cnt];
int rest = m%cnt;
if (rest>0) temp1+=get_ma(i-1,i+rest-1)-c[i-1];
LL temp2 = 0;
//第二种情况
//留cnt个
if (m>=cnt){
if (c[cnt]>0) temp2 += 1LL*(m-cnt)/cnt*c[cnt];
temp2+=get_ma(i-1,i+cnt-1)-c[i-1];
}
temp = max(temp,temp1);
temp = max(temp,temp2);
}
return temp;
}
int main()
{
//freopen("D:\\rush.txt","r",stdin);
ios::sync_with_stdio(0),cin.tie(0);
int T;
cin >> T;
int kk = 0;
while (T--){
cin >> n >> s >> m >> k;
for (int i = 1;i <= n;i++) cin >> a[i];
for (int i = 1;i <= n;i++) vis[i] = 0;
LL ans = 0;
for (int i = 1;i <= n;i++)
if (vis[i]==0){
cnt = 0;
for (int j = i;vis[j]==0;j = (j+k)>n?(j+k-n):j+k){
cnt++;
b[cnt] = a[j];
vis[j] = 1;
}
ans = max(ans,ok());
}
cout<<"Case #"<<++kk<<": ";
if (s<ans)
cout<<0<<endl;
else
cout<<s-ans<<endl;
}
return 0;
}
【hdu 6444】Neko's loop的更多相关文章
- 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题
[HDU 3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】
[把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...
- 【HDU 2196】 Computer(树的直径)
[HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...
- 【HDU 2196】 Computer (树形DP)
[HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...
- 【HDU 5145】 NPY and girls(组合+莫队)
pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...
- 【hdu 1043】Eight
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...
- 【HDU 3068】 最长回文
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...
- 【HDU 4699】 Editor
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4699 [算法] 维护两个栈,一个栈放光标之前的数,另外一个放光标之后的数 在维护栈的同时求最大前缀 ...
随机推荐
- nginx tomcat glassfish session 复制配置
1.nginx配置 [root@www1-nlb1 ~]# cat /usr/local/nginx/conf/nginx.conf | grep -v ^$ | grep -v ^# user w ...
- Spring技术内幕:SpringIOC原理学习总结
前一段时候我把Spring技术内幕的关于IOC原理一章看完,感觉代码太多,不好掌握,我特意又各方搜集了一些关于IOC原理的资料,特加深一下印象,以便真正掌握IOC的原理. IOC的思想是:Spring ...
- Unable to read the project file 'client.csproj'. Could not load file or assembly 'Microsoft.Build.En
错误具体信息: Unable to read the project file 'client.csproj'. Could not load file or assembly 'Microsoft. ...
- CF D. Beautiful numbers (数位dp)
http://codeforces.com/problemset/problem/55/D Beautiful Numbers : 这个数能整除它的全部位上非零整数.问[l,r]之间的Beautifu ...
- Codeforces 115A- Party(DFS)
A. Party time limit per test 3 seconds memory limit per test 256 megabytes input standard input outp ...
- Java深入 - Filter过滤器
Java的1.3開始,对servlet2.3规范中增加了过滤器的支持.过滤器可以让我们对目标资源的请求和响应进行截取. 一些filter的特性: 1. Filter是Servlet规范的规定,须要Se ...
- pod install fatal: unable to access 'https://chromium.googlesource.com/webm/libwebp/'
1. 前往文件夹 /用户/aki/.cocoapods/repos/master/1/9/2/libwebp 2.编辑libwebp.podspec.json 将source git 改为 https ...
- python实现高速排序算法(两种不同实现方式)
# -*- coding: utf-8 -*- """ Created on Fri May 16 17:24:05 2014 @author: lifeix " ...
- JS 经验总结
1.IE中div的高度是content+padding+border之和,其它的是content的高度 2.一个标签里面只有一个属性,class='cls1 cls2' 3.同一页面不能出现相同的id ...
- C#中的值类型、引用类型,代码告诉你他是什么类型。
C#代码告诉你这是什么类型. using System; using System.Collections.Generic; using System.Linq; using System.Text; ...