Codeforces 480B Long Jumps 规律题

题目链接:点击打开链接

题意:

输出n l x y

有一根直尺长度为l

上面有n个刻度。

下面n个数字是距离开头的长度(保证第一个数字是0,最后一个数字是l)

要使得 直尺中存在某2个刻度的距离为x , 某2个刻度的距离为y

要添加最少几个刻度。

问:

最少的刻度个数

输出标记的位置。

思路:

分类讨论一下。。

若本身尺子里就有x、y就输出0

若只有x 或只有y就输出一个刻度。

若2个都没有就:

1、加1个刻度ans,这个ans是距离某个刻度距离为x的,然后看一下是否有距离ans为y的刻度,若有则添加一个ans即可。

2、第1个都非法时就直接加2个刻度。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
template <class T>
inline bool rd(T &ret) {
    char c; int sgn;
    if(c=getchar(),c==EOF) return 0;
    while(c!='-'&&(c<'0'||c>'9')) c=getchar();
    sgn=(c=='-')?-1:1;
    ret=(c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
    ret*=sgn;
    return 1;
}
template <class T>
inline void pt(T x) {
    if (x <0) {
        putchar('-');
        x = -x;
    }
    if(x>9) pt(x/10);
    putchar(x%10+'0');
}
using namespace std;
typedef long long ll;
const int N = 200050;

vector<ll>G;
bool Find(ll x){
    for(int i = G.size()-2; i > 0; i--)
    {
        if(G[i] - x < 0)return false;
        if( G[lower_bound(G.begin(), G.end(), G[i] - x) - G.begin()] == G[i] - x)
            return true;
    }
    return false;
}
ll n, l, x, y;
ll go(){
    ll ans;
    for(int i = G.size()-2; i > 0; i--){
        if(G[i]-x < 0)break;
        ans = G[i]-x;
        if(G[lower_bound(G.begin(), G.end(), ans+y) - G.begin()] == ans+y || G[lower_bound(G.begin(), G.end(), ans-y) - G.begin()] == ans-y)
            return ans;
    }
    return -1;
}
void input(){
    G.clear();
    G.push_back(-5000000001LL);
    ll tmp;
    while(n--){
        rd(tmp);
        G.push_back(tmp);
    }
    G.push_back(1e10);
}

int main() {
    while(cin>>n>>l>>x>>y){
        input();
        int havx = Find(x), havy = Find(y);
        if(havx + havy == 2)
            puts("0");
        else if(havx + havy == 1)
        {
            if(havx)
                printf("1\n%I64d\n", y);
            else
                printf("1\n%I64d\n", x);
        }
        else {
            ll ans = go();
            if(ans == -1)
            {
                printf("2\n%I64d %I64d\n", x, y);
            }
            else
                printf("1\n%I64d\n", ans);
        }
    }
    return 0;
}
时间: 2024-08-06 07:28:11

Codeforces 480B Long Jumps 规律题的相关文章

Codeforces 10C Digital Root 规律题

题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h> #include<string> #include<stdlib.h> #include<algorithm> using nam

CodeForces 42C Safe cracking 规律题

题目链接:点击打开链接 3个数为一组,找最大的一个数让它降低,则显然是有解的,分类讨论一下就可以 #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<vector> using namespace std; int n, k; int a[4]; vector<pair<int,int> >ans; int m

CodeForces 468A 24 Game 规律题

给定一个n表示有n个数字[1,n] 问: 用+ - * 3种运算把n个数变成24是否可行,若可行输出任意解. 思路: 因为2*3*4 = 24; 所以当n>=4时一定有解. 若n是偶数则4个连续数一组消成0,若还多余2个则变成 (n+1)-n = 1. 若n是奇数则把5消掉: 5-1 = 4; 4-2 = 2;. [java] view plaincopy import java.io.PrintWriter; import java.util.ArrayList; import java.ut

Codeforces 396B On Sum of Fractions 规律题

题目链接:点击打开链接 我们把 1 / { u(i)*v(i) }拆开->  (1/(u(i)-v(i)) * ( 1/v(i) - 1/u(i) ) 若n +1  是素数,则显然(1/(u(i)-v(i)) * ( 1/v(i) - 1/u(i) ) 这样完全相同的式子有 u(i)-v(i) 个 那么就可以把前面系数约掉,那么剩下的式子就是 1/2 - 1/(n+1) 若不是,则我们找到第一个<=n的素数,即v(n) 和第一个>n的素数,即 u(n) 然后前面的 2-v(n)求和,即

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 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或只

Codeforces 48C The Race 模拟题

题目链接:点击打开链接 题意: 给定n个加油站,一辆车由A点跑到B点,每个100m有一个加油站,每开100m需要10升油. 在每个车站会检查一下油量,若车子若开不到下一个加油站则加x升油. 开始有x升油 下面给出加油的记录. 问下一次加油在哪一站.若答案唯一输出具体哪站. 油箱容量无限 思路: 水模拟.. #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h>

CodeForces 22D Segments 排序水题

题目链接:点击打开链接 右端点升序,取右端点 暴力删边 #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <iostream> #include <map> #include <set> #include <math.h> using namespace std; #define inf 10

ZOJ 3798 Abs Problem(规律题)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3798 Abs Problem Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Alice and Bob is playing a game, and this time the game is all about the absolute value! Alice has