codeforces #595 div3 题解
Posted mooleetzi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codeforces #595 div3 题解相关的知识,希望对你有一定的参考价值。
A. Yet Another Dividing into Teams
Description
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 <queue> 10 #include <set> 11 #include <stack> 12 #if __cplusplus >= 201103L 13 #include <unordered_map> 14 #include <unordered_set> 15 #endif 16 #include <vector> 17 #define lson rt << 1, l, mid 18 #define rson rt << 1 | 1, mid + 1, r 19 #define LONG_LONG_MAX 9223372036854775807LL 20 #define ll LL 21 using namespace std; 22 typedef long long ll; 23 typedef long double ld; 24 typedef unsigned long long ull; 25 typedef pair<int, int> P; 26 int n, m, k; 27 const int maxn = 1e5 + 10; 28 template <class T> 29 inline T read() 30 { 31 int f = 1; 32 T ret = 0; 33 char ch = getchar(); 34 while (!isdigit(ch)) 35 { 36 if (ch == ‘-‘) 37 f = -1; 38 ch = getchar(); 39 } 40 while (isdigit(ch)) 41 { 42 ret = (ret << 1) + (ret << 3) + ch - ‘0‘; 43 ch = getchar(); 44 } 45 ret *= f; 46 return ret; 47 } 48 template <class T> 49 inline void write(T n) 50 { 51 if (n < 0) 52 { 53 putchar(‘-‘); 54 n = -n; 55 } 56 if (n >= 10) 57 { 58 write(n / 10); 59 } 60 putchar(n % 10 + ‘0‘); 61 } 62 template <class T> 63 inline void writeln(const T &n) 64 { 65 write(n); 66 puts(""); 67 } 68 int a[maxn]; 69 int main(int argc, char const *argv[]) 70 { 71 #ifndef ONLINE_JUDGE 72 freopen("in.txt", "r", stdin); 73 freopen("out.txt", "w", stdout); 74 #endif 75 int t = read<int>(); 76 while (t--) 77 { 78 n = read<int>(); 79 for (int i = 0; i < n; i++) 80 a[i] = read<int>(); 81 sort(a, a + n); 82 int f = 0; 83 for (int i = 1; i < n; i++) 84 if (a[i] == a[i - 1] + 1) 85 ++f; 86 if (f) 87 puts("2"); 88 else 89 puts("1"); 90 } 91 return 0; 92 }
B. Books Exchange
Description
给出一个置换群,求每个循环的阶
Solution
dfs瞎搜一手
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 <queue> 10 #include <set> 11 #include <stack> 12 #if __cplusplus >= 201103L 13 #include <unordered_map> 14 #include <unordered_set> 15 #endif 16 #include <vector> 17 #define lson rt << 1, l, mid 18 #define rson rt << 1 | 1, mid + 1, r 19 #define LONG_LONG_MAX 9223372036854775807LL 20 #define ll LL 21 using namespace std; 22 typedef long long ll; 23 typedef long double ld; 24 typedef unsigned long long ull; 25 typedef pair<int, int> P; 26 int n, m, k; 27 const int maxn = 2e5 + 10; 28 template <class T> 29 inline T read() 30 { 31 int f = 1; 32 T ret = 0; 33 char ch = getchar(); 34 while (!isdigit(ch)) 35 { 36 if (ch == ‘-‘) 37 f = -1; 38 ch = getchar(); 39 } 40 while (isdigit(ch)) 41 { 42 ret = (ret << 1) + (ret << 3) + ch - ‘0‘; 43 ch = getchar(); 44 } 45 ret *= f; 46 return ret; 47 } 48 template <class T> 49 inline void write(T n) 50 { 51 if (n < 0) 52 { 53 putchar(‘-‘); 54 n = -n; 55 } 56 if (n >= 10) 57 { 58 write(n / 10); 59 } 60 putchar(n % 10 + ‘0‘); 61 } 62 template <class T> 63 inline void writeln(const T &n) 64 { 65 write(n); 66 puts(""); 67 } 68 int a[maxn], vis[maxn], step; 69 void dfs(int u, int f) 70 { 71 if (a[u] == f) 72 { 73 vis[u] = step; 74 return; 75 } 76 step++; 77 dfs(a[u], f); 78 vis[u] = step; 79 } 80 int main(int argc, char const *argv[]) 81 { 82 #ifndef ONLINE_JUDGE 83 freopen("in.txt", "r", stdin); 84 freopen("out.txt", "w", stdout); 85 #endif 86 int t = read<int>(); 87 while (t--) 88 { 89 n = read<int>(); 90 memset(vis, 0, sizeof(int) * (n + 1)); 91 for (int i = 1; i <= n; i++) 92 a[i] = read<int>(); 93 for (int i = 1; i <= n; i++) 94 { 95 if (!vis[i]) 96 { 97 step = 1; 98 dfs(i, i); 99 } 100 } 101 for (int i = 1; i <= n; i++) 102 printf("%d ", vis[i]); 103 puts(""); 104 } 105 return 0; 106 }
C. Good Numbers
Description
给一个数n,找出一个最小的m使得m>=n,且m是3的幂次之和,同一幂次最多出现依次
Solution
十进制分解为三进制,当某一位是2或3(上一位进位而来)时,当前位赋值0,下一位++,注意判断最后进位,以及最后更新位置之前赋0
三进制转回十进制即为答案
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 <queue> 10 #include <set> 11 #include <stack> 12 #if __cplusplus >= 201103L 13 #include <unordered_map> 14 #include <unordered_set> 15 #endif 16 #include <vector> 17 #define lson rt << 1, l, mid 18 #define rson rt << 1 | 1, mid + 1, r 19 #define LONG_LONG_MAX 9223372036854775807LL 20 #define ll LL 21 using namespace std; 22 typedef long long ll; 23 typedef long double ld; 24 typedef unsigned long long ull; 25 typedef pair<int, int> P; 26 ull n, m, k; 27 const int maxn = 1e5 + 10; 28 template <class T> 29 inline T read() 30 { 31 int f = 1; 32 T ret = 0; 33 char ch = getchar(); 34 while (!isdigit(ch)) 35 { 36 if (ch == ‘-‘) 37 f = -1; 38 ch = getchar(); 39 } 40 while (isdigit(ch)) 41 { 42 ret = (ret << 1) + (ret << 3) + ch - ‘0‘; 43 ch = getchar(); 44 } 45 ret *= f; 46 return ret; 47 } 48 template <class T> 49 inline void write(T n) 50 { 51 if (n < 0) 52 { 53 putchar(‘-‘); 54 n = -n; 55 } 56 if (n >= 10) 57 { 58 write(n / 10); 59 } 60 putchar(n % 10 + ‘0‘); 61 } 62 template <class T> 63 inline void writeln(const T &n) 64 { 65 write(n); 66 puts(""); 67 } 68 ull p[50]; 69 void init() 70 { 71 p[0] = 1; 72 for (int i = 1; i < 50; i++) 73 p[i] = p[i - 1] * 3; 74 } 75 vector<int> t3; 76 ull solve() 77 { 78 t3.clear(); 79 while (n) 80 { 81 t3.emplace_back(n % 3); 82 n /= 3; 83 } 84 int sz = t3.size(); 85 int f = -1; 86 for (int i = 0; i < sz; i++) 87 if (t3[i] == 2 || t3[i] == 3) 88 { 89 f = i; 90 if (i != sz - 1) 91 { 92 t3[i] = 0; 93 t3[i + 1]++; 94 } 95 else 96 { 97 t3[i] = 0; 98 t3.emplace_back(1); 99 } 100 } 101 sz = t3.size(); 102 ull res = 0; 103 if (f != -1) 104 for (int i = 0; i < f; i++) 105 t3[i] = 0; 106 for (int i = 0; i < sz; i++) 107 if (t3[i]) 108 res += p[i]; 109 return res; 110 } 111 int main(int argc, char const *argv[]) 112 { 113 #ifndef ONLINE_JUDGE 114 freopen("in.txt", "r", stdin); 115 // freopen("out.txt", "w", stdout); 116 #endif 117 init(); 118 int t = read<int>(); 119 while (t--) 120 { 121 n = read<ull>(); 122 writeln(solve()); 123 } 124 return 0; 125 }
D1. Too Many Segments
Description
给n个区间,一个上界k
问最少删除多少区间,使得区间内任一点的覆盖数不大于k
贪心策略,从左往右找每个需要删区间的点,对于区间选择采取满足l<=i且r最大
由于区间和n值较小,O(n^3)亦可过
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #if __cplusplus >= 201103L #include <unordered_map> #include <unordered_set> #endif #include <vector> #define lson rt << 1, l, mid #define rson rt << 1 | 1, mid + 1, r #define LONG_LONG_MAX 9223372036854775807LL #define ll LL using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef pair<int, int> P; int n, m, k; const int maxn = 1e5 + 10; template <class T> inline T read() { int f = 1; T ret = 0; char ch = getchar(); while (!isdigit(ch)) { if (ch == ‘-‘) f = -1; ch = getchar(); } while (isdigit(ch)) { ret = (ret << 1) + (ret << 3) + ch - ‘0‘; ch = getchar(); } ret *= f; return ret; } template <class T> inline void write(T n) { if (n < 0) { putchar(‘-‘); n = -n; } if (n >= 10) { write(n / 10); } putchar(n % 10 + ‘0‘); } template <class T> inline void writeln(const T &n) { write(n); puts(""); } struct node { int l, r, idx; node() {} node(int l, int r, int idx) { this->l = l, this->r = r, this->idx = idx; } bool operator<(const node &t1) const { if (r == t1.r) return l < t1.l; return r < t1.l; } }; vector<node> vec; int p[202], del[202]; int main(int argc, char const *argv[]) { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); #endif n = read<int>(), k = read<int>(); int maxx = 0; for (int i = 1; i <= n; i++) { int x = read<int>(), y = read<int>(); maxx = max(maxx, y); vec.emplace_back(x, y, i); for (int j = x; j <= y; j++) ++p[j]; } for (int i = 1; i <= maxx; i++) { while (p[i] > k) { int rr = 0, delid = -1; for (int j = 0; j < n; j++) if (!del[j] && vec[j].l <= i && vec[j].r > rr) { rr = vec[j].r; delid = j; } for (int j = vec[delid].l; j <= vec[delid].r; j++) --p[j]; del[delid] = 1; } } int res = accumulate(del, del + n, 0); writeln(res); for (int i = 0; i < n; i++) if (del[i]) write(i + 1), putchar(‘ ‘); return 0; }
D2. Too Many Segments
Description
挖坑
E. By Elevator or Stairs?
Description
Solution
简单dp
dp[i][0]表示走路到i层的最小花费,dp[i][1]表示电梯到i层的最小花费。
考虑状态转移
需要注意dp[2]是一开始就确定的,wa了一发
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 <queue> 10 #include <set> 11 #include <stack> 12 #if __cplusplus >= 201103L 13 #include <unordered_map> 14 #include <unordered_set> 15 #endif 16 #include <vector> 17 #define lson rt << 1, l, mid 18 #define rson rt << 1 | 1, mid + 1, r 19 #define LONG_LONG_MAX 9223372036854775807LL 20 #define ll LL 21 using namespace std; 22 typedef long long ll; 23 typedef long double ld; 24 typedef unsigned long long ull; 25 typedef pair<int, int> P; 26 int n, m, k; 27 const int maxn = 2e5 + 10; 28 template <class T> 29 inline T read() 30 { 31 int f = 1; 32 T ret = 0; 33 char ch = getchar(); 34 while (!isdigit(ch)) 35 { 36 if (ch == ‘-‘) 37 f = -1; 38 ch = getchar(); 39 } 40 while (isdigit(ch)) 41 { 42 ret = (ret << 1) + (ret << 3) + ch - ‘0‘; 43 ch = getchar(); 44 } 45 ret *= f; 46 return ret; 47 } 48 template <class T> 49 inline void write(T n) 50 { 51 if (n < 0) 52 { 53 putchar(‘-‘); 54 n = -n; 55 } 56 if (n >= 10) 57 { 58 write(n / 10); 59 } 60 putchar(n % 10 + ‘0‘); 61 } 62 template <class T> 63 inline void writeln(const T &n) 64 { 65 write(n); 66 puts(""); 67 } 68 ll dp[maxn][2]; 69 ll a[maxn], b[maxn], c; 70 int main(int argc, char const *argv[]) 71 { 72 #ifndef ONLINE_JUDGE 73 freopen("in.txt", "r", stdin); 74 freopen("out.txt", "w", stdout); 75 #endif 76 n = read<int>(), c = read<int>(); 77 for (int i = 1; i < n; i++) 78 a[i] = read<ll>(); 79 for (int i = 1; i < n; i++) 80 b[i] = read<ll>(); 81 dp[2][1] = c + b[1]; 82 dp[2][0] = a[1]; 83 for (int i = 3; i <= n; i++) 84 { 85 dp[i][0] = min(dp[i - 1][1], dp[i - 1][0]) + a[i - 1]; 86 dp[i][1] = min(dp[i - 1][0] + c, dp[i - 1][1]) + b[i - 1]; 87 } 88 for (int i = 1; i <= n; i++) 89 write(min(dp[i][0], dp[i][1])), putchar(‘ ‘); 90 return 0; 91 }
F. Maximum Weight Subset
Description
挖坑
以上是关于codeforces #595 div3 题解的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #617(div3) A-E2题解
Codeforces Round #617(div3) A-E2题解