Codeforces Round #579 (Div. 3)

传送门

A. Circle of Students

这题我是直接把正序、逆序的两种放在数组里面直接判断。

Code
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int N = 205;
  5. int q, n;
  6. int a[N], b[N], c[N];
  7. int main() {
  8. ios::sync_with_stdio(false); cin.tie(0);
  9. cin >> q;
  10. while(q--) {
  11. cin >> n;
  12. for(int i = 1; i <= n; i++) cin >> a[i];
  13. int p;
  14. for(int i = 1; i <= n; i++) {
  15. if(a[i] == 1) {
  16. p = i; break;
  17. }
  18. }
  19. for(int i = p; i <= n; i++) {
  20. b[i - p + 1] = a[i];
  21. }
  22. for(int i = 1; i < p; i++) {
  23. b[i + n - p + 1] = a[i];
  24. }
  25. for(int i = p; i >= 1; i--) {
  26. c[p - i + 1] = a[i];
  27. }
  28. for(int i = n; i > p; i--) {
  29. c[n - i + p + 1] = a[i];
  30. }
  31. int ok = 1;
  32. for(int i = 1; i <= n; i++) {
  33. if(b[i] - b[i - 1] != 1) ok = 0;
  34. }
  35. if(ok) {
  36. cout << "YES" << '\n';
  37. continue;
  38. }
  39. ok = 1;
  40. for(int i = 1; i <= n; i++) {
  41. if(c[i] - c[i - 1] != 1) ok = 0;
  42. }
  43. if(ok) {
  44. cout << "YES" << '\n';
  45. continue;
  46. }
  47. cout << "NO" << '\n';
  48. }
  49. return 0;
  50. }

B. Equal Rectangles

排序后判断即可,满足两个条件:对应边长相等;面积乘积相等。

Code
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int N = 1005;
  5. int q, n;
  6. int a[N], b[N];
  7. int main() {
  8. ios::sync_with_stdio(false); cin.tie(0);
  9. cin >> q;
  10. while(q--) {
  11. cin >> n; n <<= 2;
  12. for(int i = 1; i <= n; i++) cin >> a[i];
  13. sort(a + 1, a + n + 1);
  14. int i = 1, j = n;
  15. int ok = 1;
  16. int ans = -1;
  17. while(i < j) {
  18. if(a[i] == a[i + 1] && a[j] == a[j - 1]) {
  19. if(ans == -1) {
  20. ans = a[i] * a[j];
  21. } else {
  22. if(ans != a[i] * a[j]) {
  23. ok = 0; break;
  24. }
  25. }
  26. } else {
  27. ok = 0; break;
  28. }
  29. i += 2; j -= 2;
  30. }
  31. if(ok) cout << "YES" << '\n';
  32. else cout << "NO" << '\n';
  33. }
  34. return 0;
  35. }

C. Common Divisors

水题。

Code
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int N = 4e5 + 5;
  5. int q, n;
  6. ll a[N];
  7. int main() {
  8. ios::sync_with_stdio(false); cin.tie(0);
  9. cin >> n;
  10. for(int i = 1; i <= n; i++) cin >> a[i];
  11. ll g = a[1];
  12. for(int i = 2; i <= n; i++) {
  13. g = __gcd(g, a[i]);
  14. }
  15. int ans = 0;
  16. for(int i = 1; 1ll * i * i <= g; i++) {
  17. if(g % i == 0) {
  18. ans ++;
  19. if(g / i != i) ans++;
  20. }
  21. }
  22. cout << ans;
  23. return 0;
  24. }

D2. Remove the Substring (hard version)

对于串\(t\)中的每个位置,求出其在串\(s\)中最早能出现的位置以及最晚能出现的位置。之后逐一枚举判断即可。

