zoukankan      html  css  js  c++  java
  • [IOI 2011]ricehub

    Description

    乡间有一条笔直而长的路称为“米道”。沿着这条米道上 R 块稻田,每块稻田的坐标均
    为一个 1 到 L 之间(含 1 和 L)的整数。这些稻田按照坐标以不减的顺序给出,即对于 0 ≤ i <
    R,稻田 i 的坐标 X[i]满足 1 ≤ X[0] ≤ ... ≤ X[R-1] ≤ L。 
    注意:可能有多块稻田位于同一个坐标上。 
    我们计划建造一个米仓用于储存尽可能多的稻米。和稻田一样,米仓将建在米道上,其
    坐标也是一个 1 到 L 之间的整数(含 1 和 L)。这个米仓可以建在满足上述条件的任一个位
    置上,包括那些原来已有一个或多个稻田存在的位置。 
    在收获季节,每一块稻田刚好出产一滿货车的稻米。为了将这些稻米运到米仓,需要雇
    用一位货车司机来运米。司机的收费是每一满货车运送一个单位的距离收取 1 元。換言之,
    将稻米从特定的稻田运到米仓的费用在数值上等于稻田坐标与米仓坐标之差的绝对值。 
    不幸的是,今年预算有限,我们至多只能花费 B 元运费。你的任务是要帮我们找出一个
    建造米仓的位置,可以收集到尽可能多的稻米。 

    Input

    第一行 三个整数 R L B
    接下来R行 每行一个整数 表示X[i]

    Output

    一个整数 最多稻米数

    Sample Input

    5 20 6
    1
    2
    10
    12
    14

    Sample Output

    3

    HINT

    1 ≤ R ≤ 100,000
    1 ≤ L ≤ 1,000,000,000
    0 ≤ B ≤ 2,000,000,000,000,000

    题解

    做的第二道$IOI$的题,虽然还是水题。NOIp小孩都会做。

    我们考虑二分答案,对于每个二分的答案$mid$,其实其另外一个性质就是它是在数轴上包含$mid$个点的区间。

    现在我们的问题就是这$mid$个点的区间中各个点到某一位置的最小值是否$<=B$。

    一个有价值的性质就是:数轴上$n$个点,若$n$为奇,那么所有点到点$lceil {n over 2} ceil$的总距离是最短的,而若$n$为偶,那么到$n over 2$或${n over 2}+1$的距离相等且最短。

    那么我们现在显然就是在数轴上找出所有包含$mid$个点的区间,并计算其最小距离和是否$<=B$。

    统计总距离:对于检查的$r$,我们再取$mid = {r over 2}$。

    随着$pos-1$(中点)向右移到$pos$,我们统计的值满足下列变换:

    1     tot -= abs(a[pos-(r&1)]-a[pos-(r&1)-mid]);
    2     tot += abs(a[pos+mid]-a[pos]);

     附张图片便于理解上式:

     1 //It is made by Awson on 2017.10.17
     2 #include <set>
     3 #include <map>
     4 #include <cmath>
     5 #include <ctime>
     6 #include <stack>
     7 #include <queue>
     8 #include <vector>
     9 #include <string>
    10 #include <cstdio>
    11 #include <cstdlib>
    12 #include <cstring>
    13 #include <iostream>
    14 #include <algorithm>
    15 #define LL long long
    16 #define Min(a, b) ((a) < (b) ? (a) : (b))
    17 #define Max(a, b) ((a) > (b) ? (a) : (b))
    18 #define Abs(x) ((x) < 0 ? (-(x)) : (x))
    19 using namespace std;
    20 const int N = 100000;
    21 
    22 LL n, m, b;
    23 LL a[N+5];
    24 
    25 bool check(LL r) {
    26     LL mid = r>>1, pos = mid+(r&1), tot = 0;
    27     for (int i = 1; i <= r; i++) tot += Abs(a[i]-a[pos]);
    28     if (tot <= b) return true;
    29     for (pos++; pos+mid <= n; pos++) {
    30         tot -= Abs(a[pos-(r&1)]-a[pos-(r&1)-mid]);
    31         tot += Abs(a[pos+mid]-a[pos]);
    32         if (tot <= b) return true;
    33     }
    34     return false;
    35 }
    36 void work() {
    37     scanf("%lld%lld%lld", &n, &m, &b);
    38     for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    39     LL L = 1, R = n, ans = 0;
    40     while (L <= R) {
    41         LL mid = (L+R)>>1;
    42         if (check(mid)) ans = mid, L = mid+1;
    43         else R = mid-1;
    44     }
    45     printf("%lld
    ", ans);
    46 }
    47 int main() {
    48     work();
    49     return 0;
    50 }
  • 相关阅读:
    python 文件 笔记
    python 模块、包 笔记
    类、对象
    python 函数 笔记
    测试价值体现
    断舍离-笔记2
    Happy 2006 POJ
    Triangle War POJ
    Complete the sequence! POJ
    放苹果 POJ
  • 原文地址:https://www.cnblogs.com/NaVi-Awson/p/7683818.html
Copyright © 2011-2022 走看看