ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Educational Codeforces Round 131 (Rated for Div. 2) A - D

2022-07-10 01:31:47  阅读:132  来源: 互联网

标签:Educational Rated const int ll Codeforces long ans include


传送门

A - Grass Field

每次清除一行一列,最多也就清除 2 次,判断一下就好了

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <deque>
#include <stack>
#include <array>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
const ll maxn = 2e5 + 10;
const ll inf = 1e17 + 10;

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int f = 0;
        for(int i=0; i<4; i++)
        {
            int x;
            scanf("%d", &x);
            f += x > 0;
        }
        int ans = 0;
        if(f == 4) ans = 2;
        else if(f) ans = 1;
        printf("%d\n", ans);

    }

    return 0;
}

B - Permutation

一开始看错题,疑惑半天

答案最多的肯定是 \(d = 2\) 的情况,因为拥有因数 \(2\) 的数字是最多的

直接构造就好了,从奇数开始不断地乘 \(2\),直到当前数字大于 \(n\),然后进行下一个奇数

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <deque>
#include <stack>
#include <array>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
const ll maxn = 2e5 + 10;
const ll inf = 1e17 + 10;
int vis[maxn];

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n;
        scanf("%d", &n);
        for(int i=0; i<=n; i++) vis[i] = 0;
        int tp = 1, now = 1, ans = 0;
        printf("2\n");
        for(int i=0; i<n; i++)
        {
            if(now > n)
            {
                while(vis[tp]) tp++;
                now = tp;
            }
            vis[now] = 1;
            if(i) printf(" ");
            printf("%d", now);
            now <<= 1;
        }
        printf("\n");
    }
    return 0;
}

C - Schedule Management

二分答案

显然答案具备单调性,直接二分,\(check()\) 的时候,优先让工人自己先干自己的活

  • 如果有时间溢出,则能多干 \(last / 2\) 的工作

  • 如果不够时间,则记录还有多少活要干

如果 wa4 记得开 long long(悲

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <deque>
#include <stack>
#include <array>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
const ll maxn = 2e5 + 10;
const ll inf = 1e17 + 10;
int n, m;
ll vis[maxn];

bool check(ll x)
{
    ll ans = 0;
    for(int i=1; i<=n; i++)
    {
        if(x > vis[i]) ans += (x - vis[i]) >> 1;
        else ans -= vis[i] - x;
    }
    return ans >= 0;
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d", &n, &m);
        for(int i=0; i<=n; i++) vis[i] = 0;
        for(int i=0; i<m; i++)
        {
            int x;
            scanf("%d", &x);
            vis[x]++;
        }
        ll l = 0, r = m * 2;
        while(l < r)
        {
            ll mid = l + r >> 1;
            if(check(mid)) r = mid;
            else l = mid + 1;
        }
        printf("%lld\n", l);
    }
    return 0;
}

D - Permutation Restoration

贪心

很类似于调度问题,考虑好贪心优先级即可

显然对于每个位置 \(i\) 我们可以根据现有的 \(i\) 和 \(b_i\) 来推算出可行的 \(a_i\) 的范围,是一段连续的区间,我们假设为 \([l_i, r_i]\)

题目说了答案必有解,因此从贪心的考虑来讲,只用考虑如何将 \(1 - n\) 分配给每一个 \(a_i\)

从 \(1\) 开始,如果有能够填入的地方,也就是可行区间的 \(l_i \leq 1\) 的,我们贪心地按照其 \(r_i\) 从小到大排序,因为 \(r_i\) 较小的区间显然要分配的更加早一些,如果晚了,那么说明会有一个 \(r_j\) 更大的抢走了原本能分配的,例如:\([1,2]\) 和 \([1,1]\),显然我们要先将 \(1\) 分配给 \([1, 1]\)

因此我们可以使用一个优先队列,队头为 \(r_i\) 较小的,每次想要分配 \(i\) 时,都将 \(l_j = i\) 的区间加入优先队列,然后在这些可分配的区间中将 \(i\) 分配给 \(r_j\) 较小的区间

一开始以为是什么二分图匹配,然后网络流和二分图显然复杂度都不对,后来开始觉得是贪心问题的时候已经乱掉了,赛后才过

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <deque>
#include <stack>
#include <array>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
const ll maxn = 5e5 + 10;
const ll inf = 1e17 + 10;
int ans[maxn], vis[maxn];

struct node
{
    int l, r, id;
    node(){}
    node(int _l, int _r, int _id){l = _l; r = _r; id = _id;}
    bool operator < (const node& a)const
    {
        return a.r < r;
    }
}num[maxn];

bool cmp(const node& a, const node &b)
{
    return a.l < b.l;
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n;
        scanf("%d", &n);
        for(int i=1; i<=n; i++)
        {
            int x;
            scanf("%d", &x);
            if(x == 0)
                num[i] = node(i + 1, n, i);
            else
                num[i] = node(i / (x + 1) + 1, i / x, i);
        }
        sort(num + 1, num + n + 1, cmp);
        priority_queue<node>q;
        int tp = 1;
        for(int i=1; i<=n; i++)
        {
            while(tp <= n && num[tp].l <= i) q.push(num[tp++]);
            node now = q.top();
            q.pop();
            ans[now.id] = i;
        }
        for(int i=1; i<=n; i++)
        {
            if(i != 1) printf(" ");
            printf("%d", ans[i]);
        }
        printf("\n");
    }
    return 0;
}

标签:Educational,Rated,const,int,ll,Codeforces,long,ans,include
来源: https://www.cnblogs.com/dgsvygd/p/16462402.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有