神奇密码

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

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

  

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <functional>
  4.  
  5. using namespace std;
  6.  
  7. //pair<int, int>Acc[100016];
  8. static struct _set
  9. {
  10. int sum, index;
  11. bool operator<(const _set&x)const
  12. {
  13. return sum < x.sum;
  14. }
  15. }Acc[];
  16.  
  17. void solve(const int, const int);
  18. int get_sum(int *const, int*const, int*const, const int, const int,const int);
  19. int ABS(int);
  20.  
  21. int main(void)//游标卡尺大法
  22. {
  23. int n, k, t, tmp;
  24.  
  25. while (~scanf("%d%d", &n, &k))
  26. {
  27. if (n == && k == )
  28. break;
  29. Acc[].sum = ; Acc[].index = ;
  30. for (int i = ; i <= n; i++)
  31. {
  32. scanf("%d", &tmp);
  33. Acc[i].index = i; Acc[i].sum = Acc[i - ].sum + tmp;
  34. }
  35. sort(Acc, Acc + n + );//直接给和排序
  36.  
  37. for (int i = ; i < k; i++)
  38. {
  39. scanf("%d", &t);
  40. solve(n, t);
  41. }
  42. }
  43. return ;
  44. }
  45.  
  46. void solve(const int n, const int S)
  47. {
  48. int ans_sum, ans_lb, ans_ub, lb, ub, sum;
  49.  
  50. lb = ub = ; sum = ans_sum = 0x80808080;
  51. while ()
  52. {
  53. while (ub < n && sum < S)//标准尺取法
  54. sum = get_sum(&ans_sum, &ans_lb, &ans_ub, lb, ++ub, S);
  55. if (sum < S)
  56. break;
  57. sum = get_sum(&ans_sum, &ans_lb, &ans_ub, ++lb, ub, S);
  58. }
  59. printf("%d %d %d\n", ans_sum, ans_lb + , ans_ub);
  60. }
  61.  
  62. int ABS(int x)
  63. {
  64. return x >= ? x : -x;
  65. }
  66.  
  67. int get_sum(int *const ans_sum, int*const ans_lb, int*const ans_ub, const int lb, const int ub,const int S)
  68. {
  69. if (lb >= ub)
  70. return INT_MIN;
  71. int tmp = Acc[ub].sum - Acc[lb].sum;
  72. if (ABS(tmp - S) < ABS(*ans_sum - S))
  73. {
  74. *ans_sum = tmp;
  75. *ans_lb = min(Acc[ub].index, Acc[lb].index);
  76. *ans_ub = max(Acc[ub].index, Acc[lb].index);
  77. }
  78. return tmp;
  79. }

  

参考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. TMS320C64X+ 中使用EDMA3中断

    关于EDMA3,TI的文档有详细的介绍.园子里的文章 http://www.cnblogs.com/fpga/archive/2009/10/13/1582516.html,http://www.cn ...

  2. CSS文档流

    文档流 将窗体自上而下分成一行行, 并在每行中按从左至右的顺序排放元素,即为文档流. 每个非浮动块级元素都独占一行, 浮动元素则按规定浮在行的一端. 若当前行容不下, 则另起新行再浮动. 内联元素也不 ...

  3. Backbone☞View中的events...click事件失效

    <div id="container"> <input type="button" id="test_click" val ...

  4. 2013nanjingJ

    J - Ball Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  5. hdu.1430.魔板(bfs + 康托展开)

    魔板 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  6. PHP get_class 返回对象的类名

    get_class (PHP 4, PHP 5) get_class — 返回对象的类名 说明 string get_class ([ object $obj ] ) 返回对象实例 obj 所属类的名 ...

  7. [转]CentOS 5.3通过yum升级php到最新版本的方法

    来自:www.jasonlitka.com/media 通过测试,方法三可行: 方法三 vim /etc/yum.repos.d/utterramblings.repo 输入 [utterrambli ...

  8. vncserver和Ubuntu Xfce4远程桌面环境的配置,解决不显示图形界面

    vncserver和Ubuntu Xfce4远程桌面环境的配置 参考的http://blog.163.com/thinki_cao/blog/static/8394487520130301453180 ...

  9. [KOJ6024]合并果子·改(强化版)

    [COJ6024]合并果子·改(强化版) 试题描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多把这些果子堆排成一排,然后所有的果子合成一堆.    每一次合并 ...

  10. aspcms标签

    [newslist:date style=yy-m-d] 日期格式 {aspcms:sitepath}/Templates/{aspcms:defaulttemplate} 幻灯片标签{aspcms: ...