Code
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int N = 2e5 + 5;
  5. char s[N], t[N];
  6. int f[N][26], g[N][26];
  7. int nxt[26];
  8. int h[N][2];
  9. int main() {
  10. ios::sync_with_stdio(false); cin.tie(0);
  11. cin >> s + 1 >> t + 1;
  12. int n = strlen(s + 1), m = strlen(t + 1);
  13. for(int i = n; i >= 0; i--) {
  14. for(int j = 0; j < 26; j++) f[i][j] = nxt[j];
  15. nxt[s[i] - 'a'] = i;
  16. }
  17. for(int i = 0; i < 26; i++) nxt[i] = 0;
  18. for(int i = 1; i <= n + 1; i++) {
  19. for(int j = 0; j < 26; j++) g[i][j] = nxt[j];
  20. nxt[s[i] - 'a'] = i;
  21. }
  22. int now = 0;
  23. for(int i = 1; i <= m; i++) {
  24. h[i][0] = f[now][t[i] - 'a'];
  25. now = f[now][t[i] - 'a'];
  26. }
  27. now = n + 1;
  28. for(int i = m; i >= 1; i--) {
  29. h[i][1] = g[now][t[i] - 'a'];
  30. now = g[now][t[i] - 'a'];
  31. }
  32. int res = max(h[1][1] - 1, n - h[m][0]);
  33. // cout << h[1][1] << '\n';
  34. for(int i = 1; i < m; i++) {
  35. res = max(res, h[i + 1][1] - h[i][0] - 1);
  36. }
  37. cout << res;
  38. return 0;
  39. }

E. Boxers

用个桶来进行标记就行,对于一个数,优先标记其前面的那一个数,其次标记本身,最后标记后面。

因为我们是从小到大枚举,可以证明这样最优。

Code
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int N = 150005;
  5. int q, n;
  6. int a[N], c[N];
  7. bool vis[N];
  8. int main() {
  9. ios::sync_with_stdio(false); cin.tie(0);
  10. cin >> n;
  11. for(int i = 1; i <= n; i++) cin >> a[i];
  12. for(int i = 1; i <= n; i++) c[a[i]]++;
  13. if(c[1] > 1) {
  14. vis[1] = vis[2] = 1;
  15. } else if(c[1] == 1) vis[1] = 1;
  16. for(int i = 2; i < N; i++) {
  17. if(c[i]) {
  18. if(!vis[i - 1]) vis[i - 1] = 1;
  19. else if(!vis[i]) vis[i] = 1;
  20. else vis[i + 1] = 1;
  21. if(c[i] == 2) {
  22. if(!vis[i - 1] && i > 1) vis[i - 1] = 1;
  23. else if(!vis[i]) vis[i] = 1;
  24. else vis[i + 1] = 1;
  25. }
  26. if(c[i] >= 3) {
  27. vis[i] = 1;
  28. vis[i - 1] = 1;
  29. vis[i + 1] = 1;
  30. }
  31. }
  32. }
  33. int ans = 0;
  34. for(int i = 1; i < N; i++) ans += vis[i];
  35. cout << ans;
  36. return 0;
  37. }

F1. Complete the Projects (easy version)

首先考虑把所有\(y>0\)的给搞完,然后对于\(y<0\)的,按照\(x+y\)从大到小排序,再逐一判断就行。

这里从大到小排序可以保证每次能够选得尽量地多,就相当于处理一个\(r->a_i+r\)的逆问题。

Code
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int N = 60005;
  5. int q, n, m, r;
  6. struct node{
  7. int r, v;
  8. }a[N], b[N];
  9. bool vis[N];
  10. int main() {
  11. ios::sync_with_stdio(false); cin.tie(0);
  12. cin >> n >> r;
  13. for(int i = 1; i <= n; i++) {
  14. cin >> a[i].r >> a[i].v;
  15. }
  16. while(1) {
  17. int mx = -N, id;
  18. for(int i = 1; i <= n; i++) {
  19. if(!vis[i] && a[i].r <= r && a[i].v > mx) {
  20. mx = a[i].v; id = i;
  21. }
  22. }
  23. if(mx < 0) break;
  24. vis[id] = 1;
  25. r += mx;
  26. }
  27. int tot = 0;
  28. for(int i = 1; i <= n; i++) {
  29. if(!vis[i]) b[++tot] = a[i];
  30. }
  31. sort(b + 1, b + tot + 1, [&](node a, node b){return a.r + a.v > b.r + b.v;});
  32. int ok = 1;
  33. for(int i = 1; i <= tot; i++) {
  34. if(r >= b[i].r && r + b[i].v >= 0) {
  35. r += b[i].v;
  36. } else ok = 0;
  37. }
  38. if(ok) cout << "YES" << '\n';
  39. else cout << "NO" << '\n';
  40. return 0;
  41. }

F2. Complete the Projects (hard version)

类似于之前的思路,首先贪心地把\(y>0\)的部分尽量取完,然后对于\(y<0\)的\(dp\)来处理。

设\(dp[i][j]\)表示当前\(i\)个物品中选了若干个,\(r\)为\(j\)时取的最大数量,转移时类似于背包。之后可以滚动数组优化一维。

注意一下我们还是要按照\(x+y\)来排序,否则有些状态不能转移到。

Code
  1. #include <bits/stdc++.h>
  2. #define MP make_pair
  3. #define fi first
  4. #define se second
  5. #define INF 0x3f3f3f3f
  6. using namespace std;
  7. typedef long long ll;
  8. typedef pair<int, int> pii;
  9. const int N = 105, MAX = 60005;
  10. int n, r;
  11. pii a[N], b[N];
  12. bool vis[N];
  13. int dp[MAX];
  14. int main() {
  15. ios::sync_with_stdio(false); cin.tie(0);
  16. cin >> n >> r;
  17. for(int i = 1; i <= n; i++) cin >> a[i].fi >> a[i].se;
  18. sort(a + 1, a + n + 1);
  19. int ans = 0;
  20. while(1) {
  21. int mx = -MAX, id = -1;
  22. for(int i = 1; i <= n; i++) {
  23. if(!vis[i] && a[i].fi <= r && a[i].se > mx) {
  24. mx = a[i].se; id = i;
  25. }
  26. }
  27. if(mx < 0) break;
  28. vis[id] = 1;
  29. r += mx; ans++;
  30. }
  31. int tot = 0;
  32. for(int i = 1; i <= n; i++) {
  33. if(a[i].fi <= r && a[i].se < 0) b[++tot] = a[i];
  34. }
  35. sort(b + 1, b + tot + 1, [&](pii a, pii b){
  36. return a.fi + a.se == b.fi + b.se ? a.fi > a.se : a.fi + a.se > b.fi + b.se;
  37. });
  38. // for(int i = 1; i <= tot; i++) cout << b[i].fi << ' ' << b[i].se << '\n';
  39. for(int i = 1; i <= tot; i++) {
  40. for(int j = b[i].fi; j <= r; j++) {
  41. if(j + b[i].se >= 0) dp[j + b[i].se] = max(dp[j + b[i].se], dp[j] + 1);
  42. }
  43. }
  44. int res = 0;
  45. for(int i = 0; i <= r; i++) res = max(res, dp[i]);
  46. ans += res;
  47. cout << ans;
  48. return 0;
  49. }

Codeforces Round #579 (Div. 3)的更多相关文章

  1. 双指针(最大删除子串)Codeforces Round #579 (Div. 3)--Remove the Substring (hard version)

    题目链接:https://codeforces.com/contest/1203/problem/D2 题意: 给你S串.T串,问你最长删除多长的子串使得S串里仍然有T的子序列. 思路: 想了好久,先 ...

  2. Codeforces Round #579 (Div. 3) 套题 题解

    A. Circle of Students      题目:https://codeforces.com/contest/1203/problem/A 题意:一堆人坐成一个环,问能否按逆时针或者顺时针 ...

  3. Codeforces Round #579 (Div. 3) Complete the Projects(贪心、DP)

    http://codeforces.com/contest/1203/problem/F1 Examples input 1 - - output 1 YES input 2 - - output 2 ...

  4. 【cf比赛练习记录】Codeforces Round #579 (Div. 3)

    思考之后再看题解,是与别人灵魂之间的沟通与碰撞 A. Circle of Students 题意 给出n个数,问它们向左或者向右是否都能成一个环.比如样例5是从1开始向左绕了一圈 [3, 2, 1, ...

  5. Codeforces Round #579 (Div. 3) 题解

    比赛链接:https://codeforc.es/contest/1203/ A. Circle of Students 题意:\(T\)组询问,每组询问给出\(n\)个数字,问这\(n\)个数字能否 ...

  6. Codeforces Round #579 (Div. 3)D(字符串,思维)

    #include<bits/stdc++.h>using namespace std;char s[200007],t[200007];int last[200007][27],nxt[2 ...

  7. Codeforces Round #579 (Div. 3) B Equal Rectangles、C. Common Divisors

    B Equal Rectangles 题意: 给你4*n个数,让你判断能不能用这个4*n个数为边凑成n个矩形,使的每个矩形面积相等 题解: 原本是想着用二分来找出来那个最终的面积,但是仔细想一想,那个 ...

  8. Codeforces Round #579 (Div. 3) D2. Remove the Substring (hard version) (思维,贪心)

    题意:给你一个模式串\(t\),现在要在主串\(s\)中删除多个子串,使得得到的\(s\)的子序列依然包含\(t\),问能删除的最长子串长度. 题解:首先,我们不难想到,我们可以选择\(s\)头部到最 ...

  9. Codeforces Round #579 (Div. 3) E. Boxers (贪心)

    题意:给你一组数,每个数都可以进行一次加一减一,问最后最多能有多少不同的数. 题解:我们可以用桶存每个数的次数,然后枚举\([1,150001]\)来求对答案的贡献,然后贪心,这里我们不用担心其他乱七 ...

随机推荐

  1. beeline无密码连接hiveserver2

    1.说明 #hiveserver2增加了权限控制,需要在hadoop的配置文件中配置 core-site.xml 增加以下内容: <property> <name>hadoop ...

  2. 【More Effective C++ 条款3】最好不要以多态方式处理数组

    1.在数组与多态混用的情况下,数组元素的访问会出现不可预期的结果(因为数组元素的访问会使用到下标运算) 将一个子类对象数组传递给一个父类对象数组声明的函数,编译器会允许这个行为,但是由于子类对象和父类 ...

  3. redis启动出现错误 can't chdir ...

    启动redis出现以下错误:[15816] *********** # Can't chdir to ’**********‘ :No such file or directory 解决方法:手动创建 ...

  4. mongodb数据库环境配置

    数据是每一前端人员必定接触的一样,所有的数据都是后端来编写,如果自己想练习项目,却没有数据,而是写一些假数据,去编写,或者通过json-server搭建一个数据,今天我们就通过MongoDB来搭建一个 ...

  5. 【mysql】windows7 安装mysql5.7 解压缩版 + windows7 安装mysql5.7报错 计算机丢失了MSVCR120.dll解决方法

    1.下载mysql 5.7的zip版解压缩的安装包 在mysql官网:http://dev.mysql.com/downloads/mysql/ 2.解压到本地任意目录,并创建一个mysql_data ...

  6. js计算两经纬度之间的距离

    js如下: // 方法定义 lat,lng function GetDistance( lat1, lng1, lat2, lng2){    var radLat1 = lat1*Math.PI / ...

  7. edrawmax使用技巧备忘

    由于自己经常需要画图示意,在对比研究了市面上的画图软件后,最终选择了亿图图示,一来是该软件貌似简单强大,有好多现成可用的功能,二来支持国产,并且亿图团队感觉还是不错的一直在用心打磨产品,当然要支持的! ...

  8. CentOS 7.0 使用yum 安装 Mariadb

    第一步: 使用命令查看是否已经安装: mysql -u root -p 返回 Enter password:  时表示已经安装成功的,需要卸载安装. 第二步: 使用yum直接安装mariadb,注意带 ...

  9. SQL Server强制释放内存

    --强制释放内存 CREATE procedure [dbo].ClearMemory as begin --清除所有缓存 DBCC DROPCLEANBUFFERS --打开高级配置 EXEC (' ...

  10. 简洁的 systemd 操作指南Linux下Service文件服务说明(转)

    1.服务权限systemd有系统和用户区分:系统(/user/lib/systemd/system/).用户(/etc/lib/systemd/user/). 一般系统管理员手工创建的单元文件建议存放 ...