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. POJ 3764 The xor-longest( 树上异或前缀和&字典树求最大异或)

    In an edge-weighted tree, the xor-length of a path p is defined as the xor sum of the weights of edg ...

  2. HihoCoder 1504 : 骑士游历 (矩阵乘法)

    描述 在8x8的国际象棋棋盘上给定一只骑士(俗称“马”)棋子的位置(R, C),小Hi想知道从(R, C)开始移动N步一共有多少种不同的走法. 输入 第一行包含三个整数,N,R和C. 对于40%的数据 ...

  3. HDU1525 Euclid's Game

    Two players, Stan and Ollie, play, starting with two natural numbers. Stan, the first player, subtra ...

  4. UVA562(01背包均分问题)

    Dividing coins Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Descriptio ...

  5. HDU 1143 Tri Tiling 递归问题

    将一个3*n的矩形用1*2的矩形填充,n为奇数时一定不能被填满,n*3%2==1 接下来处理这个问题我们要从简单的情况开始考虑,所谓递归就是要能将问题的规模不断减小,通过小问题的解决最后将复杂问题解决 ...

  6. .NETFramework:StaticValueInjecter

    ylbtech-.NETFramework:StaticValueInjecter 1.程序集 Omu.ValueInjecter, Version=3.1.1.0, Culture=neutral, ...

  7. css3 position:sticky

    最近在写一个小程序,项目中遇到一个需求:页面滚动到tab切换菜单时,菜单fixed到页面顶部: 实现方法: 使用小程序的onPageScroll事件,滚动到指定位置添加fixed样式: bug1:获取 ...

  8. Java--23种设计模式之decorator模式

    装饰模式:装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案,提供比继承更多的灵活性.动态给一个对象增加功能,这些功能可以再动态的撤消.增加由一些基本功能的排列组合而产生的非常大量的 ...

  9. Python_中__init__和__new__的异同

    1:__new__:它是创建对象时调用,会返回当前对象的一个实例: __init__:它是创建对象后调用,对当前对象的一些实例初始化,无返回值 代码示例: >>> class Dat ...

  10. 12_tcp_ip相关概念

    java基础班  网络编程应该提过.大学学计算机或者是通信的对这些东西肯定比较熟一些.主机到网络层是跟硬件相关的一些协议了.上层协议得依赖下层的协议.也就是说它们得联合起来共同工作才能够把数据传输出去 ...