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 ...
随机推荐
- oracle 11g ocr 冗余配置
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/royjj/article/details/30506343 oracle 11g ocr 冗余 ...
- pip安装时使用国内源加快下载速度
国内源: 清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 h ...
- ubuntu nohup命令用法
让程序在后台运行 该命令的一般形式nohup command & 程序在后台运行并打印日志 nohup ./china_fund.py > china_fund.file 2>&a ...
- 9patch图片
9patch图片可直接缩放,放在drawable文件夹下就可以 右边和下边指定内容区域
- Android RelativeLayout相对布局
RelativeLayout是相对布局控件:以控件之间相对位置或相对父容器位置进行排列. 相对布局常用属性: 子类控件相对子类控件:值是另外一个控件的id android:layout_above-- ...
- 使用valgrind进行内存泄漏和非法内存操作检测
valgrind是一个强大的工具,最常用的功能是用它来检测内存泄漏和非法内存的使用.要想让valgrind报告的更加细致,请使用-g进行编译. 基本命令如下: $ valgrind --tool=me ...
- PYTHON 爬虫笔记八:利用Requests+正则表达式爬取猫眼电影top100(实战项目一)
利用Requests+正则表达式爬取猫眼电影top100 目标站点分析 流程框架 爬虫实战 使用requests库获取top100首页: import requests def get_one_pag ...
- [原创]Java给word中的table赋值
一.准备工作: 下载PageOffice for Java:http://www.zhuozhengsoft.com/dowm/ 二. 实现方法: 要调用PageOffice操作Word中的tabl ...
- linux命令学习笔记(25):linux文件属性详解
Linux 文件或目录的属性主要包括:文件或目录的节点.种类.权限模式.链接数量.所归属的用户和用户组. 最近访问或修改的时间等内容.具体情况如下: 命令: ls -lih 输出: [root@loc ...
- Linux网络编程 gethostbyaddr()
C语言函数 概述: 返回对应于给定地址的主机信息. #include <winsock.h> struct hostent FAR *PASCAL FAR gethostbyaddr(co ...