codeforces round #612解题报告A~D

时间:2020-01-31 19:18:21   收藏:0   阅读:85

codeforces round #612解题报告A~D

A. Angry Students

#include<bits/stdc++.h>
using namespace std;
void solve()
{
    int n;
    cin >> n;
    string str;
    cin >> str;
    int ans = 0;
    for(int i = 0; i < n; i++)
    {
        if(str[i] == 'A')
        {
            for(int j = i+1; j < n; j++)
            {
                if(str[j] == 'A') break;
                else ans = max(ans, j - i);
            }
        }
    }
    cout << ans << endl;
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--) solve();
    return 0;
}

B. Hyperset

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1500 + 10;
typedef long long ll;
string s[maxn];
int n, k;
map<string, int> mp;
int main()
{
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++)
    {
        cin >> s[i];
        mp[s[i]]++;
    }
    int all = 'S' + 'E' + 'T';
    int ans = 0;
    for(int i = 1; i <= n; i++)
    {
        for(int j = i+1; j <= n; j++)
        {
            string tmp = "";
            for(int t = 0; t < k; t++)
            {
                if(s[i][t] == s[j][t])
                    tmp += s[i][t];
                else tmp += all - s[i][t] - s[j][t];
            }
            ans += mp[tmp];
        }
    } cout << ans / 3 << endl;
    return 0;
}

C. Garland

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e2 + 10;
int a[maxn], n;
int f[maxn][maxn][maxn][3];
int odd, even;

int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    even = n/2, odd = n-even;
    memset(f, 0x3f, sizeof(f));
    f[0][0][0][0] = f[0][0][0][1] = 0;
    int k;
    for(int i = 1; i <= n; i++)
    {
        if(a[i])
        {
            if(a[i]&1)
            {
                for(int j = 1; j <= i; j++)
                {
                    k = i - j;
                    f[i][j][k][1] = min(f[i-1][j-1][k][1], f[i-1][j-1][k][0]+1);
                }
            }
            else
            {
                for(int j = 0; j < i; j++)
                {
                    k = i - j;
                    f[i][j][k][0] = min(f[i-1][j][k-1][0], f[i-1][j][k-1][1]+1);
                }
            }
        }
        else
        {
            for(int j = 1; j <= i; j++)
            {
                k = i - j;
                f[i][j][k][1] = min(f[i-1][j-1][k][1], f[i-1][j-1][k][0]+1);
            }
            for(int j = 0; j < i; j++)
            {
                k = i - j;
                f[i][j][k][0] = min(f[i-1][j][k-1][0], f[i-1][j][k-1][1]+1);
            }
        }
    }
    cout << min(f[n][odd][even][0], f[n][odd][even][1]) << endl;
    return 0;
}

D. Numbers on Tree

题意:

思路:

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

vector<int> dfs(int x)
{
    vector<int> ans, res; ans.clear();
    for(auto y : son[x])
    {
        vector<int> tmp = dfs(y);
        for(auto t : tmp)
            ans.push_back(t);
    }

    if(ans.size() < c[x]) {puts("NO"); exit(0);}
    for(int i = 0; i < c[x]; i++) res.push_back(ans[i]);
    res.push_back(x);
    for(int i = c[x]; i < ans.size(); i++) res.push_back(ans[i]);
    return res;
}

int main()
{
    scanf("%d", &n);
    for(int i = 1, f, w; i <= n; i++)
    {
        scanf("%d%d", &f, &w);
        c[i] = w; son[f].push_back(i);
    } vector<int> ans = dfs(0);
    puts("YES");
    for(int i = 0; i < ans.size(); i++)
        a[ans[i]] = i+1;
    for(int i = 1; i <= n; i++)
        printf("%d ", a[i]);
    return 0;
}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!