UVA 699 The Falling Leaves

https://vjudge.net/problem/UVA-699

题目

水得不行,就不说了……

题解

水题,只是注意数组不能开太大……直接就TLE了(缩小10倍用了0.9秒,服了……)

AC代码(其实没有看的必要)

#include<bits/stdc++.h>
using namespace std;

#define MAXN 1000007

int ground[MAXN];//MAXN太大导致TLE(上界估计得太松)

int lmax, rmax;

inline void getint(int &ans) {
	ans = 0;
	bool sign = false;
	char ch = getchar();
	while(!isdigit(ch) && (ch!='-')) ch = getchar();
	if(ch=='-') ch = getchar(), sign = true;
	while(isdigit(ch)) {
		ans = ans*10+ch-'0';
		ch = getchar();
	}
	if(sign) ans = -ans;
}

inline void build(int pos) {
	int v;
	getint(v);
	if(!~v) return;
	if(pos<lmax) lmax=pos;
	if(pos>rmax) rmax=pos;
	ground[pos]+=v;
	build(pos-1); build(pos+1);
}

int h=0;
char st[1007];
inline void putint(int k) {
	int sti=0;
	bool sign = false;
	if(k<0) sign = true, k=-k;
	do {
		st[sti++]=k%10;
		k/=10;
	} while(k>0);
	if(h+sign+sti>80) putchar('
'), h=0;
	else if(h>0) putchar(' ');
	h+=sign+sti;
	if(sign) putchar('-');
	while(0<sti--) {
		putchar(st[sti]+'0');
	}
}

int main() {
	int kase = 0;
	while(1) {
		if(kase) putchar('
');
		kase++;
		lmax = rmax = MAXN/2; h=0;
		memset(ground, 0, sizeof ground);
		build(MAXN/2);
		if(ground[MAXN/2]==0) break;
		
		printf("Case %d:
", kase);
		for(int i=lmax; i<=rmax; i++) {
			putint(ground[i]);
		}
		putchar('
');
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/sahdsg/p/10394420.html