[关闭]
@994495jj 2017-05-31T11:47:01.000000Z 字数 1155 阅读 816

codeforces contest 257

(ACM)思维



257D Sum(思维)

题意

题解

代码

  1. #include <algorithm>
  2. #include <iostream>
  3. #include <cstring>
  4. #include <vector>
  5. #include <cstdio>
  6. #include <string>
  7. #include <cmath>
  8. #include <queue>
  9. #include <set>
  10. #include <map>
  11. using namespace std;
  12. typedef long long ll;
  13. typedef unsigned long long ull;
  14. typedef pair<int,int> pii;
  15. typedef pair<ll,ll> pll;
  16. #define mp make_pair
  17. #define pb push_back
  18. #define fi first
  19. #define se second
  20. #define lson l,m,rt<<1
  21. #define rson m+1,r,rt<<1|1
  22. #define de(x) cout << #x << "=" << x << endl
  23. const int N=100005;
  24. int res[N],a[N];
  25. int main() {
  26. int n;
  27. while(~scanf("%d",&n)) {
  28. ///read
  29. for(int i=1;i<=n;++i) scanf("%d",a+i);
  30. ///solve
  31. ll sum=a[n];res[n]=1;
  32. for(int i=n-1;i>=1;--i) {
  33. if(-a[i]<=sum+a[i]&&sum+a[i]<=a[i]) {
  34. sum=sum+a[i];
  35. res[i]=1;
  36. } else {
  37. sum=sum-a[i];
  38. res[i]=0;
  39. }
  40. }
  41. if(sum<0) {
  42. for(int i=1;i<=n;++i) res[i]=1-res[i];
  43. }
  44. ///print
  45. for(int i=1;i<=n;++i) printf("%c",res[i]?'+':'-');
  46. puts("");
  47. }
  48. return 0;
  49. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注