http://arc066.contest.atcoder.jp/tasks/arc066_c?lang=en

这类题目是我最怕的,没有什么算法,但是却很难想,

这题的题解是这样的,观察到,在+号里面添加括号是没用的,

那么看看减号,任意两个相邻减号,

比如1 - 20 + 8 - 13 - 5 + 6 + 7 - 8

可以变成1 - (20 + 8 - 13) + 5 + 6 + 7 + 8

为什么后面的可以全部都变成正数呢?

因为可以这样变,1 - (20 + 8 - 13 - (5 + 6 + 7) - 8)

所以,观察到,这个观察到,到底需要多大的脑洞呢?

暴力枚举任意一对相邻的减号,只有其里面包括的数字全部变成负数为代价,使得后面的数字全部变正。

暴力枚举即可。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 1e5 + ;
LL perfixSum[maxn], absSum[maxn];
vector<int>pos;
void cut(LL &val, int pos1, int pos2) {
if (pos1 > pos2) return;
val -= absSum[pos2] - absSum[pos1 - ];
}
void work() {
int n;
scanf("%d", &n);
int val;
scanf("%d", &val);
perfixSum[] = val;
absSum[] = val;
for (int i = ; i <= n; ++i) {
int val;
char op;
cin >> op;
scanf("%d", &val);
if (op == '+') {
perfixSum[i] = perfixSum[i - ] + val;
} else {
perfixSum[i] = perfixSum[i - ] - val;
pos.push_back(i);
}
absSum[i] = absSum[i - ] + val;
}
LL ans = perfixSum[n];
for (int i = ; i <= (int)pos.size() - ; ++i) {
int p1 = pos[i], p2 = pos[i + ];
LL tans = absSum[n] - absSum[p2 - ];
tans += perfixSum[p1];
cut(tans, p1 + , p2 - );
ans = max(ans, tans);
}
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
// int val;
// scanf("%d", &val);
// cout << val << endl;
work();
return ;
}

E - Addition and Subtraction Hard AtCoder - 2273 思维观察题的更多相关文章

  1. [leetcode-592-Fraction Addition and Subtraction]

    Given a string representing an expression of fraction addition and subtraction, you need to return t ...

  2. [LeetCode] Fraction Addition and Subtraction 分数加减法

    Given a string representing an expression of fraction addition and subtraction, you need to return t ...

  3. [Swift]LeetCode592. 分数加减运算 | Fraction Addition and Subtraction

    Given a string representing an expression of fraction addition and subtraction, you need to return t ...

  4. 592. Fraction Addition and Subtraction

    Problem statement: Given a string representing an expression of fraction addition and subtraction, y ...

  5. [LeetCode] 592. Fraction Addition and Subtraction 分数加减法

    Given a string representing an expression of fraction addition and subtraction, you need to return t ...

  6. LC 592. Fraction Addition and Subtraction

    Given a string representing an expression of fraction addition and subtraction, you need to return t ...

  7. 【LeetCode】592. Fraction Addition and Subtraction 解题报告(Python)

    [LeetCode]592. Fraction Addition and Subtraction 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuem ...

  8. [Gym101982M][思维好题][凸壳]Mobilization

    [gym101982M][思维好题][凸壳]Mobilization 题目链接 20182019-acmicpc-pacific-northwest-regional-contest-div-1-en ...

  9. 土题大战Vol.0 A. 笨小猴 思维好题

    土题大战Vol.0 A. 笨小猴 思维好题 题目描述 驴蛋蛋有 \(2n + 1\) 张 \(4\) 星武器卡片,每张卡片上都有两个数字,第 \(i\) 张卡片上的两个数字分别是 \(A_i\) 与 ...

随机推荐

  1. XML中CDATA和#PCDATA的区别

    在XML文档中, 能看到“CDATA"的地方有三处: 1)在DTD中,指定标签中某个属性的类型为字符型时,使用CDATA.因为XML解析器会去分析这段字符内容,因而里面如果需要使用>, ...

  2. BestCoder7 1001 Little Pony and Permutation(hdu 4985) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4985 题目意思:有 n 个数,对于第 i 个数给出 σ(i) 的值.求出互不相交的循环的个数,并输出每 ...

  3. nginx + ffmpeg

    第一种方案:ffmpeg+nginx 新的ffmpeg已经支持HLS.(本人也参与了代码供献,给自己做个广告:)) 点播: 生成hls分片: ffmpeg -i <媒体文件> -c:v l ...

  4. Hibernate 模糊查询 ' %?% ' SQL执行异常

    今天我在使用Hibernate 的SQL预编译之后注入参数的形式写了一条模糊查询语句.刚开始我是这么写的

  5. servlet里的forward和redirect的区别

    forward方式:request.getRequestDispatcher("/somePage.jsp").forwardrequest, response);     red ...

  6. linux命令:wc命令

    Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式: wc [选项]文件... 2.命令功能: 统计指定文件中的字节数. ...

  7. vscode实现列编辑

    ctrl + shift + 左键选择要编辑的列 好用,再也不用使用\n替换了

  8. HDU - 1495 非常可乐 bfs互倒三杯水

    非常可乐 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  9. jQuery 实现网页跳转或用命令打开指定网页!

    Jquery实现网页跳转或用命令打开指定网页! location.href = "www.baidu.com"; location.href = "aa.aspx&quo ...

  10. numpy.ndarray常用属性和方法

    import numpy as np a = np.array([[1,2,3],[4,3,2],[6,3,5]])print(a) [[1 2 3] [4 3 2] [6 3 5]] print(a ...