其他分享
首页 > 其他分享> > Codeforces Round #494(Div.3)

Codeforces Round #494(Div.3)

作者:互联网

Codeforces Round #494(Div.3)

A. Polycarp's Pockets

题意:

思路:

#include<bits/stdc++.h>
using namespace std;
int n, vis[100+10];
int main()
{
    cin >> n;
    for(int i = 1, x; i <= n; i++)
    {
        scanf("%d", &x);
        vis[x]++;
    } int ans = 0;
    for(int i = 1; i <= 100+2; i++)
        ans = max(ans, vis[i]);
    cout << ans << endl;
    return 0;
}

B. Binary String Constructing

题意:

思路:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a, b, x;
    cin >> a >> b >> x;
    string ans;
    char u, v;
    if(a > b) u = '0', v = '1';
    else u = '1', v = '0', swap(a, b);
    for(int i = 1; i <= x/2; i++)
        ans += u, ans += v, a--, b--;
    if(x % 2 == 0)
    {
        while(b--) ans += v;
        while(a--) ans += u;
    }
    else
    {
        while(a--) ans += u;
        while(b--) ans += v;
    }
    cout << ans << endl;
}

C. Intense Heat

题意:

思路:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e3 + 10;
double n, k, a[maxn];
//输出15位小数
int main()
{
    double ans = 0;
    cin >> n >> k;
    for(int i = 1; i <= n; i++)
    {
        cin >> a[i];
        a[i] = a[i-1] + a[i];
    }
    //枚举长度
    for(int i = k; i <= n; i++)
        //枚举右端点
        for(int j = i; j <= n; j++)
        ans = max(ans, (a[j]-a[j-i])/i);
    printf("%.15f\n", ans);
    return 0;
}

D. Coins and Queries

题意:

思路:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
int n, q;
int sum[35];

int main()
{
    scanf("%d%d", &n, &q);
    for(int i = 1, x; i <= n; i++)
    {
        scanf("%d", &x);
        int t = log2(x);
        sum[t]++;
    }
    while(q--)
    {
        int x, ans = 0;
        scanf("%d", &x);
        for(int i = 31; i >= 0; i--)
        {
            int cur = min(sum[i], x/(1<<i));
            ans += cur;
            x -=(1<<i)*cur;
        }
        if(x != 0) puts("-1");
        else cout << ans << endl;
    }
    return 0;
}

E. Tree Constructing

题意:

思路:

#include<bits/stdc++.h>
using namespace std;
int n, d, k;
int cnt;
vector<pair<int, int>> ans;

void bfs(int depth, int fa, int deg)
{
    if(depth <= 0) return;
    if(cnt == n+1) return;
    queue<pair<int, int>> q;
    for(int i = 1; i <= deg; i++)
    {
        ans.push_back({fa, cnt});
        q.push({cnt, 1});
        cnt++;
        if(cnt == n+1) return;
    }
    depth--;//已经用了一层了
    while(q.size())
    {
        int x = q.front().first, d = q.front().second;
        if(d == depth+1) return; q.pop();
        for(int i = 1; i <= deg+1; i++)
        {
            ans.push_back({x, cnt});
            q.push({cnt, d+1});
            cnt++;
            if(cnt == n+1) return;
        }
    }
}

int main()
{
    scanf("%d%d%d", &n, &d, &k);
    cnt = 1;
    if(d >= n || (d != k && k == 1))
    {
        puts("NO");
        return 0;
    }

    //先直接构造出链条
    for(int i = 1; i <= d; i++)
        ans.push_back({cnt, cnt+1}), cnt++;
    //往枝杈上插入点
    cnt++;
    for(int i = 2, j = d; i <= j; i++, j--)
    {
        int depth = i - 1; //当前这个点能插入深度多少的叶子
        bfs(depth, i, k-2);
        if(i != j) bfs(depth, j, k-2);
        if(cnt == n+1) break;
    }
    if(cnt != n+1) puts("NO");
    else
    {
        puts("YES");
        for(auto x : ans)
            printf("%d %d\n", x.first, x.second);
    }
    return 0;
}

F. Abbreviation

题意:

思路:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 300 + 10;
int f[maxn][maxn];
int n;
int len;
vector<string> a;
int main()
{
    scanf("%d", &n);
    int len = n-1;
    for(int i = 0; i < n; i++)
    {
        string str; cin >> str;
        a.push_back(str);
        len += str.size();
    }

    for(int i = n - 1; i >= 0; i--)
        for(int j = n-1; j >= 0; j--)
        {
            if(a[i] == a[j])
                if(i + 1 < n && j + 1 < n)
                    f[i][j] = f[i+1][j+1]+1;
                else f[i][j] = 1;
        }
    int ans = len;
    for(int i = 0; i < n; i++) //枚举开头
    {
        int sum = 0;
        for(int j = 0; i + j < n; j++) //枚举长度
        {
            sum += a[i+j].size(); //长度为j时的长度和
            int cnt = 1;
            for(int pos = i+j+1; pos < n; pos++)
                if(f[i][pos] > j) //相同 则可以简化
            {
                ++cnt;  //相同的串加一
                pos += j; //跳过这个区间
            }
            //当前计算的结果 = 总长度 - 长度为j时的长度*有多少个这样的区间 + 区间数
            int cur = len - sum*cnt + cnt;
            if(cnt > 1 && ans > cur) ans = cur;
        }
    } cout << ans << endl;
    return 0;
}

标签:cnt,题意,using,int,Codeforces,长度,494,Div.3,include
来源: https://www.cnblogs.com/zxytxdy/p/12202780.html