K键盘里的青春K

我曾经失去的荣耀,我定加倍夺回

Educational Codeforces Round 42 BCD

好久没做cf,四道题读错2道你敢信- -

B. Students in Railway Carriage
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are nn consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger.

The university team for the Olympiad consists of aa student-programmers and bb student-athletes. Determine the largest number of students from all a+ba+b students, which you can put in the railway carriage so that:

  • no student-programmer is sitting next to the student-programmer;
  • and no student-athlete is sitting next to the student-athlete.

In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting.

Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all).

Input

The first line contain three integers nnaa and bb (1n21051≤n≤2⋅1050a,b21050≤a,b≤2⋅105a+b>0a+b>0) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes.

The second line contains a string with length nn, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member.

Output

Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete.

Examples
input
Copy
5 1 1
*...*
output
Copy
2
input
Copy
6 2 3
*...*.
output
Copy
4
input
Copy
11 3 10
.*....**.*.
output
Copy
7
input
Copy
3 2 3
***
output
Copy
0
Note

In the first example you can put all student, for example, in the following way: *.AB*

In the second example you can put four students, for example, in the following way: *BAB*B

In the third example you can put seven students, for example, in the following way: B*ABAB**A*B

The letter A means a student-programmer, and the letter B — student-athlete.


   题意是输入n,a,b,表示有n个字符,a个A,b个B,字符串中'.'表示空位,A B不能相连,问最多能放多少个A和B。

