其他分享
首页 > 其他分享> > Codeforces Round #611Div3

Codeforces Round #611Div3

作者:互联网

Codeforces Round #611Div3 解题报告

A. Minutes Before the New Year

#include<bits/stdc++.h>
#define PII pair<int, int>
using namespace std;
typedef long long ll;
int T, n;
void solve()
{
    int h, m;
    cin >> h >> m;
    int ans = 0;
    ans += (24 - h - 1) * 60;
    ans += (60 - m);
    cout << ans << endl;
}
int main()
{
    scanf("%d", &T);
    while(T--) solve();
    return 0;
}

B. Candies Division

#include<bits/stdc++.h>
#define PII pair<int, int>
using namespace std;
typedef long long ll;
int T, n, k;
void solve()
{
    cin >> n >> k;
    if(n%k == 0){
        cout << n << endl;
        return;
    }
    int a = n / k; //每个人a个糖
    int b = n % k;
    if(b <= k / 2) cout << a*k+b << endl;
    else cout << a*k + k / 2 << endl;
}
int main()
{
    scanf("%d", &T);
    while(T--) solve();
    return 0;
}

C. Friends and Gifts

#include<bits/stdc++.h>
#define PII pair<int, int>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
int n, a[maxn];
int vis[maxn];
vector<int> z;
int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &a[i]);
        vis[a[i]] = 1;
        if(a[i] == 0) z.push_back(i);
    }

    int cnt = 1;
    for(int i = n; i >= 1; i--)
    {
        if(a[i]) continue;
        while(vis[cnt]) cnt++;
        a[i] = cnt; vis[cnt] = 1;
    }

    for(int i = 0; i < z.size(); i++)
    {
        int num = z[i];
        if(a[num] == num)
        {
            if(i < z.size()-1) swap(a[num], a[z[i+1]]);
            else swap(a[num], a[z[i-1]]);
            break;
        }
    }

    for(int i = 1; i <= n; i++)
        printf("%d ", a[i]);
    return 0;
}

D. Christmas Trees

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
queue<int> q;
map<int, int> mp;
int n, m;
int main()
{
    ios::sync_with_stdio(false);
    cin >> n >> m;
    for(int i = 1, x; i <= n; i++)
    {
        cin >> x;
        q.push(x); mp[x] = 0;
    }

    long long res = 0;
    vector<int> ans;
    while(m)
    {
        int p = q.front(); q.pop();
        if(!mp.count(p-1))
        {
            q.push(p-1);
            mp[p-1] = mp[p]+1;
        }
        if(!mp.count(p+1))
        {
            q.push(p+1);
            mp[p+1] = mp[p]+1;
        }
        if(mp[p] > 0)
        {
            ans.push_back(p);
            res += mp[p];
            m--;
        }
    }
    cout << res << endl;
    for(auto x : ans)
        cout << x << " ";
    puts("");
    return 0;
}

E. New Year Parties

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
int a[maxn], n;
int vis[maxn];
int vis1[maxn];
int vis2[maxn];
bool mi[maxn];
void get_min()
{
    for(int i = 1; i <= n; i++)
    {
        if(vis1[i] && !mi[i])
        {
            if(vis1[i-1])
            {
                vis1[i-1] += vis1[i];
                vis1[i] = 0;
                mi[i-1] = 1;
            }
            else if(vis1[i+1])
            {
                vis1[i+1] += vis1[i];
                vis1[i] = 0;
                mi[i+1] = 1;
            }
            else if(vis1[i+2] && !mi[i+2])
            {
                vis1[i+1] += vis1[i+2];
                vis1[i+1] += vis1[i];
                vis1[i] = vis1[i+2] = 0;
                mi[i+1] = mi[i] = mi[i+2] = 1;
            }
        }
    } int ans = 0;
    for(int i = 0; i <= n+1; i++)
        if(vis1[i]) ans++;
    cout << ans << " ";
}

void get_max()
{
    for(int i = 1; i <= n; i++)
    {
        if(vis[i])
        {
            int num = vis[i];
            if(num >= 3)
            {
                vis2[i-1]++;
                vis2[i]++;
                vis2[i+1]++;
            }
            else if(num == 2)
            {
                //如果上一个位置没有放
                if(!vis2[i-1])
                {
                    vis2[i-1]++;
                    vis2[i]++;
                }
                else {
                    vis2[i]++;
                    vis2[i+1]++;
                }
            }
            else if(num == 1)
            {
                if(!vis2[i-1]) vis2[i-1]++;
                else if(!vis2[i]) vis2[i]++;
                else vis2[i+1]++;
            }
        }
    }
    int ans = 0;
    for(int i = 0; i <= n+1; i++)
        if(vis2[i]) ans++;
    cout << ans << endl;
}

int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &a[i]);
        vis[a[i]]++;
        vis1[a[i]]++;
    }
    get_min();
    get_max();
    return 0;
}

F. DIY Garland

题意描述

思路

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int deg[maxn], n, rt;
vector<int> main_lamp;
int main()
{
    scanf("%d", &n);
    scanf("%d", &rt);
    deg[rt]++;
    main_lamp.push_back(rt);
    for(int i = 2, x; i <= n - 1; i++)
    {
        scanf("%d", &x);
        deg[x]++;
        main_lamp.push_back(x);
    }
    priority_queue<int, vector<int>, greater<int> > q;
    for(int i = 1; i <= n; i++)
        if(deg[i] == 0) q.push(i);
    vector<pair<int, int> > ans;
    while(q.size())
    {
        int x = q.top();
        q.pop();
        int y = main_lamp[main_lamp.size()-1];
        main_lamp.pop_back();
        ans.push_back(make_pair(y, x));
        if(ans.size() == n - 1) break;
        deg[y]--;
        if(deg[y] == 0) q.push(y);
    }
    cout << rt << endl;
    for(int i = ans.size()-1; i >= 0; i--)
        cout << ans[i].first << " " << ans[i].second << endl;
    return 0;
}

标签:vis2,int,Codeforces,long,++,maxn,mp,611Div3,Round
来源: https://www.cnblogs.com/zxytxdy/p/12163896.html