D. Rating System

Problem - D - Codeforces

D. Rating System_第1张图片

 D. Rating System_第2张图片

 思路:我们先将输入数据做一个前缀和,能够得到它的变化,然后我们能够发现我们只需要找到两个点,第一个点-第二个点最大即可,因为假如说我们现在到了一峰

D. Rating System_第3张图片

// Problem: D. Rating System
// Contest: Codeforces - Educational Codeforces Round 151 (Rated for Div. 2)
// URL: https://codeforces.com/problemset/problem/1845/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include 
#include
#include
#include
#include
#define fi first
#define se second
#define i128 __int128
using namespace std;
typedef long long ll;
typedef double db;
typedef pair PII;
typedef pair > PIII;
const double eps=1e-7;
const int N=5e5+7 ,M=5e5+7, INF=0x3f3f3f3f,mod=1e9+7,mod1=998244353;
const long long int llINF=0x3f3f3f3f3f3f3f3f;
inline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;}
inline void write(ll x) {if(x < 0) {putchar('-'); x = -x;}if(x >= 10) write(x / 10);putchar(x % 10 + '0');}
inline void write(ll x,char ch) {write(x);putchar(ch);}
void stin() {freopen("in_put.txt","r",stdin);freopen("my_out_put.txt","w",stdout);}
bool cmp0(int a,int b) {return a>b;}
template T gcd(T a,T b) {return b==0?a:gcd(b,a%b);}
template T lcm(T a,T b) {return a*b/gcd(a,b);}
void hack() {printf("\n----------------------------------\n");}

int T,hackT;
int n,m,k;
ll w[N];

void solve() {
	n=read();
	
	for(int i=1;i<=n;i++) w[i]=read();
	
	for(int i=2;i<=n;i++) w[i]+=w[i-1];
	
	ll res=0;
	ll maxn=0;
	ll last=0-w[1];
	for(int i=1;i<=n;i++) {
		ll t=maxn-w[i];
		if(t>last) {
			last=t;
			res=maxn;
		}
		maxn=max(maxn,w[i]);
	}
	
	printf("%lld\n",res);
}   

int main() {
    // init();
    // stin();

    scanf("%d",&T);
    // T=1; 
    while(T--) hackT++,solve();
    
    return 0;       
}          

 

你可能感兴趣的:(codeforces,算法)