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. MYSQL进阶学习笔记十八:MySQL备份和还原!(视频序号:进阶_37)

    知识点十九:MySQL的备份的还原(38) 一.mysql的备份 1.通过使用mysqldump的命令备份 使用mysqldump命令备份,mysqldump命令将数据库中的数据备份成一个文本文件.表 ...

  2. 使用grunt js进行js的链接和压缩

    1,http://nodejs.org/download/ 安装nodejs 2,配置环境变量,将nodejs的安装目录放置在Path环境变量中 3,在cmd中 npm install -g grun ...

  3. Java深度理解——Java字节代码的操纵

    导读:Java作为业界应用最为广泛的语言之一,深得众多软件厂商和开发者的推崇,更是被包括Oracle在内的众多JCP成员积极地推动发展.但是对于 Java语言的深度理解和运用,毕竟是很少会有人涉及的话 ...

  4. 我在面试.NET/C#程序员时会提出的问题

    我在面试.NET/C#程序员时会提出的问题 2011-03-03 15:38 by 老赵, 28107 visits 说起来我也面试过相当数量的.NET(包括C#,后文不重复)程序员了,有的通过电话, ...

  5. Opencv— — image offset

    // define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...

  6. 关于SelectObject之后是否要恢复之前的GDI对象

    以下列代码为例 { // 创建内存DC CDC mMemDc; mMemDc.CreateCompatibleDC( &dc ); // 创建兼容位图 CBitmap bmpMemBmp; b ...

  7. configure: error: APR not found. Please read the documentation

    本以为Apache的编译安装很简单,其实不然: 以前的环境下编译报错很少 ,但这次不行了 提示configure: error: APR not found. Please read the docu ...

  8. 杂文笔记《Redis在万亿级日访问量下的中断优化》

    杂文笔记<Redis在万亿级日访问量下的中断优化> Redis在万亿级日访问量下的中断优化 https://mp.weixin.qq.com/s?__biz=MjM5ODI5Njc2MA= ...

  9. Tool:Power Designer

    ylbtech-Tool:Power Designer 1.返回顶部 1. PowerDesigner最初由Xiao-Yun Wang(王晓昀)在SDP Technologies公司开发完成.Powe ...

  10. 类似懒加载的js功能

    <!-- 显示15条数据 --> <ul class="list" pagesize="15"> <li> <div ...