CF33C Wonderful Randomized Sum 题解
简要题意:
你可以无限次的把该数组的一个前缀和后缀 \(\times -1\),问最终的最大序列和。
这题盲目WA了数次才知道本质
这题89个数据吊打std
CF真好啊,发现一个错后面就不测了
下面,就以我艰辛的思维历程来构造本篇博客。
算法一
盲猜:所有数都可以变成正数。
然后绝对值相加。
连样例也没测。
然后,\(\frac{2}{89} pts\). 只过了前两个样例,第三个就死了。
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
inline int read(){char ch=getchar();int f=1;while(ch<'0' || ch>'9') {if(ch=='-') f=-f; ch=getchar();}
int x=0;while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x*f;}
int main(){
int n=read(),s=0; while(n--) {
int t=read();
s+=abs(t);
} printf("%d\n",s);
return 0;
}
算法二
突然发现不符合样例!
仔细想了以下,嗯嗯,似乎只有开始的前一段负数和最后的后一段负数可以改变。
然后若有所思的写下一段代码。
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+1;
inline int read(){char ch=getchar();int f=1;while(ch<'0' || ch>'9') {if(ch=='-') f=-f; ch=getchar();}
int x=0;while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x*f;}
int n,a[N];
int s=0;
int main(){
n=read();
for(int i=1;i<=n;i++) a[i]=read();
for(int i=1;i<=n;i++)
if(a[i]<0) a[i]=-a[i];
else break; //前一段
for(int i=n;i>=1;i--)
if(a[i]<0) a[i]=-a[i];
else break; //后一段
for(int i=1;i<=n;i++) s+=a[i];
printf("%d\n",s);
return 0;
}
交上去,发现得了 \(\frac{6}{89}\) 分。
发现 \(a_i = 0\) 有点问题。
算法三
\(a_i = 0\)?然后加了几个等号。
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+1;
inline int read(){char ch=getchar();int f=1;while(ch<'0' || ch>'9') {if(ch=='-') f=-f; ch=getchar();}
int x=0;while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x*f;}
int n,a[N];
int s=0;
int main(){
n=read();
for(int i=1;i<=n;i++) a[i]=read();
for(int i=1;i<=n;i++)
if(a[i]<=0) a[i]=-a[i];
else break;
for(int i=n;i>=1;i--)
if(a[i]<=0) a[i]=-a[i];
else break;
for(int i=1;i<=n;i++) s+=a[i];
printf("%d\n",s);
return 0;
}
然后得了 \(\frac{8}{89}\) 分。
我却,我答案是负数,它答案是正数
算法四
真正感到自己 脑抽了 挺坚强的。
其实呢,我们还是要重视它,毕竟是 \(C\) 吗。(其实也不难)
你想,比方说前一段是 \(A\),后一段是 \(C\),重叠是 \(B\),一共是 \(S\).(\(\emptyset = 0\))
(指前缀、后缀、重叠部分、总部分的和)
此时答案为:
\]
然而:
\]
(这是因为,中间一段经过两次之后没变,所以还是加上)
所以答案变形为:
\]
显然让 \(C\) 越大越好。
那答案不就摆在面前了?
Dev-c++:那你还调试那么多次
因为,\(C\) 肯定是连续的一段并且你可以随便的取,所以:
\]
哎呀,激动地写了个程序。
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+1;
inline int read(){char ch=getchar();int f=1;while(ch<'0' || ch>'9') {if(ch=='-') f=-f; ch=getchar();}
int x=0;while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x*f;}
int n,a[N];
int s=0,f[N];
int main(){
n=read();
for(int i=1;i<=n;i++) a[i]=read(),s+=a[i];
int ans=a[1]; f[1]=a[1];
for(int i=2;i<=n;i++) f[i]=max(f[i-1]+a[i],a[i]),ans=max(ans,f[i]);
printf("%d\n",ans*2-s);
return 0;
}
看上去没啥问题,然后得了 \(0pt\).
原因: \(\texttt{ans}\) 的初值应该是:
max(a[1],0)
导致第一个样例去世,然后全军覆没~
算法五
终于算是拨云见雾了,结果在临近 \(\texttt{AC}\) 的时候因为初值掉坑。
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+1;
inline int read(){char ch=getchar();int f=1;while(ch<'0' || ch>'9') {if(ch=='-') f=-f; ch=getchar();}
int x=0;while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x*f;}
int n,a[N];
int s=0,f[N];
int main(){
n=read();
for(int i=1;i<=n;i++) a[i]=read(),s+=a[i];
int ans=max(a[1],0); f[1]=a[1];
for(int i=2;i<=n;i++) f[i]=max(f[i-1]+a[i],a[i]),ans=max(ans,f[i]);
printf("%d\n",ans*2-s);
return 0;
}
终于 \(\text{AC}\) 了。时间复杂度:\(O(n)\).
CF33C Wonderful Randomized Sum 题解的更多相关文章
- 【极值问题】【CF33C】 Wonderful Randomized Sum
传送门 Description 给你一个数列\(A\),你可以选择任意一个前缀和任意一个后缀,前缀后缀可重合.给他们乘\(-1\).求最大能获得的序列和. Input 第一行是一个数\(n\)代表数列 ...
- Ural 1248 Sequence Sum 题解
目录 Ural 1248 Sequence Sum 题解 题意 题解 程序 Ural 1248 Sequence Sum 题解 题意 给定\(n\)个用科学计数法表示的实数\((10^{-100}\s ...
- LeetCode Continuous Subarray Sum 题解 同余前缀和 Hash表
文章目录 题意 思路 特殊情况k=0 Source Code 1 Source Code 2 题意 给定一个数组和一个整数k,返回是否存在一个长度至少为2的连续子数组的和为k的倍数. 思路 和上一篇博 ...
- Hdoj 1003.Max Sum 题解
Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum ...
- [LeetCode]Combination Sum题解(DFS)
Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T), f ...
- [LeetCode] Three Sum题解
Three Sum: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? ...
- 01Two Sum题解
Tow Sum 原题概述: Given an array of integers, return indices of the two numbers such that they add up to ...
- 【CF1445D】Divide and Sum 题解
题目链接 题意简介 将一个长度为 2n 的数列平均分为两个子数列 p 和 q 后,p 按从小到大排序,q 按从大到小排序. 排序后,记 p 为 \(\{x_i\}\) ,q 为 \(\{y_i\}\) ...
- BZOJ3155:Preprefix sum——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3155 最朴素的想法是两棵树状数组,一个记录前缀和,一个记录前缀前缀和,但是第二个我们非常不好修改 ...
随机推荐
- Python的Flask框架开发RESTful API
web框架选择 Django,流行但是笨重,还麻烦,人生苦短,肯定不选 web.py,轻量,但据说作者仙逝无人维护,好吧,先pass tornado,据说倡导自己造轮子,虽然是facebook开源的吧 ...
- 查漏补缺:进程间通信(IPC):管道
管道是UNIX系统IPC的最古老形式,所有UNIX系统都提供此种通信机制.管道有以下两种局限性: (1)历史上,管道是半双工的(即数据只能在一个方向上流动). (2)管道只能在具有公共先祖的两个进程之 ...
- 安装Redis内存分析工具rdbtools
一.安装Python2.7 1. wget http://10.12.29.98:8090/tools/Python-2.7.11.tgz 2. ln -s /usr/local/python2.7/ ...
- Ubuntu 16.04 PXE+kickstart部署系统
#PXE+TFTP+Kickstart 自动部署服务器系统系统Ubuntu16.04apt-get install isc-dhcp-servervim /etc/default/isc-dhcp-s ...
- Autowired和Resource区别
@Autowired和@Resource熟悉吧?是不是经常复制粘贴顺手就来,两者都是用来给成员变量自动装载,可是它俩到底有啥区别呢? 1.@Autowired与@Resource都可以用来装配bean ...
- nginx图片过滤处理模块http_image_filter_module安装配置
http_image_filter_module是nginx提供的集成图片处理模块,支持nginx-0.7.54以后的版本,在网站访问量不是很高磁盘有限不想生成多余的图片文件的前提下可,就可以用它实时 ...
- 记一次MySQL表分区操作
最近一次日常迭代中,业务线需要对一张大表进行联合查询,查询性能可想而知,测试过程中服务接口直接响应超时,导致服务不可用,最后临时对该表进行分区操作,暂时缓解性能问题.由于是第一次操作表分区,姑且记录一 ...
- Python2.7错误处理FileNotFoundError报错NameError: name 'FileNotFoundError' is not defined
错误信息如下: 原因是FileNotFoundError是python3.0中的写法,而Python2.7中应写为IOError.
- 美团CodeM 资格赛第一题
美团外卖的品牌代言人袋鼠先生最近正在进行音乐研究.他有两段音频,每段音频是一个表示音高的序列.现在袋鼠先生想要在第二段音频中找出与第一段音频最相近的部分. 具体地说,就是在第二段音频中找到一个长度和第 ...
- 小程序在ios10.2系统上兼容
1. 定位元素在ios10.2系统上出现样式问题??? 没错,就是在测试在侧道ios10.2系统时发现了样式错误的问题,比如一个Swiper中,最后一个展示有问题. 这是啥原因❓❓❓❓❓❓ 大写的问 ...