Codeforces 479D Long Jumps(贪心+二分)

题目链接:Codeforces 479D Long Jumps

题目大意:valery是个体育老师,现在他要为学生考跳远,女生标准为x,男生为y,现在一个长为L的刻度尺,有N个刻

度,给定N个刻度,现在为说还需要加几个刻度才能测量x,y这两个长度。

解题思路:因为总共就x,y两个长度,所以最多加两个刻度。所以只要判断不加和加一个的情况即可。

先枚举每个刻度a[i],然后用二分查找一下a[i]+x或者a[i]-x刻度存不存在,同理y。如果x和y都通过判断,那么就是不需

要加刻度。

如果只通过x或只通过y,只需要加一个刻度。

有一种情况就为x和y都没有通过,但是加一个刻度就够了,这种情况,只要枚举加刻度的位置即可。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 1e5+5;
int N, L,  X, Y, A[maxn];

bool judge (int u) {
    if (u < 0 || u > L) return false;
    int k = lower_bound(A, A + N, u) - A;
    return u == A[k];
}

void solve () {
    int ans = 0;
    for (int i = 0; i < N; i++) {
        if (judge(A[i] - X) || judge(A[i] + X))
            ans |= 1;
        if (judge(A[i] - Y) || judge(A[i] + Y))
            ans |= 2;
    }

    if (ans == 3)
        printf("0\n");
    else if (ans == 2)
        printf("1\n%d\n", X);
    else if (ans == 1)
        printf("1\n%d\n", Y);
    else {

        for (int i = 0; i < N; i++) {
            int tx = A[i] + X;
            int ty = A[i] + Y;

            if (tx <= L && (judge(tx - Y) || judge(tx + Y))) {
                printf("1\n%d\n", tx);
                return;
            }

            if (ty <= L && (judge(ty - X) || judge(ty + X))) {
                printf("1\n%d\n", ty);
                return;
            }
        }

        for (int i = 0; i < N; i++) {
            int tx = A[i] - X;
            int ty = A[i] - Y;

            if (tx >= 0 && (judge(tx - Y) || judge(tx + Y))) {
                printf("1\n%d\n", tx);
                return;
            }

            if (ty >= 0 && (judge(ty - X) || judge(ty + X))) {
                printf("1\n%d\n", ty);
                return;
            }
        }
        printf("2\n%d %d\n", X, Y);
    }
}

int main () {
    scanf("%d%d%d%d", &N, &L, &X, &Y);
    for (int i = 0; i < N; i++)
        scanf("%d", &A[i]);
    solve();
    return 0;
}
时间: 2024-12-22 09:51:34

Codeforces 479D Long Jumps(贪心+二分)的相关文章

Codeforces 479D - Long Jumps

479D - Long Jumps, 480B - Long Jumps It is easy to see that the answer is always 0, 1 or 2. If we can already measure both x and y, output 0. Then try to measure both x and y by adding one more mark. If it was not successful, print two marks: one at

Codeforces 825D Suitable Replacement - 贪心 - 二分答案

You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters. Suitability of string s is calculated by following metric: Any two letters can be swapped positions, these operations can be performed arbi

Long Jumps CodeForces - 479D

E - Long Jumps CodeForces - 479D Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found anot

Card Game Cheater(贪心+二分匹配)

Card Game Cheater Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1566    Accepted Submission(s): 822 Problem Description Adam and Eve play a card game using a regular deck of 52 cards. The rule

Codeforces Round #300——C贪心——Tourist&#39;s Notes

Description A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, me

Codeforces 374D Inna and Sequence 二分+树状数组

题目链接:点击打开链接 给定n个操作,m长的序列a 下面n个数 if(co>=0)则向字符串添加一个co (开始是空字符串) else 删除字符串中有a的下标的字符 直接在序列上搞,简单模拟 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h&

Codeforces 77C 树形dp + 贪心

题目链接:点击打开链接 题意: 给定n个点, 每个点的豆子数量 下面是一棵树 再给出起点 每走到一个点,就会把那个点的豆子吃掉一颗. 问:回到起点最多能吃掉多少颗豆子 思路:树形dp 对于当前节点u,先把子节点v都走一次. 然后再往返于(u,v) 之间,直到u点没有豆子或者v点没有豆子. dp[u] 表示u点的最大值.a[u] 是u点剩下的豆子数. #include <cstdio> #include <vector> #include <algorithm> #inc

Codeforces 8D Two Friends 三分+二分+计算几何

题目链接:点击打开链接 题意:点击打开链接 三分house到shop的距离,二分这条斜边到cinema的距离 #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<math.h> #include<set> #include<queue> #include<vector> #include<

贪心/二分查找 BestCoder Round #43 1002 pog loves szh II

题目传送门 1 /* 2 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 3 当然有可能两个数和超过p,那么an的值最优,每次还要和an比较 4 注意:不能选取两个相同的数 5 反思:比赛时想到了%p和sort,lower_bound,但是还是没有想到这个贪心方法保证得出最大值,还是题目做的少啊:( 6 */ 7 #include <cstdio> 8 #include <algorithm>