Codeforces Round #579 (Div. 3)
Codeforces Round #579 (Div. 3)
A. Circle of Students
这题我是直接把正序、逆序的两种放在数组里面直接判断。
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 205;
int q, n;
int a[N], b[N], c[N];
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> q;
while(q--) {
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
int p;
for(int i = 1; i <= n; i++) {
if(a[i] == 1) {
p = i; break;
}
}
for(int i = p; i <= n; i++) {
b[i - p + 1] = a[i];
}
for(int i = 1; i < p; i++) {
b[i + n - p + 1] = a[i];
}
for(int i = p; i >= 1; i--) {
c[p - i + 1] = a[i];
}
for(int i = n; i > p; i--) {
c[n - i + p + 1] = a[i];
}
int ok = 1;
for(int i = 1; i <= n; i++) {
if(b[i] - b[i - 1] != 1) ok = 0;
}
if(ok) {
cout << "YES" << '\n';
continue;
}
ok = 1;
for(int i = 1; i <= n; i++) {
if(c[i] - c[i - 1] != 1) ok = 0;
}
if(ok) {
cout << "YES" << '\n';
continue;
}
cout << "NO" << '\n';
}
return 0;
}
B. Equal Rectangles
排序后判断即可,满足两个条件:对应边长相等;面积乘积相等。
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1005;
int q, n;
int a[N], b[N];
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> q;
while(q--) {
cin >> n; n <<= 2;
for(int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + n + 1);
int i = 1, j = n;
int ok = 1;
int ans = -1;
while(i < j) {
if(a[i] == a[i + 1] && a[j] == a[j - 1]) {
if(ans == -1) {
ans = a[i] * a[j];
} else {
if(ans != a[i] * a[j]) {
ok = 0; break;
}
}
} else {
ok = 0; break;
}
i += 2; j -= 2;
}
if(ok) cout << "YES" << '\n';
else cout << "NO" << '\n';
}
return 0;
}
C. Common Divisors
水题。
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 4e5 + 5;
int q, n;
ll a[N];
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
ll g = a[1];
for(int i = 2; i <= n; i++) {
g = __gcd(g, a[i]);
}
int ans = 0;
for(int i = 1; 1ll * i * i <= g; i++) {
if(g % i == 0) {
ans ++;
if(g / i != i) ans++;
}
}
cout << ans;
return 0;
}
D2. Remove the Substring (hard version)
对于串\(t\)中的每个位置,求出其在串\(s\)中最早能出现的位置以及最晚能出现的位置。之后逐一枚举判断即可。
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
char s[N], t[N];
int f[N][26], g[N][26];
int nxt[26];
int h[N][2];
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> s + 1 >> t + 1;
int n = strlen(s + 1), m = strlen(t + 1);
for(int i = n; i >= 0; i--) {
for(int j = 0; j < 26; j++) f[i][j] = nxt[j];
nxt[s[i] - 'a'] = i;
}
for(int i = 0; i < 26; i++) nxt[i] = 0;
for(int i = 1; i <= n + 1; i++) {
for(int j = 0; j < 26; j++) g[i][j] = nxt[j];
nxt[s[i] - 'a'] = i;
}
int now = 0;
for(int i = 1; i <= m; i++) {
h[i][0] = f[now][t[i] - 'a'];
now = f[now][t[i] - 'a'];
}
now = n + 1;
for(int i = m; i >= 1; i--) {
h[i][1] = g[now][t[i] - 'a'];
now = g[now][t[i] - 'a'];
}
int res = max(h[1][1] - 1, n - h[m][0]);
// cout << h[1][1] << '\n';
for(int i = 1; i < m; i++) {
res = max(res, h[i + 1][1] - h[i][0] - 1);
}
cout << res;
return 0;
}
E. Boxers
用个桶来进行标记就行,对于一个数,优先标记其前面的那一个数,其次标记本身,最后标记后面。
因为我们是从小到大枚举,可以证明这样最优。
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 150005;
int q, n;
int a[N], c[N];
bool vis[N];
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= n; i++) c[a[i]]++;
if(c[1] > 1) {
vis[1] = vis[2] = 1;
} else if(c[1] == 1) vis[1] = 1;
for(int i = 2; i < N; i++) {
if(c[i]) {
if(!vis[i - 1]) vis[i - 1] = 1;
else if(!vis[i]) vis[i] = 1;
else vis[i + 1] = 1;
if(c[i] == 2) {
if(!vis[i - 1] && i > 1) vis[i - 1] = 1;
else if(!vis[i]) vis[i] = 1;
else vis[i + 1] = 1;
}
if(c[i] >= 3) {
vis[i] = 1;
vis[i - 1] = 1;
vis[i + 1] = 1;
}
}
}
int ans = 0;
for(int i = 1; i < N; i++) ans += vis[i];
cout << ans;
return 0;
}
F1. Complete the Projects (easy version)
首先考虑把所有\(y>0\)的给搞完,然后对于\(y<0\)的,按照\(x+y\)从大到小排序,再逐一判断就行。
这里从大到小排序可以保证每次能够选得尽量地多,就相当于处理一个\(r->a_i+r\)的逆问题。
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 60005;
int q, n, m, r;
struct node{
int r, v;
}a[N], b[N];
bool vis[N];
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> n >> r;
for(int i = 1; i <= n; i++) {
cin >> a[i].r >> a[i].v;
}
while(1) {
int mx = -N, id;
for(int i = 1; i <= n; i++) {
if(!vis[i] && a[i].r <= r && a[i].v > mx) {
mx = a[i].v; id = i;
}
}
if(mx < 0) break;
vis[id] = 1;
r += mx;
}
int tot = 0;
for(int i = 1; i <= n; i++) {
if(!vis[i]) b[++tot] = a[i];
}
sort(b + 1, b + tot + 1, [&](node a, node b){return a.r + a.v > b.r + b.v;});
int ok = 1;
for(int i = 1; i <= tot; i++) {
if(r >= b[i].r && r + b[i].v >= 0) {
r += b[i].v;
} else ok = 0;
}
if(ok) cout << "YES" << '\n';
else cout << "NO" << '\n';
return 0;
}
F2. Complete the Projects (hard version)
类似于之前的思路,首先贪心地把\(y>0\)的部分尽量取完,然后对于\(y<0\)的\(dp\)来处理。
设\(dp[i][j]\)表示当前\(i\)个物品中选了若干个,\(r\)为\(j\)时取的最大数量,转移时类似于背包。之后可以滚动数组优化一维。
注意一下我们还是要按照\(x+y\)来排序,否则有些状态不能转移到。
Code
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 105, MAX = 60005;
int n, r;
pii a[N], b[N];
bool vis[N];
int dp[MAX];
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> n >> r;
for(int i = 1; i <= n; i++) cin >> a[i].fi >> a[i].se;
sort(a + 1, a + n + 1);
int ans = 0;
while(1) {
int mx = -MAX, id = -1;
for(int i = 1; i <= n; i++) {
if(!vis[i] && a[i].fi <= r && a[i].se > mx) {
mx = a[i].se; id = i;
}
}
if(mx < 0) break;
vis[id] = 1;
r += mx; ans++;
}
int tot = 0;
for(int i = 1; i <= n; i++) {
if(a[i].fi <= r && a[i].se < 0) b[++tot] = a[i];
}
sort(b + 1, b + tot + 1, [&](pii a, pii b){
return a.fi + a.se == b.fi + b.se ? a.fi > a.se : a.fi + a.se > b.fi + b.se;
});
// for(int i = 1; i <= tot; i++) cout << b[i].fi << ' ' << b[i].se << '\n';
for(int i = 1; i <= tot; i++) {
for(int j = b[i].fi; j <= r; j++) {
if(j + b[i].se >= 0) dp[j + b[i].se] = max(dp[j + b[i].se], dp[j] + 1);
}
}
int res = 0;
for(int i = 0; i <= r; i++) res = max(res, dp[i]);
ans += res;
cout << ans;
return 0;
}
Codeforces Round #579 (Div. 3)的更多相关文章
- 双指针(最大删除子串)Codeforces Round #579 (Div. 3)--Remove the Substring (hard version)
题目链接:https://codeforces.com/contest/1203/problem/D2 题意: 给你S串.T串,问你最长删除多长的子串使得S串里仍然有T的子序列. 思路: 想了好久,先 ...
- Codeforces Round #579 (Div. 3) 套题 题解
A. Circle of Students 题目:https://codeforces.com/contest/1203/problem/A 题意:一堆人坐成一个环,问能否按逆时针或者顺时针 ...
- 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 ...
- 【cf比赛练习记录】Codeforces Round #579 (Div. 3)
思考之后再看题解,是与别人灵魂之间的沟通与碰撞 A. Circle of Students 题意 给出n个数,问它们向左或者向右是否都能成一个环.比如样例5是从1开始向左绕了一圈 [3, 2, 1, ...
- Codeforces Round #579 (Div. 3) 题解
比赛链接:https://codeforc.es/contest/1203/ A. Circle of Students 题意:\(T\)组询问,每组询问给出\(n\)个数字,问这\(n\)个数字能否 ...
- Codeforces Round #579 (Div. 3)D(字符串,思维)
#include<bits/stdc++.h>using namespace std;char s[200007],t[200007];int last[200007][27],nxt[2 ...
- Codeforces Round #579 (Div. 3) B Equal Rectangles、C. Common Divisors
B Equal Rectangles 题意: 给你4*n个数,让你判断能不能用这个4*n个数为边凑成n个矩形,使的每个矩形面积相等 题解: 原本是想着用二分来找出来那个最终的面积,但是仔细想一想,那个 ...
- Codeforces Round #579 (Div. 3) D2. Remove the Substring (hard version) (思维,贪心)
题意:给你一个模式串\(t\),现在要在主串\(s\)中删除多个子串,使得得到的\(s\)的子序列依然包含\(t\),问能删除的最长子串长度. 题解:首先,我们不难想到,我们可以选择\(s\)头部到最 ...
- Codeforces Round #579 (Div. 3) E. Boxers (贪心)
题意:给你一组数,每个数都可以进行一次加一减一,问最后最多能有多少不同的数. 题解:我们可以用桶存每个数的次数,然后枚举\([1,150001]\)来求对答案的贡献,然后贪心,这里我们不用担心其他乱七 ...
随机推荐
- Linux基于FPM制作RPM包(以Nginx为例)
1.搭建Epel Yum源 安装在线yum源 [root@localhost ~]# rpm -ivh epel-release-latest-.noarch.rpm //安装扩展源 [root@ ...
- 洛谷P3232[HNOI2013]游走
有一个无向简单连通图,顶点从 \(1\) 编号到 \(n\),边从 \(1\) 编号到 \(m\) 小Z在该图上进行随机游走,初始时小Z在\(1\)号顶点,每一步小Z以相等的概率随机选 择当前顶点的某 ...
- [LeetCode] 230. Kth Smallest Element in a BST 二叉搜索树中的第K小的元素
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- [LeetCode] 38. Count and Say 计数和读法
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- Spring Boot中整合Sharding-JDBC读写分离示例
在我<Spring Cloud微服务-全栈技术与案例解析>书中,第18章节分库分表解决方案里有对Sharding-JDBC的使用进行详细的讲解. 之前是通过XML方式来配置数据源,读写分离 ...
- 解决VMware虚拟机中centos 7无法上网的问题
在WMware中安装centos 7后发现无法安装软件,开始以为是镜像服务器的问题,后来通过ping之后发现根本没办法连接到网络.由于很多设置都是默认的,并且虚拟机也是NAT模式,和电脑主机共享网络, ...
- [最新].NET Core ORM 开源项目一览,持续更新
截至2019-05-08共收集27个 .NET Core ORM 开源项目,38个 .NET ORM 开源项目. .NET Core ORM 开源项目收集地址:https://github.com/o ...
- zipfile
zipfile是一个用于处理zip压缩格式的文件的模块, 主要会用到它的ZipFile类 import zipfile zipfile.is_zipfile('myzip.zip')) # 判断一个文 ...
- W5500嵌入式开发
W5500是韩国一款集成全硬件 TCP/IP 协议栈的嵌入式以太网控制器,W5500同时也是一颗工业级以太网控制芯片,最近发现我们国内也有和W5500 芯片一样芯片 介绍给大家 如下图:
- 我的周记9——"所以快乐才是真谛"
七月又名:巧月.瓜月.霜月.相月.凉月.初商.肇秋.初秋.首秋.早秋.新秋.上秋.孟秋.夷则.申月等. 你好七月 ,想起大学的时候我在校园的点歌台点了一首歌,歌名是July . 七月一号,新的一月,新 ...