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\) 与 ...
随机推荐
- centos6.5 mysql 5.6修改root密码,以及创建用户并授权
mkdir -p mysql_home/{data,temp,undologs,logs} chown -R mysql:mysql /dbfiles/mysql_home mysql_install ...
- ubuntu的ufw如何开放特定端口?
ubuntu的ufw如何开放特定端口? 1.安装 sudo apt-get install ufw 2.开启 sudo ufw enable 默认关闭外部访问 sudo ufw default den ...
- collectd+logstash+influxdb+grafana构建windows服务器应用监控系统
一.背景介绍 本监控方案支持对Windows Server服务器集群的全面监控,方案提供丰富的图表展示, 以及对异常问题进行邮件的实时报警. 本系统由Collectd(操作系统数据搜集).logsta ...
- linux应用之php开发环境lamp搭建(centos)
搭建linux+apache+mysql+php环境 1.安装apache: yum install httpd httpd-devel 启动apache: /etc/init.d/httpd ...
- 运行swoole_server方法
运行 php 文件 server.php 运行结果是如下: 只是服务器开启了 如果想看客户端连接的情况 可以测试一下 从新连接一个连接 用命令 方式 telnet 127.0.0.1 9501 这个9 ...
- linguistic相关
Knowing a word means knowing both its sound and its meaning, while being able to use a word requires ...
- SPOJ:D-query(非常规主席树求区间不同数的个数)
Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) ...
- WinThruster中文版破解方法(注册表无伤清理工具)
每次卸载完软件,都会有注册表残余垃圾,久而久之电脑会越来越慢,winThruster可以检测出系统无用注册表,并删除. 1.解压文件,安装Setup_WinThruster_2015.exe文件. 2 ...
- CentOS 6.5远程连接工具x shell
安装X shell 在Window系统下远程连接Linux,x shell只是一种远程连接工具,类似工具还有CRT.VNC.putty. 以下是安装X shell的注意事项 此选项中,如不把——初始数 ...
- Indexed DB入门导学(1) – 51CTO.COM
在html 5中,其中一个引人注意的新特性,那就是允许使用Indexed DB.… 查阅全文 ›