比赛地址c++
题目连接
⭐数组
题目:
给出\(n\)和\(k\)询问,若\(k\)能整除\(n\)个数累加和,问这\(n\)个数中最大值最小化时值为多少app
解析:
由题目条件知\(n\ge 1\),若是\(n\le k\),则每一个元素至少为1,那么这样状况下平均分配能让最大值最小化,同理\(n>k\)时,则使得\(n\le dk\),且\(dk\%n<k\)也就是让这个\(dk\)越小越好编码
注:\(\lceil\frac{a}{b}\rceil=\lfloor\frac{a+b-1}{b}\rfloor\)spa
#include<bits/stdc++.h> using namespace std; /*===========================================*/ int main() { int T; scanf("%d", &T); while (T--) { int n, k; scanf("%d%d", &n, &k); k *= (n + k - 1) / k; printf("%d\n", (k + n - 1) / n); } }
题目连接
⭐code
题目:
给出\(p\)数组,公式\(k_i=\frac{p[i]}{\sum_{j=0}^{i-1}p[j]}\),要求\(k_i\)均小于等于指定数值\(k\%\),如今若是要知足这个要求,可能须要对数组\(p\)部分元素进行增长,则总增长量最小是多少递归
解析:
不难发现对于当前\(i\)来讲,增长本身或者增长\(j(j>0)\),是充满不肯定性的,由于这样的操做可能会使得\(k_i>k\%\),因此若是增长值所有汇聚在\(p_0\)是最好的(\(k_0\)与题目要求无关),那么只须要找到使得每一个元素符合要求的最小增长量便可字符串
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;++i) typedef long long LL; using namespace std; /*===========================================*/ int main() { int T; scanf("%d", &T); while (T--) { LL sum, t; LL ret = 0; int n, k; scanf("%d%d", &n, &k); scanf("%lld", &sum); rep(i, 1, n) { scanf("%lld", &t); if (100 * t > k * sum) { LL tmp = (100 * t + k - 1) / k; ret += tmp - sum; sum = tmp; } sum += t; } printf("%lld\n", ret); } }
题目连接
⭐⭐⭐get
题目:
给出\(n\)条链长度为\(c[i]\)和\(a,b\)数组,数组表示第\(i\)条链的“1”端链接至第\(i-1\)条链的\(a[i]\)处,“\(c[i]\)”端链接至第\(i-1\)条链的\(b[i]\)处,假设每一个链接的长度为单位长度,求这个图中的最大环it
解析:
注意:
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;++i) typedef long long LL; using namespace std; /*===========================================*/ const int maxn = 1e5 + 5; int a[maxn], b[maxn], c[maxn]; LL ret, now; int main() { int T; scanf("%d", &T); while (T--) { int n; scanf("%d", &n); rep(i, 0, n) scanf("%d", &c[i]); rep(i, 0, n) scanf("%d", &a[i]); rep(i, 0, n) scanf("%d", &b[i]); now = ret = 0; rep(i, 1, n) { if (i != 1) { if (a[i] == b[i]) now = 2; else now += 2LL + min(a[i], b[i]) - 1 + c[i - 1] - max(a[i], b[i]); } now = max(now, 2LL + abs(a[i] - b[i])); ret = max(ret, now + c[i] - 1); } printf("%lld\n", ret); } }
题目连接
⭐⭐
题目:
给出一个字符串,若是第\(i\)个字符为\(L\)则表明存在通路\(i-1\rightarrow i\),\(R\)表明存在通路\(i-1\leftarrow i\),且每使用一次道路,全部道路方向反向,那么求出起始点在第\(i\)点可以途径点数的最大值
解析:
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;++i) #define rep1(i,a,b) for(int i=a;i<=b;++i) #define rrep(i,a,b) for(int i=b-1;i>=a;--i) #define rrep1(i,a,b) for(int i=b;i>=a;--i) using namespace std; /*===========================================*/ const int maxn = 3e5 + 5; char s[maxn]; int l[maxn], r[maxn]; int main() { int T; scanf("%d", &T); while (T--) { int n; scanf("%d%s", &n, s + 1); l[0] = r[n] = 0; int cnt = 0; rep1(i, 1, n) { if (s[i] != s[i - 1]) ++cnt; else cnt = 1; if (s[i] == 'L') l[i] = cnt; else l[i] = 0; } cnt = 0; rrep1(i, 1, n) { if (s[i] != s[i + 1]) ++cnt; else cnt = 1; if (s[i] == 'R') r[i - 1] = cnt; else r[i - 1] = 0; } rep1(i, 0, n) printf("%d ", l[i] + r[i] + 1); printf("\n"); } }
题目连接
⭐⭐⭐
题目:
给出\(n\)个模式串和\(m\)个待匹配串,且每一个待匹配串有一个指望值\(mt\),询问是否能够将模式串从新排列,使得第一个遇到的成功匹配的模式串是第\(mt\)个串
字符串长度\(k(1\le k\le4)\)
解析:
注意: 对于给出\(mt\)模式串自己,没法与对应的字符串进行匹配的状况须要特殊处理
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;++i) #define rep1(i,a,b) for(int i=a;i<=b;++i) using namespace std; /*===========================================*/ const int maxn = 27 * 27 * 27 * 27; char s[5]; int vis[maxn]; int d[maxn]; vector<int> e[maxn]; int n, m, k, mt; int dat[100005]; int Pow[4] = { 1,27,27 * 27,27 * 27 * 27 }; void check(int x) { if (vis[x]) { ++d[vis[x]]; if (vis[x] != mt) e[mt].push_back(vis[x]); } } void fill(int now, int x) { if (x == k) check(now); else { fill(now, x + 1); fill(now + (26 - s[x] + 'a') * Pow[k - x - 1], x + 1); } } int main() { scanf("%d%d%d", &n, &m, &k); rep1(i, 1, n) { scanf("%s", s); int t = 0; rep(j, 0, k) { t *= 27; t += s[j] == '_' ? 26 : s[j] - 'a'; } vis[t] = i; } rep(i, 0, m) { scanf("%s%d", s, &mt); int t = 0; rep(j, 0, k) { t *= 27; t += s[j] - 'a'; } fill(t, 0); --d[mt]; } queue<int> q; rep1(i, 1, n) { if (!d[i]) q.push(i); } int cnt = 0; while (!q.empty()) { int t = q.front(); q.pop(); dat[++cnt] = t; for (auto& i : e[t]) { if (--d[i] == 0) q.push(i); } } if (cnt == n) { printf("YES\n"); rep1(i, 1, n) printf("%d ", dat[i]); } else printf("NO"); }