• poj3258 River Hopscotch (二分,最小值最大化)


    题目做的太少了。。二分感觉不太适应这种想法。。

    题目大意是一条长L 的河上, 除了START 和 END 还有N 个石子, 分别距离起点距离di, 求去掉M个石子后相邻的最小距离的最大值。

    一道比较典型的求最小值最大化的题目

    定义函数 c(x) 是求距离x能否留下N-M个石子。然后通过二分找出最大值

    题目:

    A - River Hopscotch
    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
    Submit Status
    Appoint description:

    Description

    Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, L units away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L).

    To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.

    Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to M rocks (0 ≤ MN).

    FJ wants to know exactly how much he can increase the shortest distance *before* he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks.

    Input

    Line 1: Three space-separated integers: L, N, and M
    Lines 2..N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.

    Output

    Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing M rocks

    Sample Input

    25 5 2
    2
    14
    11
    21
    17

    Sample Output

    4

    Hint

    Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).
     
     
     
    代码:
     1 #include <iostream>
     2 #include <algorithm>
     3 using namespace std;
     4 #define MAXN 50000+10
     5 
     6 int L,N,M;
     7 int dist[MAXN];
     8 
     9 bool C(int x)
    10 {
    11     int num = N-M;
    12 
    13     int last = 0;
    14     for(int i=0;i<num;i++)
    15     {
    16             int cur = last +1;
    17 
    18             while( cur<= N && dist[cur]-dist[last]<x)
    19             {
    20                 cur++;
    21             }
    22             if( cur > N)return 0;
    23 
    24             last = cur;
    25     }
    26     return 1;
    27 
    28 }//判断距离x能否去掉 M块石头
    29 
    30 int main()
    31 {
    32 
    33     cin>>L>>N>>M;
    34 
    35     for(int i=1;i<=N;i++)
    36     {
    37         cin>>dist[i];
    38     }
    39     dist[N+1] = L;
    40 
    41     sort(dist, dist+N+1);
    42 
    43     if( M == N)
    44     {
    45         cout<<L <<endl;
    46         return 0;
    47     }
    48 
    49     int l = 0 ,r = L;
    50     while( r- l >1)
    51     {
    52         int mid =l+ (r-l) /2;
    53 
    54         if( C(mid) ) l = mid;
    55         else
    56             r = mid;
    57     }
    58     cout<<l<<endl;
    59 
    60     return 0;
    61 }
  • 相关阅读:
    C+= concurrent_queue 线程安全测试
    c++ 枚举 在函数中的应用
    shell脚本积累
    hibernate+spring整合增删改事务错误
    checkbox批量删除功能
    html全选和取消全选JS
    hibernate+pageBean实现分页dao层功能代码
    table样式
    WebStorm 11 Lisence server
    Kb,KB,Kbps,Mb,Mbps等一些列概念
  • 原文地址:https://www.cnblogs.com/doubleshik/p/3537356.html
Copyright © 2020-2023  润新知