【五校联考7day1】游戏

Description
WYF从小就爱乱顶,但是顶是会造成位移的。他之前水平有限,每次只能顶出k的位移,也就是从一个整点顶到另一个整点上。我们现在将之简化到数轴上,即从 一个整点可以顶到与自己相隔在k之内的数轴上的整点上。现在WYF的头变多了,于是他能顶到更远的地方,他能顶到任意整点上。现在他在玩一个游戏,这个游 戏里他只能向正方向顶,同时如果他从i顶到j,他将得到a[j] * (j - i)的分数,其中a[j]是j点上的分数,且要求j > i, 他最后必须停在n上。
现给出1~n上的所有分数,原点没有分数。他现在在原点,没有分。WYF想知道他最多能得多少分。

Input
第一行一个整数n。
第二行有n个整数,其中第i个数表示a[j]。

Output
一个整数,表示WYF最多能得到的分数。

Sample Input
3
1 1 50

Sample Output
150

Data Constraint
对于60%的数据,n<=1000;
对于100%的数据,n<=100000,0<=a[j]<=50。

.
.
.
.
.
分析
这道题可以用斜率优化贪心来做

在这里插入图片描述

.
.
.
.
.
程序:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

struct edge
{
	int w,wz;
} a[100100];

bool cmp(edge x,edge y)
{
	return x.w>y.w;
}

int main()
{
	freopen("game.in","r",stdin);
	freopen("game.out","w",stdout);
	int n;
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&a[i].w);
		a[i].wz=i;
	}
	sort(a+1,a+n+1,cmp);
	int ans=0,now=0;
	for (int i=1;i<=n;i++)
		if (now<a[i].wz)
		{
			ans+=(a[i].wz-now)*a[i].w;
			now=a[i].wz;
		}
	printf("%d",ans);
	fclose(stdin);
	fclose(stdout);
	return 0;
}
原文地址:https://www.cnblogs.com/YYC-0304/p/10458930.html