codeforces #584 ABCD

A. Paint the Numbers

Description

Solution

水题

B. Koala and Lights

Description

 Solution

模拟到1e4。

C. Paint the Digits

Description

给出一个序列,将序列每个值染色为1或2。

问能否给出一种染色序列使得12序列为排序后的序列。

Solution

原序列排一遍序比较。

扫两遍,第一次染色1,第二次染色2。

判断染色序列是否完整覆盖原序列。

  1 #include <algorithm>
  2 #include <cctype>
  3 #include <cmath>
  4 #include <cstdio>
  5 #include <cstdlib>
  6 #include <cstring>
  7 #include <iostream>
  8 #include <map>
  9 #include <numeric>
 10 #include <queue>
 11 #include <set>
 12 #include <stack>
 13 #if __cplusplus >= 201103L
 14 #include <unordered_map>
 15 #include <unordered_set>
 16 #endif
 17 #include <vector>
 18 #define lson rt << 1, l, mid
 19 #define rson rt << 1 | 1, mid + 1, r
 20 #define LONG_LONG_MAX 9223372036854775807LL
 21 #define pblank putchar(' ')
 22 #define ll LL
 23 #define fastIO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
 24 using namespace std;
 25 typedef long long ll;
 26 typedef long double ld;
 27 typedef unsigned long long ull;
 28 typedef pair<int, int> P;
 29 int n, m, k;
 30 const int maxn = 2e5 + 10;
 31 template <class T>
 32 inline T read()
 33 {
 34     int f = 1;
 35     T ret = 0;
 36     char ch = getchar();
 37     while (!isdigit(ch))
 38     {
 39         if (ch == '-')
 40             f = -1;
 41         ch = getchar();
 42     }
 43     while (isdigit(ch))
 44     {
 45         ret = (ret << 1) + (ret << 3) + ch - '0';
 46         ch = getchar();
 47     }
 48     ret *= f;
 49     return ret;
 50 }
 51 template <class T>
 52 inline void write(T n)
 53 {
 54     if (n < 0)
 55     {
 56         putchar('-');
 57         n = -n;
 58     }
 59     if (n >= 10)
 60     {
 61         write(n / 10);
 62     }
 63     putchar(n % 10 + '0');
 64 }
 65 template <class T>
 66 inline void writeln(const T &n)
 67 {
 68     write(n);
 69     puts("");
 70 }
 71 template <typename T>
 72 void _write(const T &t)
 73 {
 74     write(t);
 75 }
 76 template <typename T, typename... Args>
 77 void _write(const T &t, Args... args)
 78 {
 79     write(t), pblank;
 80     _write(args...);
 81 }
 82 template <typename T, typename... Args>
 83 inline void write_line(const T &t, const Args &... data)
 84 {
 85     _write(t, data...);
 86 }
 87 string s, dist;
 88 int col[maxn];
 89 int main(int argc, char const *argv[])
 90 {
 91 #ifndef ONLINE_JUDGE
 92     freopen("in.txt", "r", stdin);
 93     // freopen("out.txt", "w", stdout);
 94 #endif
 95     fastIO;
 96     int t;
 97     cin >> t;
 98     while (t--)
 99     {
100         cin >> n;
101         cin >> s;
102         dist = s;
103         sort(dist.begin(), dist.end());
104         int now = 0;
105         for (int i = 0; i < n; i++)
106             if (s[i] == dist[now])
107             {
108                 col[i] = 1;
109                 s[i] = '*';
110                 ++now;
111             }
112         for (int i = 0; i < n; i++)
113             if (s[i] == dist[now])
114             {
115                 col[i] = 2;
116                 s[i] = '*';
117                 ++now;
118             }
119         if (now == n)
120         {
121             for (int i = 0; i < n; i++)
122                 cout << col[i];
123             cout << "
";
124         }
125         else
126             cout << "-
";
127     }
128     return 0;
129 }
View Code

D. Cow and Snacks

Description

给出n个零食m位客人。

每个客人由两种爱吃的零食,每种零食只有一个。

一位客人如果吃就会吃掉所有他喜欢的零食。

求一个客人吃零食序列,使得没有零食吃的客人数目最少。

Solution

很真实,拿到就想二分图。

看题解的思路。

零食作为节点,客人的喜好作为边。

并查集维护关系。

如果新加入的两个零食都出现在同一个并查集里,那么代表当前客人没有零食可选,答案加一。

  1 #include <algorithm>
  2 #include <cctype>
  3 #include <cmath>
  4 #include <cstdio>
  5 #include <cstdlib>
  6 #include <cstring>
  7 #include <iostream>
  8 #include <map>
  9 #include <numeric>
 10 #include <queue>
 11 #include <set>
 12 #include <stack>
 13 #if __cplusplus >= 201103L
 14 #include <unordered_map>
 15 #include <unordered_set>
 16 #endif
 17 #include <vector>
 18 #define lson rt << 1, l, mid
 19 #define rson rt << 1 | 1, mid + 1, r
 20 #define LONG_LONG_MAX 9223372036854775807LL
 21 #define pblank putchar(' ')
 22 #define ll LL
 23 #define fastIO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
 24 using namespace std;
 25 typedef long long ll;
 26 typedef long double ld;
 27 typedef unsigned long long ull;
 28 typedef pair<int, int> P;
 29 int n, m, k;
 30 const int maxn = 1e5 + 10;
 31 template <class T>
 32 inline T read()
 33 {
 34     int f = 1;
 35     T ret = 0;
 36     char ch = getchar();
 37     while (!isdigit(ch))
 38     {
 39         if (ch == '-')
 40             f = -1;
 41         ch = getchar();
 42     }
 43     while (isdigit(ch))
 44     {
 45         ret = (ret << 1) + (ret << 3) + ch - '0';
 46         ch = getchar();
 47     }
 48     ret *= f;
 49     return ret;
 50 }
 51 template <class T>
 52 inline void write(T n)
 53 {
 54     if (n < 0)
 55     {
 56         putchar('-');
 57         n = -n;
 58     }
 59     if (n >= 10)
 60     {
 61         write(n / 10);
 62     }
 63     putchar(n % 10 + '0');
 64 }
 65 template <class T>
 66 inline void writeln(const T &n)
 67 {
 68     write(n);
 69     puts("");
 70 }
 71 template <typename T>
 72 void _write(const T &t)
 73 {
 74     write(t);
 75 }
 76 template <typename T, typename... Args>
 77 void _write(const T &t, Args... args)
 78 {
 79     write(t), pblank;
 80     _write(args...);
 81 }
 82 template <typename T, typename... Args>
 83 inline void write_line(const T &t, const Args &... data)
 84 {
 85     _write(t, data...);
 86 }
 87 int fa[maxn];
 88 int find(int x)
 89 {
 90     if (!fa[x])
 91         return x;
 92     return fa[x] = find(fa[x]);
 93 }
 94 void merge(int x, int y)
 95 {
 96     x = find(x), y = find(y);
 97     fa[y] = x;
 98 }
 99 int main(int argc, char const *argv[])
100 {
101 #ifndef ONLINE_JUDGE
102     freopen("in.txt", "r", stdin);
103     // freopen("out.txt", "w", stdout);
104 #endif
105     n = read<int>(), m = read<int>();
106     int res = 0;
107     for (int i = 0; i < m; i++)
108     {
109         int x = read<int>(), y = read<int>();
110         if (find(x) == find(y))
111             ++res;
112         else
113         {
114             merge(x, y);
115         }
116     }
117     writeln(res);
118     return 0;
119 }
View Code
原文地址:https://www.cnblogs.com/mooleetzi/p/11831284.html