1044. Shopping in Mars
来源:互联网 发布:贵定浪人网络客服 编辑:程序博客网 时间:2024/04/28 21:55
Two points (proposed to be low and high )both start to point the begining of the array,
If current subsequence sum is more than the need sum,low++;
If current subsequence sum is less than the need sum,high++;
If current subsequence sum is equal to the need sum,print the current subsequence;
At the end,if we can't find the corresponding result is equal to the need sum,just find the minimum sum(minimumMax) of the subsequence which is more than the need sum.
Then,search the subsequence which is equal to minimumMax as previously described.
// 1044. Shopping in Mars.cpp: 主项目文件。#include "stdafx.h"#include <cstdio>const int INF=0x7fffffff;const int N=100003;int diamond[N];int arrNum,needSum;bool selectChain(){bool tag=false;int low=0,high=0,minimumMax=INF,curSum=diamond[low];while(low<arrNum&&high<arrNum&&low<=high){if(curSum>needSum){if(curSum<minimumMax)minimumMax=curSum;}if(low==high&&curSum>needSum){low++,high++;curSum=diamond[low];continue;}if(curSum==needSum){tag=true;printf("%d-%d\n",low+1,high+1);curSum=curSum-diamond[low++]+diamond[++high];}else if(curSum<needSum){high++;curSum+=diamond[high];}else{curSum-=diamond[low];low++;}}needSum=minimumMax;return tag;}int main(){scanf("%d%d",&arrNum,&needSum);for(int i=0;i<arrNum;i++)scanf("%d",diamond+i);bool tag=selectChain();if(tag)return 0;elseselectChain();return 0;}
- 1044. Shopping in Mars
- 1044. Shopping in Mars
- 1044. Shopping in Mars
- 1044. Shopping in Mars
- 1044. Shopping in Mars
- 1044. Shopping in Mars
- 1044. Shopping in Mars
- 1044. Shopping in Mars
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- PAT 1044. Shopping in Mars
- 1044. Shopping in Mars (25)
- PAT 1044. Shopping in Mars
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- Oracle 10g RAC 启动与关闭
- 禁止Form窗口调整大小,禁止combobox输入,关闭窗口的_ 口 X
- 两分钟学会在GitHub托管代码
- PLSQL Developer怎么查看一个表的建表语句
- 关于string 和 char * 区别
- 1044. Shopping in Mars
- Redhat 5.4 Orcle RAC 数据库 从10.2.0.1升级到 10.2.0.4
- Android SDK 中的 SDK Manager.exe 无法启动、一闪而过的解决办法
- mmc无法创建管理单元的解决方法
- 在jsavaScript中设置select的某个选项被选中
- Nginx源码剖析之内存池和内存管理
- android View.setTag()与View.getTag()的作用
- 空间点过程与随机测度(一):从数星星说起
- web前端构架师