思路:暴力遍历,对于连续的偶数个空位无所谓,奇数个就贪心一下,把第一个放多的,也不用想着怎么直接算出来- -,直接暴力就好了啊,两种写法

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 2e5 + 10;
char str[maxn];
int main()
{
    int n, a, b;
    while(~scanf("%d%d%d",  &n, &a, &b))
    {
        scanf(" %s", str);
        int len = strlen(str);
        int ans = 0, flag;
        if(a > b) flag = 0;
        else flag = 1;
        for(int i = 0; i < len; i++)
        {
            if(str[i] == '.')
            {
                if(flag)
                {
                    b--; flag = 0;
                    if(b >= 0) ans++;
                }
                else if(flag == 0)
                {
                    a--; flag = 1;
                    if(a >= 0) ans++;
                }
            }
            if(str[i] == '*')
            {
                if(a < b) flag = 1;
                else flag = 0;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

这种就比较巧妙了

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 2e5 + 5;
char str[maxn];
int main()
{
    int n, a, b;
    while(~scanf("%d%d%d",  &n, &a, &b))
    {
        scanf(" %s", str);
        int len = strlen(str);
        int ans = 0, cnt = 0;
        for(int i = 0; i <= len; i++)
        {
            if(str[i] == '.')
                cnt++;
            else
            {
                if(a > b) swap(a, b);
                ans += min(a, cnt/2);
                a -= min(a, cnt/2);
                cnt -= cnt/2;
                ans += min(b, cnt);
                b -= min(b, cnt);
                cnt = 0;
            }
        }
        cout << ans << endl;
    }
    return 0;
}
C. Make a Square
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a positive integer nn, written without leading zeroes (for example, the number 04 is incorrect).

In one operation you can delete any digit of the given integer so that the result remains a positive integer without leading zeros.

Determine the minimum number of operations that you need to consistently apply to the given integer nn to make from it the square of some positive integer or report that it is impossible.

An integer xx is the square of some positive integer if and only if x=y2x=y2 for some positive integer yy.

Input

The first line contains a single integer nn (1n21091≤n≤2⋅109). The number is given without leading zeroes.

Output

If it is impossible to make the square of some positive integer from nn, print -1. In the other case, print the minimal number of operations required to do it.

Examples
input
Copy
8314
output
Copy
2
input
Copy
625
output
Copy
0
input
Copy
333
output
Copy
-1
Note

In the first example we should delete from 83148314 the digits 33 and 44. After that 83148314 become equals to 8181, which is the square of the integer 99.

In the second example the given 625625 is the square of the integer 2525, so you should not delete anything.

In the third example it is impossible to make the square from 333333, so the answer is -1.

题意:最少删掉几个数字可以让剩下的是一个平方数,并且删掉后的数字不能有前导0

思路:这题一眼bfs, 然而有前导0就比较复杂了, 最直接的思路就是bfs+优先队列一下,其实也可以暴力枚举y,让y*y <= n,然后暴力按顺序匹配y与n,看能不能通过删掉字符得出y,维护一个最小值。这样就不存在前导0的问题了

bfs+优先队列代码:

#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <queue>
#include <cmath>
using namespace std;
long long po[20];
struct node
{
    string s;
    int step;
    node(){}
    node(string ss, int st)
    {
        s = ss;
        step = st;
    }
    bool operator < (const node &a) const
    {
        return step > a.step;
    }
};
int bfs(string s)
{
    priority_queue<node> q;
    q.push(node(s, 0));
    while(!q.empty())
    {
        node p = q.top();
        q.pop();
        string tmp = p.s;
        int num = 0;
//        cout << tmp << endl;
        int len = tmp.size();
        for(int i = len-1; i >= 0; i--)
            num += po[len-1-i]*(tmp[i]-'0');
//        cout << num << endl;
        int x = sqrt(num);
        if(x * x == num) return p.step;
        for(int i = 0; i < len; i++)
        {
            string t;
            for(int j = 0; j < len; j++)
                if(j != i)
                    t += tmp[j];
            if(t == "") continue;
            int cur = p.step+1;
            int flag = 0;
            for(int i = 0; i < t.size(); i++)
            {
                if(t[i] == '0')
                {
                    if(i == t.size()-1) flag = 1;
                    cur++;
                }
                else break;
            }
            if(flag) continue;
            q.push(node(t, cur));
        }
    }
    return -1;
}
int main()
{
    po[0] = 1;
    for(int i = 1; i <= 12; i++)
        po[i] = po[i-1] * 10;
    string s;
    while(cin >> s)
    {
        printf("%d\n",bfs(s));
    }
    return 0;
}
D. Merge Equals
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array of positive integers. While there are at least two equal elements, we will perform the following operation. We choose the smallest value xx that occurs in the array 22 or more times. Take the first two occurrences of xx in this array (the two leftmost occurrences). Remove the left of these two occurrences, and the right one is replaced by the sum of this two values (that is, 2x2⋅x).

Determine how the array will look after described operations are performed.

For example, consider the given array looks like [3,4,1,2,2,1,1][3,4,1,2,2,1,1]. It will be changed in the following way: [3,4,1,2,2,1,1]  [3,4,2,2,2,1]  [3,4,4,2,1]  [3,8,2,1][3,4,1,2,2,1,1] → [3,4,2,2,2,1] → [3,4,4,2,1] → [3,8,2,1].

If the given array is look like [1,1,3,1,1][1,1,3,1,1] it will be changed in the following way: [1,1,3,1,1]  [2,3,1,1]  [2,3,2]  [3,4][1,1,3,1,1] → [2,3,1,1] → [2,3,2] → [3,4].

Input

The first line contains a single integer nn (2n1500002≤n≤150000) — the number of elements in the array.

The second line contains a sequence from nn elements a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109) — the elements of the array.

Output

In the first line print an integer kk — the number of elements in the array after all the performed operations. In the second line print kkintegers — the elements of the array after all the performed operations.

Examples
input
Copy
7
3 4 1 2 2 1 1
output
Copy
4
3 8 2 1 
input
Copy
5
1 1 3 1 1
output
Copy
2
3 4 
input
Copy
5
10 40 20 50 30
output
Copy
5
10 40 20 50 30 
Note

The first two examples were considered in the statement.

In the third example all integers in the given array are distinct, so it will not change.

题意:给你一个数组,保证至少有一个数字重复至少2次,每次找出个数>=2最小的那个数字的最左面两个,把最左面的删掉,靠右的*2,问最后的数组剩下什么

思路:这题没读错- -,很快a了, 一个优先队列维护数组,一个vector存答案就好了

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
int a[maxn];
struct node
{
    ll val, index;
    node(){}
    node(ll ii, ll vv) : index(ii), val(vv){}
    bool operator < (const node &a) const
    {
        if(val == a.val)
            return index > a.index;
        else
            return val > a.val;
    }
};
//struct node1
//{
//    int val, index;
//    node1(){}
//    node1(int ii, int vv) : index(ii), val(vv){}
//    bool operator < (const node1 &a) const
//    {
//        return index < a.index;
//    }
//};
int cmp(node a, node b)
{
    return a.index < b.index;
}
int main()
{
    int n;
    while(~scanf("%d", &n))
    {
        priority_queue<node> pq;
        vector<node> ans;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &a[i]);
            pq.push(node(i, a[i]));
        }
        while(pq.size())
        {
            node p = pq.top();
            pq.pop();
            if(pq.empty())
            {
                ans.push_back(p);
                break;
            }
            if(p.val != pq.top().val)
                ans.push_back(p);
            else
            {
                p = pq.top();
                pq.pop();
                p.val *= 2;
                pq.push(p);
            }
        }
        printf("%d\n", ans.size());
        sort(ans.begin(), ans.end(),cmp);
        for(int i = 0; i < ans.size(); i++)
            printf("%lld ", ans[i].val);
        cout << endl;
    }
    return 0;
}


阅读更多
版权声明:希望我的博客可以为别人带去知识与便利,让那些像我曾经一样迷茫的小伙伴不再迷茫~ https://blog.csdn.net/qq_34374664/article/details/79978073
个人分类: CF
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Educational Codeforces Round 42 BCD

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