SEERC 2006 Subsequence, 高效算法设计 ,LA 2678
来源:互联网 发布:手机理财软件安全吗 编辑:程序博客网 时间:2024/06/05 04:09
题目:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=679
详见注释:
#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<climits>#include<queue>#include<vector>#include<map>#include<sstream>#include<set>#include<stack>#include<cctype>#include<utility>#pragma comment(linker, "/STACK:102400000,102400000")#define PI 3.1415926535897932384626#define eps 1e-10#define sqr(x) ((x)*(x))#define FOR0(i,n) for(int i=0 ;i<(n) ;i++)#define FOR1(i,n) for(int i=1 ;i<=(n) ;i++)#define FORD(i,n) for(int i=(n) ;i>=0 ;i--)#define lson num<<1,le,mid#define rson num<<1|1,mid+1,ri#define MID int mid=(le+ri)>>1#define zero(x)((x>0? x:-x)<1e-15)#define mk make_pair#define _f first#define _s secondusing namespace std;//const int INF= ;typedef long long ll;//const ll inf =1000000000000000;//1e15;//ifstream fin("input.txt");//ofstream fout("output.txt");//fin.close();//fout.close();//freopen("a.in","r",stdin);//freopen("a.out","w",stdout);const int INF =0x3f3f3f3f;const int maxn=100000+20 ;//const int maxm= ;//by yskysker123ll a[maxn];int n ;ll S;ll sum;//表示某一段的和,后有注释,一开始我开的是sum[maxn]int lef;// 和lef[maxn],后来发现根据计算的顺序,没必要,减了点空间复杂度。不过与AC应该无关。int dp[maxn];//dp[x]记录以第x个数结尾,满足要求的最短连续序列 长度。//int st[maxn];int start;void cope(int x){ sum+=a[x]; int i; for( i=lef;sum-a[i]>=S ;sum-=a[i],i++); lef=i;dp[x]= x-i+1;}int main(){ while(scanf("%d%lld",&n,&S)==2) { for(int i=1;i<=n;i++) scanf("%lld",&a[i]); start=0;sum=0;//start表示从1到第start个数开始 ,∑(a)>=S; for(int i=1;i<=n;i++) { sum+=a[i]; if(sum>S) {start=i;break;} } if(!start) {puts("0");continue;} dp[start]=start;//这里确定sum的初值 int ans=start;lef=1;//ans为最终结果,lef是变化的,一开始是1,随后不断右移。 for(int i=start+1;i<=n;i++) { cope(i); ans=min(ans,dp[i]); } printf("%d\n",ans);//整个算法时间复杂度预估为O(n),大约2*n; } return 0;}
0 0
- SEERC 2006 Subsequence, 高效算法设计 ,LA 2678
- UVaLive 2678(LA)Subsequence SEERC 2006 题解
- 高效算法设计专项:LA 2689
- 高效算法设计专项:LA 4726
- 高效算法设计专项:LA 2963
- 高效算法设计专项:LA 4356
- LA 2678 Subsequence
- LA 2678 – Subsequence
- LA 2678 Subsequence
- LA 2678(p48)----Subsequence
- LA 2678 Subsequence .
- LA 2678 UVA 1121 - Subsequence
- SEERC 2004,LA 3029最大子矩阵
- SEERC 2006 / UVa 1121 Subsequence (有和下界的最短连续子序列)
- LA 2678 Subsequence(尺取法)
- UVA 1330 LA 3029 seerc 2004 - City Game
- UVaLive(LA) 3029 City Game,SEERC 2004题解
- 高效算法设计
- *LeetCode-Multiply Strings
- 意图(Intent)讲解4:安卓使用意图(Intent)静态变量方法传递数据
- Spark执行样例报警告:WARN scheduler.TaskSchedulerImpl: Initial job has not accepted any resources
- Xcode7 &&iOS9一些新特性 记录
- 八卦CSDN博客排名
- SEERC 2006 Subsequence, 高效算法设计 ,LA 2678
- 树莓派raspberry pi安装archlinux,并且在上面搭建以太坊(ethereum)环境
- 最小生成树(poj1251 poj1861 poj1789)
- c语言的二级别指针
- INotifyPropertyChanged 接口
- ajax
- 二维数组的传参
- 《构建之法》第十四章 质量保证
- JavaScript控制元素CSS