Codeforces 645C Enduring Exodus 【二分】

本文介绍了解决Codeforces645C问题的方法,该问题要求最小化一个人与其放置的若干牛之间的最大距离。通过二分查找算法确定最优解,并提供了完整的AC代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:Codeforces 645C Enduring Exodus

题意:有 n 个一排的房子,0表示空, 1 表示非空。现在要把一个人和k头牛放里面(保证一定可以放完)。让你最小化 人和所有牛之间距离的最大值。

思路:我们来二分答案。对于当前的 mid 值,我们只需考虑在区间 [l,r] 能否有 k+1 个空位置。

AC 代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e5 + 10;
const int pN = 1e6;// <= 10^7
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
char str[MAXN];
int sum[MAXN];
int n, k;
bool judge(int pos, int len) {
    int l = max(1, pos-len);
    int r = min(n, pos+len);
    return (sum[r] - sum[l-1]) >= k + 1;
}
int Solve(int pos) {
    int l = 1, r = n;
    int ans;
    while(r >= l) {
        int mid = (l + r) >> 1;
        if(judge(pos, mid)) {
            ans = mid;
            r = mid-1;
        }
        else
            l = mid+1;
    }
    return ans;
}
int main()
{
    cin >> n >> k; cin >> str+1;
    sum[0] = 0;
    for(int i = 1; i <= n; i++) {
        if(str[i] == '0') {
            sum[i] = sum[i-1] + 1;
        }
        else
            sum[i] = sum[i-1];
    }
    int ans = INF;
    for(int i = 1; i <= n; i++) {
        if(str[i] == '1') continue;
        ans = min(ans, Solve(i));
    }
    cout << ans << endl;
    return 0;
}
Key Features The problems of CSS at scale - specificity, the cascade and styles intrinsically tied to element structure. The shortfalls of conventional approaches to scaling CSS. The ECSS methodology and the problems it solves. How to develop consistent and enforceable selector naming conventions with ECSS. How to organise project structure to more easily isolate and decouple visual components. Considerations of CSS tooling and processing: Sass/PostCSS and linting. Addressing the notion of CSS selector speed with hard data and browser representative insight. Book Description Learn with me, Ben Frain, about how to really THINK about CSS and how to use CSS for any size project! I'll show you how to write CSS that endures continual iteration, multiple authors, and yet always produces predictable results. Enduring CSS, often referred to as ECSS, offers you a robust and proven approach to authoring and maintaining style sheets at scale. Enduring CSS is not a book about writing CSS, as in the stuff inside the curly braces. This is a book showing you how to think about CSS, and be a smarter developer with that thinking! It's about the organisation and architecture of CSS: the parts outside the braces. I will help you think about the aspects of CSS development that become the most difficult part of writing CSS in larger projects. You ll learn about the problems of authoring CSS at scale - including specificity, the cascade and styles intrinsically tied to document structure. I'll introduce you to the ECSS methodology, and show you how to develop consistent and enforceable selector naming conventions. We'll cover how to apply ECSS to your web applications and visual models. And how you can organise your project structure wisely, and handle visual state changes with ARIA, providing greater accessibility considerations. In addition, we'll take a deep look into CSS tooling and process considerations. Finally we will address performance considerations by examining topics such as CSS selector speed with hard data and browser-representative insight. Enduring CSS is a book for considering how to write CSS in the most effective manner, and how you too can create an enduring CSS code base, regardless of project size. Take a journey with me if you want to explore these topics and deepen your thinking as a CSS author.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值