社区讨论

二分法60分 蒟蒻卑微求调

P2678[NOIP 2015 提高组] 跳石头参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@lo30fsto
此快照首次捕获于
2023/10/23 22:45
2 年前
此快照最后确认于
2023/10/23 22:45
2 年前
查看原帖
应该是想复杂了 一个二分答案写了好长XD
我想的是二分最短的起跳距离x
check所有岩石,如果跳跃距离小于x就去掉这个石头
最后看去掉石头的数目是否小于等于题干
如果大于就返回false
小于等于就返回true
CPP
#include <iostream>

using namespace std;
const int N = (int)5e4;
int total_run;
int total_rocks;
int demand;
int dist[N];
int ans;
inline bool check(int x)
{
    int sum = 0;
    int land_mark = 0;
    int jump_mark = 0;
    int gap = 0;
    // i代表起跳点
    for (int i = 0; i <= total_rocks;)
    {
        jump_mark = i;
        land_mark = i + 1;
        if (i == 0)
        {
            gap = dist[i];
        }
        else
        {
            if (i == total_rocks)
            {
                gap = total_run - dist[i];
            }
            else
            {
                gap = dist[land_mark] - dist[i];
            }
        }
        // 不满足条件,去掉石子
        while (gap < x)
        {
            sum++;
            land_mark++;
            if (land_mark == total_rocks + 1)
            {
                gap = total_run - dist[jump_mark];
            }
            else
            {
                gap = dist[land_mark] - dist[jump_mark];
            }
            // int body = land_mark - 1;
            // cout << body << endl;
            // cout << x << endl;
            // cout << "dogshit" << endl;
            if (sum > demand)
            {
                // cout << "fuck" << endl;
                return false;
            }
        }
        i = land_mark;
    }
    return true;
}
int main()
{
    cin >> total_run >> total_rocks >> demand;
    for (int i = 1; i <= total_rocks; i++)
    {
        cin >> dist[i];
    }
    int left = 0;
    int right = total_run;
    while (left <= right)
    {
        int mid = (left + right) / 2;
        if (check(mid))
        {
            ans = mid;
            left = mid + 1;
        }
        else
        {
            right = mid - 1;
        }
    }
    cout << ans << endl;
    return 0;
}

回复

1 条回复,欢迎继续交流。

正在加载回复...