CF Educational Codeforces Round 15(A~D)

来源:互联网 发布:电脑锣走刀路软件 编辑:程序博客网 时间:2024/05/23 01:58
题目链接:http://codeforces.com/contest/702
A. Maximum Increase
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given array consisting of n integers. Your task is to find the maximum length of an increasing subarray of the given array.

A subarray is the sequence of consecutive elements of the array. Subarray is called increasing if each element of this subarraystrictly greater than previous.

Input

The first line contains single positive integer n (1 ≤ n ≤ 105) — the number of integers.

The second line contains n positive integersa1, a2, ..., an (1 ≤ ai ≤ 109).

Output

Print the maximum length of an increasing subarray of the given array.

Examples
Input
51 7 2 11 15
Output
3
Input
6100 100 100 100 100 100
Output
1
Input
31 2 3
Output
3

题目大意: n个数求最长的连续子序列

思路:直接for一遍就好,前后比较一下

#include <bits/stdc++.h>using namespace std;int num[111111];int main(){    int n;    while (~scanf("%d",&n))    {        for (int i = 0 ; i < n ; i++ )        {            scanf("%d",&num[i]);        }        int res = 0;        int temp = 1;        for (int i = 1 ; i < n ; i++ )        {            if (num[i] > num[i-1])            {                temp++;            }            else             {                if (temp > res)                {                    res = temp;                                    }                temp  = 1;            }        }        if (temp > res)        {            res = temp;        }        printf("%d\n",res);    }}

B. Powers of Two
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given n integers a1, a2, ..., an. Find the number of pairs of indexesi, j (i < j) thatai + aj is a power of2 (i. e. some integerx exists so thatai + aj = 2x).

Input

The first line contains the single positive integer n (1 ≤ n ≤ 105) — the number of integers.

The second line contains n positive integersa1, a2, ..., an (1 ≤ ai ≤ 109).

Output

Print the number of pairs of indexes i, j (i < j) thatai + aj is a power of2.

Examples
Input
47 3 2 1
Output
2
Input
31 1 1
Output
3
Note

In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).

In the second example all pairs of indexes (i, j) (wherei < j) include in answer.

题目大意:给你n个数,问有多少对数的和是2的幂

思路:预处理2的幂,因为小于10^9 所以不超过32位。然后用map存这n个数出现的次数。再for一次,求预处理出来的2的幂与该数差出现的次数。最后答案要除于2,因为会由重复

#include <bits/stdc++.h>using namespace std;map<__int64 ,__int64>ma;__int64 x[1111111];int main(){    __int64 num[33];    num[0] = 1;    for (int i = 1 ; i < 33 ; i++ )    {        num[i] = num[i - 1] * 2;    }    int n;    while (~scanf("%d",&n))    {        ma.clear();        for (int i = 0 ; i < n ; i++ )        {            scanf("%I64d",&x[i]);            ma[x[i]]++;        }        __int64 res = 0;        for (int i = 0 ; i < n ; i ++ )        {            for (int j = 0 ; j < 33 ; j++ )            {                if (num[j] < x[i])                {                    continue;                }                else                 {                    res += ma[fabs(num[j] - x[i])];                    if ( fabs(num[j] - x[i]) == x[i])                    {                        res --;                    }                }                            }        }        printf("%I64d\n",res / 2);    }    return 0;}

C. Cellular Network
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given n points on the straight line — the positions (x-coordinates) of the cities andm points on the same line — the positions (x-coordinates) of the cellular towers. All towers work in the same way — they provide cellular network for all cities, which are located at the distance which is no more thanr from this tower.

Your task is to find minimal r that each city has been provided by cellular network, i.e. for each city there is at least one cellular tower at the distance which is no more thanr.

If r = 0 then a tower provides cellular network only for the point where it is located. One tower can provide cellular network for any number of cities, but all these cities must be at the distance which is no more than r from this tower.

Input

The first line contains two positive integers n andm (1 ≤ n, m ≤ 105) — the number of cities and the number of cellular towers.

The second line contains a sequence of n integersa1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the coordinates of cities. It is allowed that there are any number of cities in the same point. All coordinatesai are given in non-decreasing order.

The third line contains a sequence of m integersb1, b2, ..., bm ( - 109 ≤ bj ≤ 109) — the coordinates of cellular towers. It is allowed that there are any number of towers in the same point. All coordinatesbj are given in non-decreasing order.

Output

Print minimal r so that each city will be covered by cellular network.

Examples
Input
3 2-2 2 4-3 0
Output
4
Input
5 31 5 10 14 174 11 15
Output
3

题目大意:在一条直线上有n个城镇,然后有m个站点覆盖。问站点的覆盖半径至少为多少可以覆盖所有的城镇

