Codeforces 980 并查集/模拟贪心最小字典序

Posted Aragaki

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 980 并查集/模拟贪心最小字典序相关的知识,希望对你有一定的参考价值。

A

技术分享图片
/*Huyyt*/
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll LLmaxn = 2e18;
int main()
{
        string a;
        cin >> a;
        int b=0, c=0;
        for (int i = 0; i < a.size(); i++)
        {
                if (a[i] == -)
                {
                        b++;
                }
                else
                {
                        c++;
                }
        }
        //cout<<b<<" "<<c<<endl;
        if (c == 0)
        {
                cout << "YES" << endl;
                return 0;
        }
        if (b % c != 0)
        {
                cout << "NO" << endl;
        }
        else
        {
                cout << "YES" << endl;
        }
        return 0;
}
View Code

B

解:

注意只要是上下对称或者是左右对称就可以使得 1-4 有一条路径的话 2-3 也有相对应的一条路径

剩下的就容易构造了

技术分享图片
/*Huyyt*/
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll LLmaxn = 2e18;
char ans[5][105];
int main()
{
        int n, k;
        cin >> n >> k;
        for (int i = 1; i <= 4; i++)
        {
                for (int j = 1; j <= n; j++)
                {
                        ans[i][j] = .;
                }
        }
        int sum = (n - 2) * 2;
        if (k > sum)
        {
                cout << "NO" << endl;
                return 0;
        }
        cout << "YES" << endl;
        if (k % 2 == 0)
        {
                int cur = k;
                for (int j = 2; j <= n - 1 && cur; j++)
                {
                        for (int i = 2; i <= 3 && cur; i++)
                        {
                                ans[i][j] = #;
                                cur--;
                        }
                }
        }
        else
        {
                k--;
                ans[2][n / 2 + 1] = #;
                for (int i = 2; i <= 3 && k; i++)
                {
                        for (int j = 2; j <= n / 2 && k; j++)
                        {
                                ans[i][j] = ans[i][n - j + 1] = #;
                                k -= 2;
                        }
                }
        }
        for (int i = 1; i <= 4; i++)
        {
                for (int j = 1; j <= n; j++)
                {
                        cout << ans[i][j];
                }
                cout << endl;
        }
        return 0;
}
View Code

C

解:

可以用并查集维护每个点最小到达的地方 也因为N<=1E5,K<=256 直接暴力模拟也可以

技术分享图片
/*Huyyt*/
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll LLmaxn = 2e18;
char ans[5][105];
int main()
{
        int n, k;
        cin >> n >> k;
        for (int i = 1; i <= 4; i++)
        {
                for (int j = 1; j <= n; j++)
                {
                        ans[i][j] = .;
                }
        }
        int sum = (n - 2) * 2;
        if (k > sum)
        {
                cout << "NO" << endl;
                return 0;
        }
        cout << "YES" << endl;
        if (k % 2 == 0)
        {
                int cur = k;
                for (int j = 2; j <= n - 1 && cur; j++)
                {
                        for (int i = 2; i <= 3 && cur; i++)
                        {
                                ans[i][j] = #;
                                cur--;
                        }
                }
        }
        else
        {
                k--;
                ans[2][n / 2 + 1] = #;
                for (int i = 2; i <= 3 && k; i++)
                {
                        for (int j = 2; j <= n / 2 && k; j++)
                        {
                                ans[i][j] = ans[i][n - j + 1] = #;
                                k -= 2;
                        }
                }
        }
        for (int i = 1; i <= 4; i++)
        {
                for (int j = 1; j <= n; j++)
                {
                        cout << ans[i][j];
                }
                cout << endl;
        }
        return 0;
}
View Code

D

卡题意

 

E

 

以上是关于Codeforces 980 并查集/模拟贪心最小字典序的主要内容,如果未能解决你的问题,请参考以下文章

P3366 模板最小生成树 (贪心+并查集,kruskal)

P3366 模板最小生成树 (贪心+并查集,kruskal)

CodeForces - 891C: Envy(可撤销的并查集&最小生成树)

CodeForces827 D. Best Edge Weight 最小生成树+倍增LCA+并查集

模板——最小生成树kruskal算法+并查集数据结构

CodeForces - 1209D Cow and Snacks 并查集