神奇密码

  题目大意:就是给你一个数组,要你找出连续的数的绝对值的和最接近t的那一串,并且要找出数组的上界和下界的下标,并显示他们的和

  因为这一题的数有正有负,所以必须要先把和求出来,然后排序,然后利用a(s,t)=sum(t)-sum(s)找出目标

  

 #include <iostream>
#include <algorithm>
#include <functional> using namespace std; //pair<int, int>Acc[100016];
static struct _set
{
int sum, index;
bool operator<(const _set&x)const
{
return sum < x.sum;
}
}Acc[]; void solve(const int, const int);
int get_sum(int *const, int*const, int*const, const int, const int,const int);
int ABS(int); int main(void)//游标卡尺大法
{
int n, k, t, tmp; while (~scanf("%d%d", &n, &k))
{
if (n == && k == )
break;
Acc[].sum = ; Acc[].index = ;
for (int i = ; i <= n; i++)
{
scanf("%d", &tmp);
Acc[i].index = i; Acc[i].sum = Acc[i - ].sum + tmp;
}
sort(Acc, Acc + n + );//直接给和排序 for (int i = ; i < k; i++)
{
scanf("%d", &t);
solve(n, t);
}
}
return ;
} void solve(const int n, const int S)
{
int ans_sum, ans_lb, ans_ub, lb, ub, sum; lb = ub = ; sum = ans_sum = 0x80808080;
while ()
{
while (ub < n && sum < S)//标准尺取法
sum = get_sum(&ans_sum, &ans_lb, &ans_ub, lb, ++ub, S);
if (sum < S)
break;
sum = get_sum(&ans_sum, &ans_lb, &ans_ub, ++lb, ub, S);
}
printf("%d %d %d\n", ans_sum, ans_lb + , ans_ub);
} int ABS(int x)
{
return x >= ? x : -x;
} int get_sum(int *const ans_sum, int*const ans_lb, int*const ans_ub, const int lb, const int ub,const int S)
{
if (lb >= ub)
return INT_MIN;
int tmp = Acc[ub].sum - Acc[lb].sum;
if (ABS(tmp - S) < ABS(*ans_sum - S))
{
*ans_sum = tmp;
*ans_lb = min(Acc[ub].index, Acc[lb].index);
*ans_ub = max(Acc[ub].index, Acc[lb].index);
}
return tmp;
}

  

参考http://www.hankcs.com/program/algorithm/poj-2566-bound-found.html

Greedy:Bound Found(POJ 2566)的更多相关文章

  1. B - Bound Found POJ - 2566(尺取 + 对区间和的绝对值

    B - Bound Found POJ - 2566 Signals of most probably extra-terrestrial origin have been received and ...

  2. 尺取法 poj 2566

    尺取法:顾名思义就是像尺子一样一段一段去取,保存每次的选取区间的左右端点.然后一直推进 解决问题的思路: 先移动右端点 ,右端点推进的时候一般是加 然后推进左端点,左端点一般是减 poj 2566 题 ...

  3. POJ 2566:Bound Found(Two pointers)

    [题目链接] http://poj.org/problem?id=2566 [题目大意] 给出一个序列,求一个子段和,使得其绝对值最接近给出值, 输出这个区间的左右端点和区间和. [题解] 因为原序列 ...

  4. poj 2566 Bound Found(尺取法 好题)

    Description Signals of most probably extra-terrestrial origin have been received and digitalized by ...

  5. poj 2566 Bound Found

    Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4384   Accepted: 1377   Spe ...

  6. POJ 2566 Bound Found 尺取 难度:1

    Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 1651   Accepted: 544   Spec ...

  7. POJ 2566 Bound Found(尺取法,前缀和)

    Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5207   Accepted: 1667   Spe ...

  8. poj 2566 Bound Found 尺取法 变形

    Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2277   Accepted: 703   Spec ...

  9. poj 2566 Bound Found 尺取法

    一.首先介绍一下什么叫尺取 过程大致分为四步: 1.初始化左右端点,即先找到一个满足条件的序列. 2.在满足条件的基础上不断扩大右端点. 3.如果第二步无法满足条件则到第四步,否则更新结果. 4.扩大 ...

随机推荐

  1. acdream1233 Royal Federation (构造?)

    http://acdream.info/problem?pid=1233 Andrew Stankevich's Contest (3) ASC 3 Royal Federation Special ...

  2. Python 文件遍历

    Python具备强大的解析能力,其中列表解析甚至可以作用在某些并非实际存储的序列上,任何可遍历对象都可以,包括可自动逐步读取的文件. 例如下面的代码将会从逐行读取一个文本文件,并且在每一行的最后加上一 ...

  3. .net利用NPOI导入导出Excel

    NPOI在.net中的操作Excel 1.读取 using (FileStream stream = new FileStream(@"c:\客户资料.xls", FileMode ...

  4. Express开发实例(2) —— Jade模板引擎

    前一篇通过helloworld,简单介绍了Express中的开发,本篇继续深入的学习express的模板. 关于Jade的用法,网上有很多,本篇参考:Jade语法 安装相关模块 在实验代码前,应该先安 ...

  5. jquery 使用textarea

    问题: 若在textarea标签中键入一个回车时,将插入2个字符\n\r ,将在datagrid不能正确显示. \n: 回车符 \r: 换行符  解决方案: txt = txt.Replace(&qu ...

  6. 各大互联网公司前端面试题(js)

    对于巩固复习js更是大有裨益.    初级Javascript: 1.JavaScript是一门什么样的语言,它有哪些特点? 没有标准答案. 2.JavaScript的数据类型都有什么? 基本数据类型 ...

  7. CSS Image Sprite--网页图片应用处理方式

    CSS Sprites简介 CSSSprites在国内很多人叫css精 灵,是一种网页图片应用处理方式.它允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问该页面时,载入的图片 ...

  8. POJ2104 —— K-th number

    1.题目大意:区间第k小,什么修改没有... 2.分析:这个是可持久化线段树,也是主席树,解释一下,n个线段树是怎么存下的,就是每一颗线段树和前一个有logn个点不一样 然后我们只需要一个线段树开lo ...

  9. windows7 + cocos2d-x 3.2 +vs2012 速度真的很慢

    每次编译要大半天,简直伤不起,这样效率如何上的去???有谁有办法?

  10. ios中二维码的使用之二: 二维码的扫描

    二维码的扫描: 1,导入支持框架,<AVFoundation/AVFoundation.h> 2 ,扫描: