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 << "\n";
124         }
125         else
126             cout << "-\n";
127     }
128     return 0;
129 }

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 }

原文地址:https://www.cnblogs.com/mooleetzi/p/11831284.html

时间: 2024-11-08 11:23:02

codeforces #584 ABCD的相关文章

codeforces #270 ABCD

Codeforces Round #270 A - Design Tutorial: Learn from Math 题意:给出n,求出两个合数x和y使x+y=n. 题解:暴力筛合数,然后暴力找 1 //#pragma comment(linker, "/STACK:102400000,102400000") 2 #include<cstdio> 3 #include<cmath> 4 #include<iostream> 5 #include<

codeforces 584 E:映射

第一步要做的是映射,把目标序列映射成1~n,然后按照这个映射规则把当前序列映射一下 接下来也稍微有点贪心的意思吧:从1开始放置,位置在它之前的元素,如果找到的元素不在该在的位置,并且和当前元素互换后,找到的元素的目标位置在当前元素的左边,才换 似乎有点抽象...就拿样例来说吧 映射后当前序列是3 2 4 1 目标序列是1 2 3 4 从1开始找,找它之前的,不在该在的位置的元素 第一个找到3,在1号位置,但是我们不换 因为现在1在4号位置,换了的话3变到4号位置,3将来还是要往左换才能到达的,这

codeforces 493 ABCD

A. Vasya and Football A. Vasya and Football time limit per test:2 seconds memory limit per test:256 megabytes input:standard input output:standard output Vasya has started watching football games. He has learned that for some fouls the players receiv

Codeforces Round #258 (Div. 2)[ABCD]

Codeforces Round #258 (Div. 2)[ABCD] ACM 题目地址:Codeforces Round #258 (Div. 2) A - Game With Sticks 题意: Akshat and Malvika两人玩一个游戏,横竖n,m根木棒排成#型,每次取走一个交点,交点相关的横竖两条木棒要去掉,Akshat先手,给出n,m问谁赢. 分析: 水题,很明显不管拿掉哪个点剩下的都是(n-1,m-1),最后状态是(0,x)或(x,0),也就是拿了min(n,m)-1次,

Codeforces Round #354 (Div. 2) ABCD

Codeforces Round #354 (Div. 2) Problems # Name     A Nicholas and Permutation standard input/output 1 s, 256 MB    x3384 B Pyramid of Glasses standard input/output 1 s, 256 MB    x1462 C Vasya and String standard input/output 1 s, 256 MB    x1393 D T

Codeforces Round #250 (Div. 2) (ABCD题解)

比赛链接:http://codeforces.com/contest/433 A. Kitahara Haruki's Gift time limit per test:1 second memory limit per test:256 megabytes Kitahara Haruki has bought n apples for Touma Kazusa and Ogiso Setsuna. Now he wants to divide all the apples between th

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #143 (Div. 2) (ABCD 思维场)

题目连链接:http://codeforces.com/contest/231 A. Team time limit per test:2 seconds memory limit per test:256 megabytes One day three best friends Petya, Vasya and Tonya decided to form a team and take part in programming contests. Participants are usually

Codeforces Round #Pi (Div. 2) (ABCD题解)

比赛链接:http://codeforces.com/contest/567 听说Round #Pi的意思是Round #314... A. Lineland Mail time limit per test:3 seconds memory limit per test:256 megabytes All cities of Lineland are located on the Ox coordinate axis. Thus, each city is associated with it