Neko's loop
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
Problem Description
Neko has a loop of size n.
The loop has a happy value ai on the i−th(0≤i≤n−1) grid.
Neko likes to jump on the loop.She can start at anywhere. If she stands at i−th grid, she will get ai happy value, and she can spend one unit energy to go to ((i+k)modn)−th grid. If she has already visited this grid, she can get happy value again. Neko can choose jump to next grid if she has energy or end at anywhere.
Neko has m unit energies and she wants to achieve at least s happy value.
How much happy value does she need at least before she jumps so that she can get at least s happy value? Please note that the happy value which neko has is a non-negative number initially, but it can become negative number when jumping.
Input
The first line contains only one integer T(T≤50), which indicates the number of test cases.
For each test case, the first line contains four integers n,s,m,k(1≤n≤104,1≤s≤1018,1≤m≤109,1≤k≤n).
The next line contains n integers, the i−th integer is ai−1(−109≤ai−1≤109)
Output
For each test case, output one line "Case #x: y", where x is the case number (starting from 1) and y is the answer.
Sample Input
2 3 10 5 2 3 2 1 5 20 6 3 2 3 2 1 5
Sample Output
Case #1: 0 Case #2: 2
题意:
有n个活动编号0~n-1,每个活动都有一个欢乐值(可以为负),给你两个数m和k,一开始你可以从中选择任意一个活动,但之后每当完成第i个活动,下一次只能选择编号为(i+k)%n的活动,当然一个活动可以被完成多次,并且欢乐值累加,求在最多参加m此活动的情况下最多能获得多少欢乐值,输出max(s-ans, 0)
思路:
当你选择起点后,能做的任务就构成了一个环,例如n=6, k=4,很显然0→4→2→0构成了一个环
根据裴蜀定理,总共有gcd(n,k)个环,每个环长度为n/gcd(n,k)
对每个环单独求,这样就变成了一个线段树区间最值问题了
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
#define LL long long
#define mod 1000000007
int Gcd(int x, int y)
{
if(y==0)
return x;
return Gcd(y, x%y);
}
LL a[10005], p[20005], sum[20005];
LL tre[88888];
void Add(int l, int r, int x)
{
int m;
if(l==r)
{
tre[x] = sum[r];
return;
}
m = (l+r)/2;
Add(l, m, x*2);
Add(m+1, r, x*2+1);
tre[x] = min(tre[x*2], tre[x*2+1]);
}
LL Query(int l, int r, int x, int a, int b)
{
int m;
LL ans = 1e18;
if(l>=a && r<=b)
return tre[x];
m = (l+r)/2;
if(a<=m)
ans = min(ans, Query(l, m, x*2, a, b));
if(b>=m+1)
ans = min(ans, Query(m+1, r, x*2+1, a, b));
return ans;
}
int main(void)
{
LL s, all, ans, temp, M;
int T, n, m, k, i, j, len, cnt, now, cas = 1;
scanf("%d", &T);
while(T--)
{
ans = 0;
scanf("%d%lld%d%d", &n, &s, &m, &k);
for(i=1;i<=n;i++)
scanf("%lld", &a[i]);
cnt = Gcd(n, k), len = n/cnt;
for(i=1;i<=cnt;i++)
{
now = i, M = m;
for(j=1;j<=len;j++)
{
p[j] = p[j+len] = a[now];
now = now+k;
if(now>=n+1)
now -= n;
}
for(j=1;j<=len*2;j++)
sum[j] = sum[j-1]+p[j];
Add(1, len*2, 1);
all = temp = 0;
if(sum[len]>0)
all = sum[len]*(M/len);
M %= len;
for(j=len+1;j<=len*2;j++)
temp = max(temp, sum[j]-Query(1, len*2, 1, j-M, j));
ans = max(ans, all+temp);
if(m>=len)
{
M = m-len;
if(sum[len]>0)
all = sum[len]*(M/len);
M = len;
for(j=len+1;j<=len*2;j++)
temp = max(temp, sum[j]-Query(1, len*2, 1, j-M, j));
ans = max(ans, all+temp);
}
}
printf("Case #%d: %lld\n", cas++, max(0ll, s-ans));
}
return 0;
}