7-6 拿糖果 (15 分)

版权声明:emmmmm喵喵喵 https://blog.csdn.net/MallowFlower/article/details/87634132

Long long ago,a handsome boy whose name is HSP studied in JSU of information science and engineering. He is clever and always thinks of ways to make fun of others. Now,it is your turn.

HSP和他的女朋友ZM来到了商店,商店有n个糖果,标号依次为1,2,3....n,对应的价值为W1,W2,W3...Wn。现在HSP先拿走一个标号为a的糖果,标号小于a的糖果就被ZM收回去了,然后HSP只能在剩下的糖果中选一个标号为b的糖果,请问Wa-Wb的最大值是多少?

输入格式:

多组数据输入,每一组数据第一行输入一个数字 n(2<=n<=100000),接下来n行,每行输入一个wi表示第i个糖果的价值

(0<wi<=100000)

输出格式:

每组数据输出Wa-Wb的最大值

输入样例:

3
3 2 1
6
1 1 1 1 1 1

输出样例:

2
0

JSU 2015ACM工作组

o(n)算法 正着倒着都可以
下面是倒着算的

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
int s[2000000];
int main(){
	int n;
	int wa,wb;
	int minn;
	int sum,sum1;
	while(cin>>n){
		//cout<<2<<endl;
		sum=-0x3f3f3f;
		minn=0x3f3f3f;
		for(int i=1;i<=n;i++){
			cin>>s[i];
		}
		for(int i=n;i>=1;i--){
			sum = max(sum, s[i] - minn);
			minn = min(minn, s[i]);
		}
		//cout<<1<<endl;
		cout<<sum<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/MallowFlower/article/details/87634132