练习cf2094B. Bobritto Bandito

题目如下
https://codeforces.com/problemset/problem/2094/B
B. Bobritto Bandito
time limit per test1 second
memory limit per test256 megabytes
In Bobritto Bandito's home town of residence, there are an infinite number of houses on an infinite number line, with houses at …,−2,−1,0,1,2,…. On day 0, he started a plague by giving an infection to the unfortunate residents of house 0. Each succeeding day, the plague spreads to exactly one healthy household that is next to an infected household. It can be shown that each day the infected houses form a continuous segment.

Let the segment starting at the 𝑙-th house and ending at the 𝑟-th house be denoted as [𝑙,𝑟]. You know that after 𝑛 days, the segment [𝑙,𝑟] became infected. Find any such segment [𝑙′,𝑟′] that could have been infected on the 𝑚-th day (𝑚≤𝑛).

Input
The first line contains an integer 𝑡 (1≤𝑡≤100) – the number of independent test cases.

The only line of each test case contains four integers 𝑛, 𝑚, 𝑙, and 𝑟 (1≤𝑚≤𝑛≤2000,−𝑛≤𝑙≤0≤𝑟≤𝑛,𝑟−𝑙=𝑛).

Output
For each test case, output two integers 𝑙′ and 𝑟′ on a new line. If there are multiple solutions, output any.

题目大意
现有一串无限长度数轴,每个数字代表一户人家,现在从0开始向两边扩散,每天扩散相邻的1户人家;现有n,m,l,r四个数字,n代表当前感染天数,l和r分别是感染的边界线,[l,r]区间内都表示已感染的人家,问第m天的可能的感染范围,m<=n。

题目分析
既然已有第n天的感染范围,求第m天的感染范围,n-m得出相差的感染天数,因为感染从0开始,那么假设每次都是先从一边开始感染,那么假设最后n-m天都从左边开始感染,根据天数在减少相应的感染人家,如果0左边的人数不够,再从右边开始减少剩下的感染人家。

点击查看代码
#include <iostream>
#include <algorithm>
using namespace std;

int main(){
    int t;
    cin >> t;
    while(t--){
        int n, m, l, r;
        cin >> n >> m >> l >> r;
        int gap = n - m;
        int sl = l, sr = r;
        while(gap && sl){
            sl++;
            gap--;
        }
        sr = sr - gap;
        printf("%d %d\n", sl, sr);
    }
    return 0;
}
posted @ 2025-07-17 20:23  sirro1uta  阅读(20)  评论(0)    收藏  举报