P6625 [省选联考 2020 B 卷] 卡牌游戏

P6625 [省选联考 2020 B 卷] 卡牌游戏

这道题是道送分题,没有什么坑点,反正在我们jx弱省都没有人会错。

大概意思就是,给你个数列\(a[i]\),每次将前面任意的\(k\)项(\(k\geq2\))合并成一项,加上这些项的和,问你怎么选最大。

一看到这道题,发现最大的痛点就是这个合并。但是我们研究一会儿就会发现,我每一次这样的“合并”,与之前的“合并”没有任何关系。你合并了是加上这些项的和,不合并也是加这些项的和。

那么就很明了了。统计一下前缀和\(sum[i]\)。从第二项开始,如果\(sum[i]\)是正的,那么就累加到答案。(至于为啥不考虑第一项,是因为你总不能把一张牌自己合并了把)

还有,记得开long long

#include<cstring>
#include<cstdio>
typedef long long ll;
ll read() {
   ll x = 0, f = 1; char ch = getchar();
   for(; ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
   for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
   return x * f;
}
const int MAXN = 1e5 + 5;
int n;
ll a[MAXN];
ll sum[MAXN];
ll ans;
int main() {
   n = read();
   for(int i = 1; i <= n; i++) a[i] = read();
   for(int i = 1; i <= n; i++) sum[i] = sum[i-1] + a[i];
   for(int i = 2; i <= n; i++)
      if(sum[i] > 0) ans += sum[i];
   printf("%lld", ans);
   return 0;
}

。。水,很水

不愧是B卷D1T1

猜你喜欢

转载自www.cnblogs.com/riju-yuezhu/p/13192801.html
今日推荐