【Luogu】P3957 [NOIP2017 Popularity Group] Hopscotch

【Luogu】P3957 [NOIP2017 Popularity Group] Hopscotch

Title description

Hopscotch, also called jumping plane, is a worldwide children's game and one of the traditional Chinese folk sports games.

The rules of the hopscotch game are as follows:

Determine a starting point on the ground, and then draw n grids on the right side of the starting point. These grids are all on the same straight line. There is a number (integer) in each grid, which indicates the score that can be obtained by reaching this grid. The player jumps to the right from the starting point for the first time, and jumps into a grid to the right of the starting point. For the second time, continue to jump to the right from the current position, and so on. The rules stipulate:

The player must jump to a grid to the right of the current position each time. Players can end the game at any time, and the score obtained is the sum of the numbers in the grid that they have reached.

Now Xiao R has developed a bouncing robot to participate in this game. But this robot has a very serious flaw. The distance it can bounce to the right each time can only be a fixed d. Little R wants to improve his robot. If he spends g coins to improve his robot, then his robot flexibility can increase g, but it should be noted that the distance of each bounce is at least 1. Specifically, when g<d, the distance his robot can choose to bounce to the right each time is d−g, d−g+1, d−g+2,..., d+g−2, d+g −1, d+g; otherwise (when g≥d), the distance that his robot can choose to bounce to the right each time is 1, 2, 3,..., d+g-2, d+g−1, d +g.

Now Little R wants to get at least k points. May I ask him how many gold coins he must spend at least to transform his robot.

Input format

The three positive integers n, d, and k in the first row respectively represent the number of grids, the fixed distance of the robot's bounce before the improvement, and the desired score at least. Use a space to separate two adjacent numbers.

In the next n rows, each row contains two integers x i , s i , which respectively represent the distance from the starting point to the iith grid and the score of the iith grid. Use a space to separate the two numbers. Ensure that x i are entered in increasing order.

Output format

A total of one line, an integer, indicating at least how many gold coins must be spent to transform his robot. If he can't get at least k points anyway, output -1.

Sample input and output

enter
7 4 10
2 6
5 -3
10 3
11 -3
13 1
17 6
20 2
Output
2
enter
7 4 20
2 6
5 -3
10 3
11 -3
13 1
17 6
20 2
Output
-1

Instructions/tips

[Description of input and output example 1] After the improvement of the two gold coins, the small R robots choose to bounce to the right by 2, 3, 5, 3, 4, and 3 respectively. The positions reached respectively are 2, 5, 10 , 13, 17, 20 correspond to the 6 grids of 1, 2, 3, 5, 6, 7. The sum of the numbers in these grids, 15 is the score obtained by the small R.

Input and output example 2 Description
Since the maximum possible number of combinations of 7 grids in the example is only 18, 20 points cannot be scored anyway.

Data scale and convention

There are 10 sets of test data in this question, each with 10 points.

For all the data, 1 ≤ n ≤ 500000, 1 ≤ d ≤2000, 1 ≤ x i , k ≤ 10 9 , |s i |<10 5 .

For the first and second groups of test data, n ≤ 10;

For the 3rd, 4th and 5th groups of test data, n ≤ 500

For the 6th, 7th and 8th groups of test data, d = 1

Ideas

Dichotomous answer,
monotonic queue optimization.

Code

#include<iostream>
#include<cstdio>
#include<deque>
using namespace std;
long long dis[500010],score[500010],f[500010],que[500010],n,d,k,ans,ql,qr;
bool check(long long x)
{
    
    
	int i,l=max((long long)1,d-x),r=d+x,lst=1;
	ql=0,qr=1,que[0]=0;
	for(i=1;i<=n;i++)
	{
    
    
		for(;qr-ql>1&&dis[i]-dis[que[ql]]>r;ql++);//单调队列优化,删除>r的数 
		if(l<=dis[i]-dis[que[ql]]&&dis[i]-dis[que[ql]]<=r) f[i]=f[que[ql]]+score[i];
		else f[i]=-10000000000000000;
		if(f[i]>=k)return 1;
		for(;lst<=i&&dis[i+1]-dis[lst]>=l;lst++)
		{
    
    
			for(;qr>ql&&f[que[qr-1]]<=f[lst];qr--);//保证>=l 
			que[qr++]=lst;
		}
	}
	return 0;
}
int main()
{
    
    
	long long i,mid,l,r;
	scanf("%lld%lld%lld",&n,&d,&k);
	for(i=1;i<=n;i++)
		scanf("%lld%lld",&dis[i],&score[i]);
	for(ans=-1,l=0,r=1000000000;l<=r;)//二分答案 
	{
    
    
		mid=(l+r)>>1;
		if(check(mid))
			ans=mid,r=mid-1;
		else
			l=mid+1;
	}
	printf("%lld",ans);
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_46975572/article/details/114671033