E - Addition and Subtraction Hard AtCoder - 2273 思维观察题
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 思维观察题的更多相关文章
- [leetcode-592-Fraction Addition and Subtraction]
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- [LeetCode] Fraction Addition and Subtraction 分数加减法
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- [Swift]LeetCode592. 分数加减运算 | Fraction Addition and Subtraction
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- 592. Fraction Addition and Subtraction
Problem statement: Given a string representing an expression of fraction addition and subtraction, y ...
- [LeetCode] 592. Fraction Addition and Subtraction 分数加减法
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- LC 592. Fraction Addition and Subtraction
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- 【LeetCode】592. Fraction Addition and Subtraction 解题报告(Python)
[LeetCode]592. Fraction Addition and Subtraction 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuem ...
- [Gym101982M][思维好题][凸壳]Mobilization
[gym101982M][思维好题][凸壳]Mobilization 题目链接 20182019-acmicpc-pacific-northwest-regional-contest-div-1-en ...
- 土题大战Vol.0 A. 笨小猴 思维好题
土题大战Vol.0 A. 笨小猴 思维好题 题目描述 驴蛋蛋有 \(2n + 1\) 张 \(4\) 星武器卡片,每张卡片上都有两个数字,第 \(i\) 张卡片上的两个数字分别是 \(A_i\) 与 ...
随机推荐
- zoj 2313 Chinese Girls' Amusement 解题报告
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1313 题目意思:有 N 个人(编号依次为1~N)围成一个圆圈,要求求 ...
- SDK Manager中勾选项
运行SDK Manager 勾选对应版本的SDK,从这里基本可以知道一个Android版本对应着一个版本的API. 其中每个包都有这么几个文件: Documentation for Android S ...
- Linux档案属性
输入命令:ls -al 档案类型权限: 第一個字元代表这个档案是『目录.档案或链接档等等』: 当为[ d ]则是目录: 当为[ - ]则是目录: 若是[ l ]则表示为链接档(link file): ...
- Java 高阶 —— native 关键字与 JNI
1. JNI Java中JNI的使用详解第一篇:HelloWorld JNI:Java Native Interface,Java 本地接口(或本机接口,更具体地讲叫本地编程接口),其提供了若干 AP ...
- python中format()方法格式化字符串
format()是python2.6新增的一个格式化字符串的方法,功能非常强大,有可能在未来完全替代%格式化方法,相比 % ,format()的优点有: 1 .格式化时不用关心数据类型的问题,form ...
- vim的tab缩进及用空格设置
编辑~/.vimrc文件,分别设置用空格而不是用tab,一个tab多少个空格,自动缩进多少宽度,显示行号. set expandtabset tabstop=4 set shiftwidth=4 se ...
- JAVA 中的堆和栈
栈与堆都是Java用来在Ram中存放数据的地方.与C++不同,Java自动管理栈和堆,程序员不能直接地设置栈或堆. Java的堆是一个运行时数据区,类的对象从中分配空间.这些对象通过new.n ...
- 19.break和continue
break;语句: 1.可以在switch语句中,结束分支语句: 2.break:语句可以出现在单循环当中,默认情况下结束距离他最近的一个循环. 3.break 后面跟一个循环的名字可以结束你指定的这 ...
- kafka之三:kafka java 生产消费程序demo示例
kafka是吞吐量巨大的一个消息系统,它是用scala写的,和普通的消息的生产消费还有所不同,写了个demo程序供大家参考.kafka的安装请参考官方文档. 首先我们需要新建一个maven项目,然后在 ...
- why ftp服务器采用多进程模式
为什么没有采用多线程或者IO复用,原因是在多线程或IO复用的情况下,当前目录是共享的,无法根据每一个连接来拥有自己的当前目录. 多进程模式下,一个连接拥有2个进程,一个是nobody进程,一个是服务进 ...