题目链接: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)的更多相关文章

  1. CSP2019知识点整理

    也算是接下来二十天的复习计划吧 仅止于联赛难度左右 基础算法 字符串 char[] cstring memset() 输入无& gets(), fgets(stdin, ,); strcmp, ...

  2. STL入门--sort,lower_bound,upper_bound,binary_search及常见错误

    首先,先定义数组 int a[10]; 这是今天的主角. 这四个函数都是在数组上操作的 注意要包含头文件 #include<algorithm> sort: sort(a,a+10) 对十 ...

  3. sort排序使用以及lower_bound( )和upper_bound( )

    sort()原型: sort(first_pointer,first_pointer+n,cmp) 排序区间是[first_pointer,first_pointer+n)      左闭右开 参数1 ...

  4. 【刷题记录】 && 【算法杂谈】折半枚举与upper_bound 和 lower_bound

    [什么是upper_bound 和 lower_bound] 简单来说lower_bound就是你给他一个非递减数列[first,last)和x,它给你返回非递减序列[first, last)中的第一 ...

  5. lower_bound 和 upper_bound

    Return iterator to lower bound Returns an iterator pointing to the first element in the range [first ...

  6. POJ-2785 4 Values whose Sum is 0(折半枚举 sort + 二分)

    题目链接:http://poj.org/problem?id=2785 题意是给你4个数列.要从每个数列中各取一个数,使得四个数的sum为0,求出这样的组合的情况个数. 其中一个数列有多个相同的数字时 ...

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

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

  9. 从零开始学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 ...

随机推荐

  1. selector + drawable 多状态图形

    select_drawble.xml<?xml version="1.0" encoding="utf-8"?> <selector xmln ...

  2. Database: key

    super-key: Any key that has more columns than necessary to uniquely identify each row in the table i ...

  3. 《CSS权威指南(第三版)》---第五章 字体

    这章主要的内容有: 1.字体:一般使用一种通用的字体. 2.字体加粗:一般从数字100 -900 . 3.字体大小:font-size 4.拉伸和调整字体:font-stretch 5.调整字体大小: ...

  4. 计算机中丢失OPENGL.dll

    开发OpenGL项目时,在VS开发环境下可能会出现如图所示的错误. 在c:\windows\system32和SysWow64文件夹下存在opengl32.dll,此时,所写程序能够正常编译,但是,程 ...

  5. Mockito @BeforeClass @BeforeMethod @BeforeTest 的生命周期

    @BeforeClass---@AfterClass 类实例化前, 被执行, 主要用于设置环境变量等, 与SpringTestContext结合用的时候要注意, 这种情况下@autowire的bean ...

  6. 利用jsp技术实现用户注册

    利用jsp技术实现用户注册,包含register.html和register_check.jsp页面代码​1. [代码]J2EE实验    <!DOCTYPE html PUBLIC " ...

  7. Win7、Win8、Win10始终以管理员身份运行程序。

    在Win7.Win8.Win10系统中,以管理员身份运行程序很麻烦,一般有以下几种方式: 1.在可执行程序或快捷方式上右键,以管理员身份运行: 2.在可执行程序或快捷方式上右键->属性-> ...

  8. .NET中Eval()方法大全

    <%# Bind("Subject") %> //绑定字段<%# Container.DataItemIndex + 1%> //实现自动编号<%# ...

  9. web性能压力测试工具http_load/webbench/ad

    http_load 下载地址:http://www.acme.com/software/http_load/http_load-12mar2006.tar.gz 程序非常小,解压后也不到100K 居家 ...

  10. hdu-5857 Median(水题)

    题目链接: Median Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...