Codeforces Round #605(Div3)A~E

时间:2020-01-31 19:17:05   收藏:0   阅读:81

Codeforces Round #605(Div3)A~E

A. Three Friends

题意:

思路:

#include<bits/stdc++.h>
using namespace std;
void solve()
{
    int a[3];
    for(int i = 0; i < 3; i++)
        cin >> a[i];
    sort(a, a+3);
    if(a[0] < a[2]) a[0]++;
    if(a[2] > a[0]) a[2]--;
    cout << 2*(a[2]-a[0]) << endl;
}

int main()
{
    int T; scanf("%d", &T);
    while(T--) solve();
    return 0;
}

B. Snow Walking Robot

题意:

思路:

#include<bits/stdc++.h>
using namespace std;

void solve()
{
    string str;
    cin >> str;
    int l, r, u, d;
    l = r = u = d = 0;
    for(auto x : str)
    {
        if(x == 'L') l++;
        if(x == 'R') r++;
        if(x == 'U') u++;
        if(x == 'D') d++;
    }
    int num1 = min(l, r);
    int num2 = min(u, d);

    if(num1 == 0 || num2 == 0)
    {
        num1 = min(num1, 1);
        num2 = min(num2, 1);
    }

    string ans = "";
    for(int i = 1; i++ <= num1;)
        ans += 'L';
    for(int i = 1; i++ <= num2;)
        ans += 'U';
    for(int i = 1; i++ <= num1;)
        ans += 'R';
    for(int i = 1; i++ <= num2;)
        ans += 'D';
    cout << ans.size() << endl;
    cout << ans << endl;
}

int main()
{
    int T; scanf("%d", &T);
    while(T--) solve();
    return 0;
}

C. Yet Another Broken Keyboard

题意:

思路:

#include<bits/stdc++.h>
using namespace std;
int n, k;
int vis[30]; //可以用的键位
string str;

int main()
{
    int n, k;
    cin >> n >> k;
    cin >> str;
    for(int i = 1; i <= k; i++)
    {
        char c[2];
        scanf("%s", c);
        vis[c[0]-'a']++;
    }
    //有溢出风险, 记得开longlong
    long long ans = 0;
    for(int i = 0; i < n; i++)
    {
        if(vis[str[i]-'a']) //如果当前位置可以
        {
            long long num = 0; //统计连续的可以的串
            for(int j = i; j < n; j++)
            {
                i = j;
                if(vis[str[j]-'a']) num++;
                else break;
            }
            ans += num*(num+1) / 2;
        }
    }
    cout << ans << endl;
    return 0;
}

D. Remove One Element

题意:

思路:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n, a[maxn], ans;
int f[maxn], g[maxn];
int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
        scanf("%d", &a[i]);

    f[1] = ans = 1;
    for(int i = 2; i <= n; i++)
    {
        f[i] = 1;
        if(a[i] > a[i-1]) f[i] = f[i-1]+1;
        ans = max(f[i], ans);
    }
    
    g[n] = 1;
    for(int i = n-1; i >= 1; i--)
    {
        g[i] = 1;
        if(a[i] < a[i+1]) g[i] = g[i+1]+1;
    }
    ans = max(ans, g[1]);
    for(int i = 2; i <= n-1; i++)
        if(a[i+1]>a[i-1])
        ans = max(ans, f[i-1]+g[i+1]);
    ans = max(ans, f[n]);
    cout << ans << endl;
    return 0;
}

E. Nearest Opposite Parity

题意:

思路:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int a[maxn], n;

int head[maxn], nex[maxn<<2], ver[maxn<<2], tot;
inline void add_edge(int x, int y){
    ver[++tot] = y; nex[tot] = head[x]; head[x] = tot;
}

int path[maxn][3];

void bfs(int dir)
{
    queue<int> q;
    for(int i = 1; i <= n; i++)
    {
        if(a[i] % 2 == dir)
        {
            path[i][dir] = 0;
            q.push(i);
        } else {
            path[i][dir] = -1;
        }
    }

    while(q.size())
    {
        int x = q.front(); q.pop();
        for(int i = head[x]; i; i = nex[i])
        {
            int y = ver[i];
            if(path[y][dir] == -1)
            {   //代表是从奇偶性不同的点转移过来的
                path[y][dir] = path[x][dir]+1;
                q.push(y);
            }
        }
    }
}


int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    for(int i = 1; i <= n; i++)
    {
        if(i + a[i] <= n) add_edge(i+a[i], i);
        if(i - a[i] >= 1) add_edge(i-a[i], i);
    }

    bfs(0); bfs(1);
    for(int i = 1; i <= n; i++)
    {
        if(a[i] % 2) printf("%d ", path[i][0]);
       else printf("%d ", path[i][1]);
    }
    return 0;
}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!