思路:二分答案,再每次判断能否覆盖所有城市。不过注意,最长的半径为2 * 10 ^ 9

#include <bits/stdc++.h>using namespace std;__int64 a[1111111];__int64 b[1111111];int main(){    int n,m;    while (~scanf("%d%d",&n,&m))    {        for (int i = 0 ; i < n ; i++ )        {            scanf("%I64d",&a[i]);        }        for (int i = 0 ; i < m ; i++ )        {            scanf("%I64d",&b[i]);        }        __int64 l = 0,r = 2000000008;        while(l < r)        {            __int64 mid = (l + r) / 2;            int flag = 0;            int x = 0;            for (int i = 0 ; i < n ; i++ )            {                if ( x >= m)                {                    flag = 1;                    break;                }                if (fabs(b[x] -a[i]) <= mid)                {                    continue;                }                else                 {                    x++;                    i--;                }            }            if ( flag == 1)            {                l = mid + 1;            }            else             {                r = mid;            }            //printf("%I64d\n",r);        }        printf("%I64d\n",l);    }}


D. Road to Post Office
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasiliy has a car and he wants to get from home to the post office. The distance which he needs to pass equals tod kilometers.

Vasiliy's car is not new — it breaks after driven every k kilometers and Vasiliy needs t seconds to repair it. After repairing his car Vasiliy can drive again (but afterk kilometers it will break again, and so on). In the beginning of the trip the car is just from repair station.

To drive one kilometer on car Vasiliy spends a seconds, to walk one kilometer on foot he needsb seconds (a < b).

Your task is to find minimal time after which Vasiliy will be able to reach the post office. Consider that in every moment of time Vasiliy can left his car and start to go on foot.

Input

The first line contains 5 positive integers d, k, a, b, t (1 ≤ d ≤ 1012;1 ≤ k, a, b, t ≤ 106;a < b), where:

  • d — the distance from home to the post office;
  • k — the distance, which car is able to drive before breaking;
  • a — the time, which Vasiliy spends to drive 1 kilometer on his car;
  • b — the time, which Vasiliy spends to walk 1 kilometer on foot;
  • t — the time, which Vasiliy spends to repair his car.
Output

Print the minimal time after which Vasiliy will be able to reach the post office.

Examples
Input
5 2 1 4 10
Output
14
Input
5 2 1 4 5
Output
13
Note

In the first example Vasiliy needs to drive the first 2 kilometers on the car (in 2 seconds) and then to walk on foot 3 kilometers (in 12 seconds). So the answer equals to 14 seconds.

In the second example Vasiliy needs to drive the first 2 kilometers on the car (in 2 seconds), then repair his car (in 5 seconds) and drive 2 kilometers more on the car (in 2 seconds). After that he needs to walk on foot 1 kilometer (in 4 seconds). So the answer equals to 13 seconds.

题目大意:一个人要到银行去,家到银行的距离为d千米,因为他的车子很破,所以每开k公里就需要修理,开车一公里需要花时间为a,走路一公里需要花b,修理时间花t

思路:1.直接分类讨论,可以只开一次车,把车扔了直接步行。还有就是一直开车,还有就是剩下<=k的路程走路。这三种情况比较一下选最小的就是答案。

2。和我一起做的小伙伴的思路是:算出车和人的平均速度,要是车的平均速度大于等于人的平均速度就直接开车,但是要留下 <= k的部分 然后剩下的部分再算只开车和只走路,然后比较取最优

#include <bits/stdc++.h>  using namespace std;  int main()  {      __int64 d,k,a,b,t;      while (~scanf("%I64d%I64d%I64d%I64d%I64d",&d,&k,&a,&b,&t))      {          __int64 res = 0;          if ( d < k)          {              printf("%I64d\n",d * a);              continue;          }          res = k * a;          if ((d - k) % k == 0)          {              res += ((d - k) / k) * t + (d - k) * a;          }          else if ((d - k) % k != 0)          {              res += ((d - k) / k + 1) * t + (d - k) * a;          }          //printf("%I64d\n",res);          __int64 temp = 0;          temp += k * a + (d - k) * b;          if (res > temp)          {              res = temp;          }         // printf("%I64d\n",res);        temp = 0;          temp = k * a;          if ((d - k) % k == 0 && d - k >= k)          {              temp += (((d - k) / k) - 1) * t + (d - k - k ) * a + k * b;          }          else if ((d - k) % k != 0)          {              temp += ((d - k) / k ) * t + (d - k - d % k) * a;              temp +=d % k * b;           }          if (res > temp)          {              res = temp;          }          temp = 0;          temp = (d - k) * b + k * a;          if (temp < res)          {              res = temp;          }          printf("%I64d\n",res);      }      return 0;  }  





0 0
原创粉丝点击