神奇密码

  题目大意:就是给你一个数组,要你找出连续的数的绝对值的和最接近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. CSS3的REM设置字体大小

    在Web中使用什么单位来定义页面的字体大小,至今天为止都还在激烈的争论着,有人说PX做为单位好,有人说EM优点多,还有人在说百分比方便,以至于出现了CSS Font-Size: em vs. px v ...

  2. ZOJ 3201 Tree of Tree

    树形DP.... Tree of Tree Time Limit: 1 Second      Memory Limit: 32768 KB You're given a tree with weig ...

  3. html 补充

    替换文本属性(Alt)alt 属性用来为图像定义一串预备的可替换的文本.替换文本属性的值是用户定义的.<img src="boat.gif" alt="Big Bo ...

  4. 【C语言入门教程】4.2 二维数组

    C 语言允许使用多维数组,即使用多组小标的数组,二维数组是最常用的多维数组.多维数组在内存中存放数据的顺序与一维数组相同,使用连续的存储单元. 4.2.1 二维数组的一般形式 二维数组的一般声明形式为 ...

  5. 解决访问ajax.googleapis.com链接失败方法

    参考文章:http://www.jianshu.com/p/690e28f7fde6 主要思路:修改hosts文件,将其网址ajax.googleapis.com指向本地服务器:本地服务器将通过aja ...

  6. thinkphp中的自动验证

    array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间]) 1.验证字段 需要验证的表单字段名称,这个字段不一定是数据库字段,也可以是表单的一些辅助字段,例如确认密码和验证码等等.有 ...

  7. 一次失败的动态转换bean的尝试与思考

    前因 公司规范确定不允许使用反射类的进行属性bean的拷贝了,只允许手动的get/set,可以猜到这样定义的原因是制定规范的同事认为反射性能低,虽然写get/set慢点好在性能高.平时开发的时候也是迫 ...

  8. CSU 1113 Updating a Dictionary(map容器应用)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113 解题报告:输入两个字符串,第一个是原来的字典,第二个是新字典,字典中的元素的格式为 ...

  9. 写给喜欢用Block的朋友(ios Block)

    作者:fengsh998原文地址:http://blog.csdn.net/fengsh998/article/details/38090205转载请注明出处如果觉得文章对你有所帮助,请通过留言或关注 ...

  10. uMlet建模工具

    下载:http://www.umlet.com/ 无意中发现的一款建模工具,能快速搭建数据库模型,前置安装条件是java环境. 这是我建的user模型表,2个字段name和age,2个方法getAge ...