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\) 与 ...
随机推荐
- ffmpeg加文字水印并控制水印显示时间或显示周期
#以下脚本保存成.sh文件运行,不会出现中文乱码问题 网上查到用enable关键字控制,实际是draw #加水印 水印位置由x,y,w,h来控制 #ffmpeg编译时需--enable-libfree ...
- 对xml文件的sax解析(增删改查)之二
先上代码: package com.saxparsetest; //the filename of this file is :saxparse.java import javax.xml.parse ...
- codeforces 469B Chat Online 解题报告
题目链接:http://codeforces.com/problemset/problem/469/B 题目意思:给出 Little Z 的上线时间段,分别是[a1, b1], [a2, b2],.. ...
- 更改Windows的“视频”“图片”“”下载“文档”“音乐”“桌面”的默认路径
右键-->属性
- hdu-2157 How many ways??(矩阵快速幂)
题目链接: How many ways?? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- vim 使用、设置笔记
一.设置.vimrc( windows下通常为_vimrc) 1.设置vim中tab的缩进 set ts=4 (注:ts是tabstop的缩写,设TAB宽4个空格) set expandtab (注 ...
- 为Xen虚拟机扩容根文件系统(LVM)
===== 为Xen虚拟机扩容根文件系统(LVM) ===== 1. 增加1个4G的映像文件 # dd if=/dev/zero of=data.img bs=4k seek=1024k count= ...
- awk里面执行shell命令
先把文件列表存在filename文件中 先 awk '{system("rm $0")}' filename -------WRONG 因为对于 system来说 $0 不再是某行 ...
- 3.1-3.3 HBase Shell创建表
一.HBase Shell创建表 1.HBASE shell命令 ## hbase(main):001:0> create_namespace 'ns1' //创建命名空间:ns1 hbase( ...
- Flutter实战视频-移动电商-22.JSON解析和复杂数据模型转换技巧
22.JSON解析和复杂数据模型转换技巧 json转Model类 创建model文件夹,在里面新建category.dart类 主要根据这个json来分析我们要做成类的样子 { ", &qu ...