zoukankan      html  css  js  c++  java
  • L

    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 another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l).

    Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≤ i ≤ j ≤ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d).

    Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y.

    Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler.

    Input

    The first line contains four positive space-separated integers nlxy (2 ≤ n ≤ 1052 ≤ l ≤ 1091 ≤ x < y ≤ l) — the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly.

    The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin.

    Output

    In the first line print a single non-negative integer v — the minimum number of marks that you need to add on the ruler.

    In the second line print v space-separated integers p1, p2, ..., pv (0 ≤ pi ≤ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them.

    Example

    Input
    3 250 185 230
    0 185 250
    Output
    1
    230
    Input
    4 250 185 230
    0 20 185 250
    Output
    0
    Input
    2 300 185 230
    0 300
    Output
    2
    185 230

    Note

    In the first sample it is impossible to initially measure the distance of 230centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark.

    In the second sample you already can use the ruler to measure the distances of 185and 230 centimeters, so you don't have to add new marks.

    In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills.

    解法:

    思路:从输出结果上来看,只有三种情况:

    1,不用添加输出0;

    2.要添加俩个,输出2和已知的男女的跳跃距离;

    3.只要添加一个,输出1和一个距离;

    第一,二种情况是最简单的,只要判断给出的刻度之间的距离等于男女的跳跃距离为一个或有两个就直接输出

    第三情况就比较麻烦.先添加一个距离,判断是否能够得到男女的跳跃距离,能的话就输出一个,否则输出二个;

    代码1:这是我自己的代码,

      1 #include <iostream>
      2 #include <algorithm>
      3 
      4 using namespace std;
      5 
      6 const int MAX = 100000;
      7 int ti1,ti2;
      8 int N;
      9 int L,L1,L2;
     10 int L0[MAX];
     11 
     12 
     13 int DP_A( int temp )
     14 {
     15     int t0,t1;
     16     t0 = 0;t1 = N-1;
     17     if(temp > L||temp  < 0)
     18         return -1;
     19     else
     20         while( t0 <= t1)
     21         {
     22             int t;
     23             t = (t0+t1)/2;
     24  //           cout<<" temp "<<temp<<endl;
     25  //           cout<<" t0 t1 t L0[t]"<<t0<<' '<<t1<<' '<<t<<' '<<L0[t]<<endl;
     26             if(L0[t] == temp)
     27                   return 1;
     28             else if(L0[t] < temp ){ t0 = t + 1; }
     29             else if(L0[t] > temp ){ t1 = t - 1; }
     30         }
     31     return -1;
     32 }
     33 
     34 int DP2()
     35 {
     36     int temp1,temp2;
     37     for(int i = 0;i < N;i++)
     38     {
     39         temp1 = L0[i] + L1;
     40         temp2 = L0[i] + L2;
     41  //       cout<<"temp1 "<<temp1<<endl;
     42         if(temp1<=L&&( DP_A(temp1-L2)==1||DP_A(temp1+L2)==1 ) )
     43            {
     44                cout<<1<<'
    '<<temp1<<endl;
     45                return 1;
     46            }
     47         if(temp2<=L&&( DP_A(temp2-L1) ==1||DP_A(temp2+L1)==1 ) )
     48             {
     49                 cout<<1<<'
    '<<temp2<<endl;
     50                  return 1;
     51             }
     52 
     53         temp1 = L0[i] - L1;
     54         temp2 = L0[i] - L2;
     55         if(temp1>=0&&( DP_A(temp1-L2)==1||DP_A(temp1+L2)==1 ) )
     56            {
     57                cout<<1<<'
    '<<temp1<<endl;
     58                 return 1;
     59            }
     60         if(temp2>=L&&( DP_A(temp2-L1)==1||DP_A(temp2+L1)==1 ) )
     61             {
     62                 cout<<1<<'
    '<<temp2<<endl;
     63                  return 1;
     64             }
     65     }
     66     cout<<2<<'
    '<<L1<<' '<<L2<<endl;
     67 
     68 }
     69 
     70 void DP()
     71 {
     72     int temp;
     73   //  cout<<" ti1 ti2"<<ti1<<' ' <<ti2<<endl;
     74     for(int i = 0;i < N;i++)
     75     {
     76         temp = DP_A( L0[i] + L1);if(  temp!= -1) ti1 =temp;
     77         temp = DP_A( L0[i] + L2);if(  temp!= -1) ti2 =temp;
     78         temp = DP_A( L0[i] - L1);if(  temp!= -1) ti1 =temp;
     79         temp = DP_A( L0[i] - L2);if(  temp!= -1) ti2 =temp;
     80     }
     81    // cout<<" ti1 ti2"<<ti1<<' ' <<ti2<<endl;
     82     if(ti1 != -1&&ti2 != -1)
     83         cout<<0<<endl;
     84     else if( ti1 != -1)
     85         cout<<1<<'
    '<<L2<<endl;
     86     else if( ti2 != -1)
     87         cout<<1<<'
    '<<L1<<endl;
     88     else if( ti1 ==-1&&ti2 == -1)
     89         DP2();
     90     else
     91         cout<<"出错"<<endl;
     92 
     93 }
     94 
     95 int main()
     96 {
     97     ti1 = -1;
     98     ti2 = -1;
     99    cin>>N;
    100    cin>>L>>L1>>L2;
    101 
    102    for(int i = 0;i < N;i++)
    103         cin >> L0[i];
    104     sort(L0,L0+N);
    105 
    106     DP();
    107     return 0;
    108 }

    代码2:这是一个大神的代码

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 
     5 using namespace std;
     6 
     7 const int maxn = 1e5+5;
     8 int N, L,  X, Y, A[maxn];
     9 
    10 bool judge (int u) {
    11     if (u < 0 || u > L) return false;
    12     int k = lower_bound(A, A + N, u) - A;
    13     return u == A[k];
    14 }
    15 
    16 void solve () {
    17     int ans = 0;
    18     for (int i = 0; i < N; i++) {
    19         if (judge(A[i] - X) || judge(A[i] + X))
    20             ans |= 1;
    21         if (judge(A[i] - Y) || judge(A[i] + Y))
    22             ans |= 2;
    23     }
    24 
    25     if (ans == 3)
    26         printf("0
    ");
    27     else if (ans == 2)
    28         printf("1
    %d
    ", X);
    29     else if (ans == 1)
    30         printf("1
    %d
    ", Y);
    31     else {
    32 
    33         for (int i = 0; i < N; i++) {
    34             int tx = A[i] + X;
    35             int ty = A[i] + Y;
    36 
    37             if (tx <= L && (judge(tx - Y) || judge(tx + Y))) {
    38                 printf("1
    %d
    ", tx);
    39                 return;
    40             }
    41 
    42             if (ty <= L && (judge(ty - X) || judge(ty + X))) {
    43                 printf("1
    %d
    ", ty);
    44                 return;
    45             }
    46         }
    47 
    48         for (int i = 0; i < N; i++) {
    49             int tx = A[i] - X;
    50             int ty = A[i] - Y;
    51 
    52             if (tx >= 0 && (judge(tx - Y) || judge(tx + Y))) {
    53                 printf("1
    %d
    ", tx);
    54                 return;
    55             }
    56 
    57             if (ty >= 0 && (judge(ty - X) || judge(ty + X))) {
    58                 printf("1
    %d
    ", ty);
    59                 return;
    60             }
    61         }
    62         printf("2
    %d %d
    ", X, Y);
    63     }
    64 }
    65 
    66 int main () {
    67     scanf("%d%d%d%d", &N, &L, &X, &Y);
    68     for (int i = 0; i < N; i++)
    69         scanf("%d", &A[i]);
    70     solve();
    71     return 0;
    72 }

     

  • 相关阅读:
    Golang ECHOhtml模板处理【5】
    Golang ECHO处理请求结果【4】
    Golang ECHO处理请求参数【3】
    Golang ECHO路由与控制器【2】
    Golang ECHO框架入门【1】
    GoLang GORM-CRUD
    Flume—(2)实时读取本地文件到HDFS
    Hive—开启日志
    Flume—(1)监控本机端口数据
    Hive—分桶及抽样查询
  • 原文地址:https://www.cnblogs.com/a2985812043/p/7221539.html
Copyright © 2011-2022 走看看