1044. Shopping in Mars (25)
分析:
考察二分,简单模拟会超时,优化后时间正好,但二分速度快些,注意以下几点:
(1):如果一个序列D1 ... Dn,如果我们计算Di到Dj的和, 那么我们可以计算D1到Dj的和sum1,D1到Di的和sum2, 然后结果就是sum1 - sum2;
(2): 那么我们二分则要搜索的就是m + sum[i]的值。
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <cctype>
#include <iomanip>
#include <cmath>
#include <map> using namespace std; const int Max_Int = 0x7fffffff;
const int Max_required = ; int sum[Max_required];
int value[Max_required]; struct Node
{
int start;
int end;
};
vector<Node> V_node; int binary_find(int target, int n)
{
int low = , high = n; while (low <= high)
{
int mid = (low + high) >> ;
if (sum[mid] == target)
return mid;
else if (sum[mid] < target)
low = mid + ;
else high = mid - ;
}
return low;
} int main()
{
int n, m;
while (scanf("%d%d", &n, &m) != EOF)
{
for (int i = ; i <= n; i++)
{
scanf("%d", &value[i]);
sum[i] = sum[i - ] + value[i];
} int Min = Max_Int;
for (int i = ; i <= n; i++)
{
int target = m + sum[i];
int res = binary_find(target, n);
//printf("res = %d\n", res);
if (sum[res] - sum[i] - m >= && sum[res] - sum[i] - m <= Min)
{
if (sum[res] - sum[i] - m < Min)
{
V_node.clear();
Min = sum[res] - sum[i] - m;
Node node;
node.start = i + ;
node.end = res;
V_node.push_back(node);
}
else if (sum[res] - sum[i] - m == Min)
{
Node node;
node.start = i + ;
node.end = res;
V_node.push_back(node);
} }
} for (int i = ; i < V_node.size(); i++)
printf("%d-%d\n", V_node[i].start, V_node[i].end);
}
return ;
}
1044. Shopping in Mars (25)的更多相关文章
- PAT 甲级 1044 Shopping in Mars (25 分)(滑动窗口,尺取法,也可二分)
1044 Shopping in Mars (25 分) Shopping in Mars is quite a different experience. The Mars people pay ...
- 1044 Shopping in Mars (25 分)
Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...
- PAT Advanced 1044 Shopping in Mars (25) [⼆分查找]
题目 Shopping in Mars is quite a diferent experience. The Mars people pay by chained diamonds. Each di ...
- 1044 Shopping in Mars (25分)(二分查找)
Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...
- PAT (Advanced Level) 1044. Shopping in Mars (25)
双指针. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT甲题题解-1044. Shopping in Mars (25)-水题
n,m然后给出n个数让你求所有存在的区间[l,r],使得a[l]~a[r]的和为m并且按l的大小顺序输出对应区间.如果不存在和为m的区间段,则输出a[l]~a[r]-m最小的区间段方案. 如果两层fo ...
- 【PAT甲级】1044 Shopping in Mars (25 分)(前缀和,双指针)
题意: 输入一个正整数N和M(N<=1e5,M<=1e8),接下来输入N个正整数(<=1e3),按照升序输出"i-j",i~j的和等于M或者是最小的大于M的数段. ...
- pat1044. Shopping in Mars (25)
1044. Shopping in Mars (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Shop ...
- 1044 Shopping in Mars (25 分)
1044 Shopping in Mars (25 分) Shopping in Mars is quite a different experience. The Mars people pay b ...
随机推荐
- haproxy+keepalived实现高可用负载均衡
软件负载均衡一般通过两种方式来实现:基于操作系统的软负载实现和基于第三方应用的软负载实现.LVS就是基于Linux操作系统实现的一种软负载,HAProxy就是开源的并且基于第三应用实现的软负载. HA ...
- Json操作(DynamicJson)
Json的简介 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了 ...
- less笔记
koala工具 注释: 1./**/,可以被编译 2.//,不可以被编译 申明变量: @box_width:300px; .box{ width:@box_wid ...
- go 的 protoc 插件调用逻辑
要让protoc使用插件,需要做下面事情: Place the plugin binary somewhere in the PATH and give it the name "proto ...
- 【练习】数据移动---parfile导出表中指定行:
要求: ①创建存放数据的文件: ②使用默认的bad文件生成方式: ③使用truncate选项方式. 1.准备条件: [oracle@host03 ~]$ mkdir datadump [oracle@ ...
- 苹果手机制作gif图片
前一段介绍了一款很好用的在模拟器上录制gif图片的工具licecap(地址:http://www.cnblogs.com/10-19-92/p/5593785.html), 但是licecap不能使用 ...
- 【Tree 2】树形结构数据呈现的非递归算法(循环)实现
一.基本概况 上一篇博客介绍到用递归实现树结构数据的查找,那么这篇博客,我就结合自己对于树的理解,然后用一种非递归的方式进行树结构数据的处理.首先,改造数据库表设计,加入度的概念: 首先,layer的 ...
- Linux下find命令
转自:http://www.cnblogs.com/skynet/archive/2010/12/25/1916873.html 1.1.find命令的一般形式 man文档中给出的find命令的一般形 ...
- LeetCode344:Reverse String@Python
Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...
- web开发-给即将毕业实习生的一点面试经验
简历投递: 智联招聘51job 像赶集网和58同城最好别去投 面试的公司,特别是深圳这边,面试的时候公司小,很多人,八九不离十是那种搞培训的,很多时候,有些公司会主动打电话来教你去面试,这些绝大多数也 ...