A. Single Push
模拟瞎搞,开始忘了判断可能出现多种差值,憨憨
B. Silly Mistake
Description
Solution
也是一个模拟瞎搞题,不过自己比较憨,忘了判断最后一节儿,还以为写了个假题,自闭.
#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 pblank putchar(' ') #define ll LL #define fastIO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0) 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(""); } template <typename T> void _write(const T &t) { write(t); } template <typename T, typename... Args> void _write(const T &t, Args... args) { write(t), pblank; _write(args...); } template <typename T, typename... Args> inline void write_line(const T &t, const Args &... data) { _write(t, data...); } int a[maxn]; map<int, int> mp; set<int> s; vector<int> res; int main(int argc, char const *argv[]) { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); #endif n = read<int>(); for (int i = 0; i < n; i++) a[i] = read<int>(); if (n & 1) { puts("-1"); return 0; } int pre = -1, f = 1; for (int i = 0; i < n && f; i++) { if (a[i] > 0) { if (mp.count(a[i]) || s.count(a[i])) f = 0; else mp[a[i]] = 1, s.emplace(a[i]); } else { if (!mp.count(-a[i])) f = 0; else mp.erase(-a[i]); } if (mp.empty()) { res.emplace_back(i - pre); pre = i; s.clear(); } } if (f) { int tmp = accumulate(res.begin(), res.end(), 0); if (tmp == n) { writeln(res.size()); for (int i = 0; i < res.size(); i++) write(res[i]), pblank; puts(""); } else { puts("-1"); } } else puts("-1"); return 0; }
C. Sweets Eating
Description
给出一个长为n的序列,代表每颗糖的甜度.
每天最多可以吃m颗糖,第d天吃的糖的甜度是$a[i] imes d$
询问吃$i$颗糖的最小甜度
Solution
显然对于一个确定的询问i颗糖,我们应该选择其中甜度最小的i颗糖,然后甜度大的最先吃.
排序前缀和递推求解.
$a[i]+=a[i-m]$,这样累计下去便可以每次将不是第一天吃的糖果加上一天的权重.
#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 pblank putchar(' ') #define ll LL #define fastIO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0) 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 = 2e5 + 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(""); } template <typename T> void _write(const T &t) { write(t); } template <typename T, typename... Args> void _write(const T &t, Args... args) { write(t), pblank; _write(args...); } template <typename T, typename... Args> inline void write_line(const T &t, const Args &... data) { _write(t, data...); } ll a[maxn]; int main(int argc, char const *argv[]) { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); #endif n = read<int>(), m = read<int>(); for (int i = 1; i <= n; i++) a[i] = read<ll>(); sort(a + 1, a + 1 + n); for (int i = 1; i <= n; i++) a[i] += a[i - 1]; for (int i = m + 1; i <= n; i++) a[i] += a[i - m]; for (int i = 1; i <= n; i++) write(a[i]), pblank; puts(""); return 0; }
D. Harmonious Graph
Description
给出一个无向图,包含n个点,m条边.
如果图中所有的边$(l,r)$都满足$(l,l+1),(l,l+2),...(l,r)$则图是优秀的.
现在要求加入最少的边使图变得优秀.
Solution
对于一个连通块,只需要考虑最大的r对答案的贡献.
对每个未访问点i找连通块,每次找最大的r,在i到r间有没有访问的点就需要加上一条边,并继续从当前点扩大i的连通块,更新r值
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 puts(""); 87 } 88 vector<int> g[maxn]; 89 int vis[maxn]; 90 int maxx; 91 void dfs(int u) 92 { 93 vis[u] = 1; 94 int sz = g[u].size(); 95 for (int i = 0; i < sz; i++) 96 { 97 int v = g[u][i]; 98 if (!vis[v]) 99 dfs(v); 100 } 101 maxx = max(maxx, u); 102 } 103 int main(int argc, char const *argv[]) 104 { 105 #ifndef ONLINE_JUDGE 106 freopen("in.txt", "r", stdin); 107 // freopen("out.txt", "w", stdout); 108 #endif 109 n = read<int>(), m = read<int>(); 110 for (int i = 0; i < m; i++) 111 { 112 int x = read<int>(); 113 int y = read<int>(); 114 g[x].emplace_back(y); 115 g[y].emplace_back(x); 116 } 117 int res = 0; 118 for (int i = 1; i <= n; i++) 119 { 120 if (!vis[i]) 121 { 122 maxx = i; 123 dfs(i); 124 for (int j = i; j < maxx; j++) 125 if (!vis[j]) 126 dfs(j), ++res; 127 i = maxx; 128 } 129 } 130 writeln(res); 131 return 0; 132 }
最近很five.