CF Educational Codeforces Round 15(A~D)

本文介绍了三个编程问题:1. 找到数组中最大递增子序列的长度;2. 计算有多少对数的和为2的幂;3. 最小覆盖半径以覆盖所有城镇的基站问题。每个问题都提供了相应的解题思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接: 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
5
1 7 2 11 15
Output
3
Input
6
100 100 100 100 100 100
Output
1
Input
3
1 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
4
7 3 2 1
Output
2
Input
3
1 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) (where i < 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 3
1 5 10 14 17
4 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;  
}  



 


### Codeforces Educational Round 26 比赛详情 Codeforces是一个面向全球程序员的比赛平台,其中Educational Rounds旨在帮助参与者提高算法技能并学习新技巧。对于具体的Educational Round 26而言,这类比赛通常具有如下特点: - **时间限制**:每道题目的解答需在规定时间内完成,一般为1秒。 - **内存限制**:程序运行所占用的最大内存量被限定,通常是256兆字节。 - 输入输出方式标准化,即通过标准输入读取数据并通过标准输出打印结果。 然而,关于Educational Round 26的具体题目细节并未直接提及于提供的参考资料中。为了提供更精确的信息,下面基于以往的教育轮次给出一些常见的题目类型及其解决方案思路[^1]。 ### 题目示例与解析 虽然无法确切描述Educational Round 26中的具体问题,但可以根据过往的经验推测可能涉及的问题类别以及解决这些问题的一般方法论。 #### 类型一:贪心策略的应用 考虑一个问题场景,在该场景下需要照亮一系列连续排列的对象。假设存在若干光源能够覆盖一定范围内的对象,则可以通过遍历整个序列,并利用贪心的思想决定何时放置新的光源以确保所有目标都被有效照射到。这种情况下,重要的是保持追踪当前最远可到达位置,并据此做出决策。 ```cpp #include <bits/stdc++.h> using namespace std; bool solve(vector<int>& a) { int maxReach = 0; for (size_t i = 0; i < a.size(); ++i) { if (maxReach < i && !a[i]) return false; if (a[i]) maxReach = max(maxReach, static_cast<int>(i) + a[i]); } return true; } ``` #### 类型二:栈结构处理匹配关系 另一个常见问题是涉及到成对出现元素之间的关联性判断,比如括号表达式的合法性验证。这里可以采用`<int>`类型的栈来记录左括号的位置索引;每当遇到右括号时就弹出最近一次压入栈底的那个数值作为配对依据,进而计算两者间的跨度长度累加至总数之中[^2]。 ```cpp #include <stack> long long calculateParens(const string& s) { stack<long long> positions; long long num = 0; for(long long i = 0 ; i<s.length() ;++i){ char c=s[i]; if(c=='('){ positions.push(i); }else{ if(!positions.empty()){ auto pos=positions.top(); positions.pop(); num+=i-pos; } } } return num; } ``` #### 类型三:特定模式下的枚举法 针对某些特殊条件约束下的计数类问题,如寻找符合条件的三位整数的数量。此时可通过列举所有可能性的方式逐一检验是否符合给定规则,从而统计满足要求的结果数目。例如求解形如\(abc\)形式且不含重复数字的正整数集合大小[^3]。 ```cpp vector<int> generateSpecialNumbers(int n) { vector<int> result; for (int i = 1; i <= min(n / 100, 9); ++i) for (int j = 0; j <= min((n - 100 * i) / 10, 9); ++j) for (int k = 0; k <= min(n % 10, 9); ++k) if ((100*i + 10*j + k)<=n&&!(i==0||j==0)) result.emplace_back(100*i+10*j+k); sort(begin(result), end(result)); return result; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值