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)

所以,观察到,这个观察到,到底需要多大的脑洞呢?

暴力枚举任意一对相邻的减号,只有其里面包括的数字全部变成负数为代价,使得后面的数字全部变正。

暴力枚举即可。

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <assert.h>
  7. #define IOS ios::sync_with_stdio(false)
  8. using namespace std;
  9. #define inf (0x3f3f3f3f)
  10. typedef long long int LL;
  11.  
  12. #include <iostream>
  13. #include <sstream>
  14. #include <vector>
  15. #include <set>
  16. #include <map>
  17. #include <queue>
  18. #include <string>
  19. #include <bitset>
  20. const int maxn = 1e5 + ;
  21. LL perfixSum[maxn], absSum[maxn];
  22. vector<int>pos;
  23. void cut(LL &val, int pos1, int pos2) {
  24. if (pos1 > pos2) return;
  25. val -= absSum[pos2] - absSum[pos1 - ];
  26. }
  27. void work() {
  28. int n;
  29. scanf("%d", &n);
  30. int val;
  31. scanf("%d", &val);
  32. perfixSum[] = val;
  33. absSum[] = val;
  34. for (int i = ; i <= n; ++i) {
  35. int val;
  36. char op;
  37. cin >> op;
  38. scanf("%d", &val);
  39. if (op == '+') {
  40. perfixSum[i] = perfixSum[i - ] + val;
  41. } else {
  42. perfixSum[i] = perfixSum[i - ] - val;
  43. pos.push_back(i);
  44. }
  45. absSum[i] = absSum[i - ] + val;
  46. }
  47. LL ans = perfixSum[n];
  48. for (int i = ; i <= (int)pos.size() - ; ++i) {
  49. int p1 = pos[i], p2 = pos[i + ];
  50. LL tans = absSum[n] - absSum[p2 - ];
  51. tans += perfixSum[p1];
  52. cut(tans, p1 + , p2 - );
  53. ans = max(ans, tans);
  54. }
  55. cout << ans << endl;
  56. }
  57.  
  58. int main() {
  59. #ifdef local
  60. freopen("data.txt", "r", stdin);
  61. // freopen("data.txt", "w", stdout);
  62. #endif
  63. // int val;
  64. // scanf("%d", &val);
  65. // cout << val << endl;
  66. work();
  67. return ;
  68. }

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. centos6.5 mysql 5.6修改root密码,以及创建用户并授权

    mkdir -p mysql_home/{data,temp,undologs,logs} chown -R mysql:mysql /dbfiles/mysql_home mysql_install ...

  2. ubuntu的ufw如何开放特定端口?

    ubuntu的ufw如何开放特定端口? 1.安装 sudo apt-get install ufw 2.开启 sudo ufw enable 默认关闭外部访问 sudo ufw default den ...

  3. collectd+logstash+influxdb+grafana构建windows服务器应用监控系统

    一.背景介绍 本监控方案支持对Windows Server服务器集群的全面监控,方案提供丰富的图表展示, 以及对异常问题进行邮件的实时报警. 本系统由Collectd(操作系统数据搜集).logsta ...

  4. linux应用之php开发环境lamp搭建(centos)

    搭建linux+apache+mysql+php环境   1.安装apache: yum install httpd httpd-devel  启动apache: /etc/init.d/httpd ...

  5. 运行swoole_server方法

    运行 php 文件 server.php 运行结果是如下: 只是服务器开启了 如果想看客户端连接的情况 可以测试一下 从新连接一个连接 用命令 方式 telnet 127.0.0.1 9501 这个9 ...

  6. linguistic相关

    Knowing a word means knowing both its sound and its meaning, while being able to use a word requires ...

  7. 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) ...

  8. WinThruster中文版破解方法(注册表无伤清理工具)

    每次卸载完软件,都会有注册表残余垃圾,久而久之电脑会越来越慢,winThruster可以检测出系统无用注册表,并删除. 1.解压文件,安装Setup_WinThruster_2015.exe文件. 2 ...

  9. CentOS 6.5远程连接工具x shell

    安装X shell 在Window系统下远程连接Linux,x shell只是一种远程连接工具,类似工具还有CRT.VNC.putty. 以下是安装X shell的注意事项 此选项中,如不把——初始数 ...

  10. Indexed DB入门导学(1) – 51CTO.COM

    在html 5中,其中一个引人注意的新特性,那就是允许使用Indexed DB.… 查阅全文 ›