51nod1065(set.upper_bound()/sort)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1065
题意:中文题诶~
思路:
解法1:set容器,将所有前缀和存储到set和sum数组里,再用set.upper_bound()查找sum[i]后面第一个大于sum[i]的元素,那么他们的差就是第i个元素开头的最小正子段和.然后再将sum[i]从set里面删除,不然会影响后面的查询嘛.遍历所有i就得到我们要的答案啦;
代码:
#include <bits/stdc++.h>
#define ll long long
#define MAXN 50010
using namespace std; const int inf=0x3f3f3f3f;
ll sum[MAXN];
set<ll> st; int main(void){
int n;
ll x, ans=inf;
scanf("%d", &n);
for(int i=; i<=n; i++){
scanf("%lld", &x);
sum[i]=sum[i-]+x;
st.insert(sum[i]);
}
st.insert();
set<ll>::iterator it;
for(int i=; i<n; i++){
it=st.upper_bound(sum[i]);
if(it!=st.end()){
ans=min(ans, *it-sum[i]);
}
st.erase(sum[i]);
}
cout << ans << endl;
return ;
}
解法2:将前缀和及其下标存储到pair对组(结构题也行啦)里;再以前缀和为权值sort一下,那么我们不难想到如果p[j].first>p[i].first&&p[j].second>p[i].second --条件1 的话p[j].fisrt-p[i].first 就是一段和为正数的子段的和.又因为我们已经给p排过序了,所以i,j越接近,那么得到的子段和就越小,很自然我们会想到j=i+1的情况.问题是对于j=i+1的情况上述条件1一定满足么?或者说最优解一定是来自j=i+1的情况里么?
答案是肯定的,并且我们不难证明它:我们假设排序后顺序为 i, k, j,i和j满足条件1,i和k 不满足条件1, 即:
p[j].second>p[i].second,p[k].second<p[i].second, 所以有:p[j].second>p[k].second,很显然k和j满足条件1并且k和j能比i和j产生更优的解;
所以我们只要考虑j=i+1的情况即可.
此外我们还要注意两点:1.我们排序后得到是p[i+1].first>=p[i].first 而非 p[i+1].first>p[i].first;
2.我们还要考虑p[i].first本身的值,即从第1个元素到第i个元素的和的情况
代码:
#include <bits/stdc++.h>
#define ll long long
#define MAXN 50010
using namespace std; const int inf=0x3f3f3f3f;
pair<ll, int> p[MAXN]; int main(void){
int n;
ll x, ans=inf;
scanf("%d", &n);
for(int i=; i<=n; i++){
scanf("%lld", &x);
p[i].first=p[i-].first+x;
p[i].second=i;
}
sort(p, p+n+);
for(int i=; i<n; i++){
if(p[i].first>){
ans=min(ans, p[i].first);
}
if(p[i+].second>p[i].second&&p[i+].first>p[i].first){
ans=min(ans, p[i+].first-p[i].first);
}
}
cout << ans << endl;
return ;
}
51nod1065(set.upper_bound()/sort)的更多相关文章
- CSP2019知识点整理
也算是接下来二十天的复习计划吧 仅止于联赛难度左右 基础算法 字符串 char[] cstring memset() 输入无& gets(), fgets(stdin, ,); strcmp, ...
- STL入门--sort,lower_bound,upper_bound,binary_search及常见错误
首先,先定义数组 int a[10]; 这是今天的主角. 这四个函数都是在数组上操作的 注意要包含头文件 #include<algorithm> sort: sort(a,a+10) 对十 ...
- sort排序使用以及lower_bound( )和upper_bound( )
sort()原型: sort(first_pointer,first_pointer+n,cmp) 排序区间是[first_pointer,first_pointer+n) 左闭右开 参数1 ...
- 【刷题记录】 && 【算法杂谈】折半枚举与upper_bound 和 lower_bound
[什么是upper_bound 和 lower_bound] 简单来说lower_bound就是你给他一个非递减数列[first,last)和x,它给你返回非递减序列[first, last)中的第一 ...
- lower_bound 和 upper_bound
Return iterator to lower bound Returns an iterator pointing to the first element in the range [first ...
- POJ-2785 4 Values whose Sum is 0(折半枚举 sort + 二分)
题目链接:http://poj.org/problem?id=2785 题意是给你4个数列.要从每个数列中各取一个数,使得四个数的sum为0,求出这样的组合的情况个数. 其中一个数列有多个相同的数字时 ...
- Codeforces Beta Round #12 (Div 2 Only) D. Ball sort/map
D. Ball Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/12/D D ...
- Codeforces Round #198 (Div. 2) D. Bubble Sort Graph (转化为最长非降子序列)
D. Bubble Sort Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 从零开始学C++之STL(七):剩下5种算法代码分析与使用示例(remove 、rotate 、sort、lower_bound、accumulate)
一.移除性算法 (remove) C++ Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ...
随机推荐
- ABap-小技巧
if FIELD cn '0123456789'. *&如果字符串包含‘数字’ STOP. endif. 同理到字母‘ABCDEFG*’ 'abcdefg*' '/' '\' 等其它字 ...
- PYTHON调用C接口(基于Ctypes)实现stein算法最大公约数的计算
相关环境配置 mingw,选择相应的32位.64位的版本,主要用于编译动态链接库dll文件,可用vs替代,这里我选择轻量级的mingw windows64位地址:https://sourceforge ...
- Android/iOS Remote debugging
简单介绍 使用下面方法可以定位webview中的元素,无法定位view中的元素. 原文地址:http://mp.weixin.qq.com/s/y_UfdgjT_pkKgYivJmqt7Q webvi ...
- Machine Learning No.2: Linear Regression with Multiple Variables
1. notation: n = number of features x(i) = input (features) of ith training example = value of feat ...
- 白话 P-value
准备再尝试一下,用大白话叙述一遍统计推断中最基础的东西(假设检验.P值.……),算是把这段时间的阅读和思考做个梳理(东西不难,思考侧重在如何表述和展示).这次打算用一种“迂回的”表达方式,比如,本文从 ...
- Redis高可用部署及监控
Redis高可用部署及监控 目录 一.Redis Sentinel简介 二.硬件需求 三.拓扑结构 .单M-S结构 .双M-S结构 .优劣对比 四.配置部 ...
- 前端多媒体(4)—— video标签全面分析
测试地址:https://young-cowboy.github.io/gallery/html5_video/index.html 属性 一些属性是只读的,一些属性是可以修改从而影响视频播放的. a ...
- nginx的fastcgi_param参数详解
在配置nginx的时候,有很多fastcgi_param参数,具体对应是什么值呢 引入:fastcgi_params 文件: fastcgi_params文件具体内容: postman 发送请求: n ...
- 理解YOLOv2训练过程中输出参数含义
原英文地址: https://timebutt.github.io/static/understanding-yolov2-training-output/ 最近有人问起在YOLOv2训练过程中输出在 ...
- ZJOI2012题解
t1灾难 给一个食物网 如果一个生物吃的所有东西都灭绝了 它也跟着灭绝 求每个生物灭绝时跟着灭绝的生物数量 支配树裸题,我们先拓扑排序,然后建立一棵树满足一个点灭绝时,有且仅有它的子树跟着灭绝 考虑如 ...