2018CCPC网络赛 && HDU 6444: G. Neko's loop(线段树)

 

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;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值