【cf比赛练习记录】Codeforces Round #579 (Div. 3)
思考之后再看题解,是与别人灵魂之间的沟通与碰撞
题意 给出n个数,问它们向左或者向右是否都能成一个环。比如样例5是从1开始向左绕了一圈 [3, 2, 1, 5, 4] 变成 [1, 2, 3, 4, 5];
思路 我的方法是差分,假如成立,相邻两个数的差的绝对值要么是1要么是n-1。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#include<string>
#include<stack>
using namespace std;
int q, n;
int num[202];
int sum[202];
int main()
{
cin >> q;
while(q--){
cin >> n;
memset(sum, 0, sizeof(sum));
for(int i = 0; i < n; i++) cin >> num[i];
bool can = true;
for(int i = 0; i < n; i++){
if(i==0){
sum[i] = num[i] - num[n-1];
}
else {
sum[i] = num[i] - num[i-1];
}
if(abs(sum[i]) != 1 && abs(sum[i]) != n-1) can = false;
}
if(can) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}
题意 给出n个矩形,问所给出的4n条木棒所组成的矩形的面积是否都相等。
分析 要使面积都相等,则要取中间值。先排个序,然后小的与大的在一起,从两头往中间靠拢。简单的实现。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#include<string>
#include<stack>
using namespace std;
int q, n;
int num[402];
int cnt[10004];
int main()
{
cin >> q;
while(q--){
cin >> n;
bool can = true;
memset(cnt, 0, sizeof(cnt));
for(int i = 0; i < 4*n; i++) {
cin >> num[i];
cnt[num[i]]++;
}
sort(num, num + 4*n);
for(int i = 1; i <= 10000; i++){
if(cnt[i]&1 == 1){ // 不清楚是否会有奇数边,所以遍历了一遍
can = false;
break;
}
}
if(can){
int sq = num[0] * num[4*n - 1]; // 两头
int l = 2, r = 4*n - 3;
for( ; l <= r; ){
if(num[l] * num[r] != sq){
can = false;
break;
}
l += 2; r -= 2;
}
}
if(can) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}
题意 求n个数的公约数的个数。
分析 先求最大公约数(gcd),然后求这个最大公约数的约数(花样找质数),注意long long。
小插曲 因为我审题不认真,导致看漏了 Output 里的 "the number of...",结果卡了很久quq。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#include<string>
#include<stack>
using namespace std;
typedef long long LL;
LL gcd(LL a, LL b){
if(b == 0) return a;
else return gcd(b, a%b);
}
int n;
LL num[400005];
int main()
{
scanf("%d", &n);
for(int i = 0; i < n; i++) scanf("%I64d", &num[i]);
LL ans = gcd(num[0], num[1]);
for(int i = 2; i < n; i++){
ans = gcd(ans, num[i]);
}
LL cnt = 0;
for(LL i = 1; i*i <= ans; i++){
if(i*i == ans) cnt += 1;
else if(ans%i == 0) cnt += 2;
}
printf("%I64d\n", cnt);
return 0;
}
补题 D1. Remove the Substring (easy version)
题意 t是s的子串,问最长可以删去s的连续子串后,仍保持t是s的子串。
分析 注意,“the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". ”。然后因为数据小,可以暴力枚举被删子串的起点与终点。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#include<string>
#include<stack>
using namespace std;
string s, t;
int ans;
int main()
{
cin >> s >> t;
for(int i = 0; i < s.size(); i++){ // 被删子串起点
for(int j = i; j < s.size(); j++){ // 被删子串终点
int tt = 0;
// 判断删后的子串是否仍满足条件------------
for(int k = 0; k < s.size(); k++){
if(k == i){ // 遇到被删的起点立即跳到被删的终点
k = j;
continue;
}
if(s[k] == t[tt]){
tt++;
if(tt == t.size()){
break;
}
}
}
// -----------------------------------------
if(tt == t.size()) ans = max(ans, j-i+1);
}
}
cout << ans << endl;
return 0;
}
补题 D2. Remove the Substring (hard version)
题意 与D1一样,只是数据更大,不能暴力。
分析 字符串的配对。经过D1的练手发现,可删去的最长连续子序列分别在t子序列的前、中、尾三个部分。思维有点像尺取法,请配合代码食用。
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
string s, t;
int pla[200005];
int main()
{
cin >> s >> t;
int j = 0, k = 1;
pla[0] = -1; // 有趣的操作
for(int i = 0; i < s.size(); i++){
if(j < t.size() && s[i] == t[j]){
pla[k++] = i; // 记录第一个子串的位置
j++;
}
}
for(int i = 0; i < k; i++){
printf("i:%d pla[]:%d\n", i, pla[i]);
} cout << endl;
int ans = 0;
for(int i = s.size() - 1, j = 0; i >= 0; i--){
ans = max(ans, i - pla[t.size() - j]);
printf("i:%d j:%d pla[]:%d i-pla[]:%d\n", i, j, pla[t.size() - j], i - pla[t.size() - j]);
if(j < t.size() && s[i] == t[t.size() - j - 1]){ // 把t子串的末尾依次往前找
j++; // 请耐心思考一下
}
}
cout << ans << endl;
return 0;
}
补题 E.Boxers
题意 在n个数里,可以对任意数+1或者-1或者不变,但是1不能变成0。问经过处理后的n个数里有多少个不同的数字。
分析 利用桶排,都往下取,因为1不能减为0.(思考一下,非常简单)
小插曲 我又看错题意了quq
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#include<string>
#include<stack>
using namespace std;
int n;
int a[1500004];
int main()
{
int num;
cin >> n;
for(int i = 0; i < n; i++){
cin >> num;
a[num]++;
}
int ans = 0;
for(int i = 1; i <= 150001; i++){
if(a[i-1]){
ans++;
}
else if(a[i]){
ans++;
a[i]--; // 减1往下取,因为减1的数没有
}
else if(a[i+1]){
ans++;
a[i+1]--; // 同理
}
}
cout << ans << endl;
return 0;
}
补题 F1. Complete the Projects (easy version)
题意 处理n个事件,每次处理时r必须不能小于\(a_i\),处理完后r的值会加上\(b_i\)的值(\(b_i\)可以是负数),问是否能处理完n个事件,处理完时r不能小于0。
分析 典型的贪心题目,需要排序。
- 当 \(b \geq 0\) 时,取a小的;
- 当 \(b \leq 0\) 时,取 a+b 大的。
证明请看参考博客
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#include<string>
#include<stack>
using namespace std;
struct node{
int a, b;
}pro[102];
int n, r;
node one[102];
node two[102];
bool cmp1(node a, node b){
return a.a < b.a;
}
bool cmp2(node a, node b){
return a.a+a.b > b.a+b.b;
}
int main()
{
cin >> n >> r;
int num, d, cnt1 = 0, cnt2 = 0;
for(int i = 0; i < n; i++) {
cin >> num >> d;
node e; e.a = num; e.b = d;
if(d >= 0){
one[cnt1++] = e;
}
else {
two[cnt2++] = e;
}
}
sort(one, one + cnt1, cmp1);
sort(two, two + cnt2, cmp2);
// cout << endl;
// for(int i = 0; i < cnt1; i++){
// cout << one[i].a << " " << one[i].b << endl;
// } cout << endl;
//
// for(int i = 0; i < cnt2; i++){
// cout << two[i].a << " " << two[i].b << endl;
// } cout << endl;
bool can = true;
for(int i = 0; i < cnt1; i++){
if(r < one[i].a){
can = false;
break;
}
else r += one[i].b;
}
for(int i = 0; i < cnt2; i++){
if(r < two[i].a){
can = false;
break;
}
else r += two[i].b;
}
if(r < 0) can = false;
if(can) cout << "YES\n";
else cout << "NO\n";
return 0;
}
【cf比赛练习记录】Codeforces Round #579 (Div. 3)的更多相关文章
- Codeforces Round #579 (Div. 3)
Codeforces Round #579 (Div. 3) 传送门 A. Circle of Students 这题我是直接把正序.逆序的两种放在数组里面直接判断. Code #include &l ...
- Codeforces Round #579 (Div. 3) 题解
比赛链接:https://codeforc.es/contest/1203/ A. Circle of Students 题意:\(T\)组询问,每组询问给出\(n\)个数字,问这\(n\)个数字能否 ...
- 双指针(最大删除子串)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 题意:一堆人坐成一个环,问能否按逆时针或者顺时针 ...
- CF每日一练 Codeforces Round #520 (Div. 2)
比赛过程总结:过程中有事就玩手机了,后面打的状态不是很好,A题理解错题意,表明了内心不在状态,B题想法和思路都是完全正确的,但是并没有写出来,因为自己代码能力不强,思路不是特别清晰,把代码后面写乱了, ...
- [CF百场计划]Codeforces Round #617 (Div. 3)
A. Array with Odd Sum Description You are given an array \(a\) consisting of \(n\) integers. In one ...
- 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 ...
- Codeforces Round #579 (Div. 3) D2. Remove the Substring (hard version) (思维,贪心)
题意:给你一个模式串\(t\),现在要在主串\(s\)中删除多个子串,使得得到的\(s\)的子序列依然包含\(t\),问能删除的最长子串长度. 题解:首先,我们不难想到,我们可以选择\(s\)头部到最 ...
- Codeforces Round #579 (Div. 3)D(字符串,思维)
#include<bits/stdc++.h>using namespace std;char s[200007],t[200007];int last[200007][27],nxt[2 ...
随机推荐
- 16 doc values 【正排索引】
搜索的时候,要依靠倒排索引:排序的时候,需要依靠正排索引,看到每个document的每个field,然后进行排序,所谓的正排索引,其实就是doc values 在建立索引的时候,一方面会建立倒排索引, ...
- 2019 钢银java面试笔试题 (含面试题解析)
本人3年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条. 钢银等公司offer,岗位是Java后端开发,最终选择去了 钢银. 面试了很多家公司,感觉大部分公司考察的点都差 ...
- selenium浏览器自动化测试框架文档(修正版)
写在最前面:目前自动化测试并不属于新鲜的事物,或者说自动化测试的各种方法论已经层出不穷,但是,能够在项目中持之以恒的实践自动化测试的团队,却依旧不是非常多.有的团队知道怎么做,做的还不够好:有的团队还 ...
- Django 信号使用问题
Django 信号使用问题: 在使用django内置信号修改新注册的用户密码的时候,发现内置信号没有被触发.百度&官方文档找到了答案 1.信号的函数应该放在哪里? 这段代码应该放在哪里? 严格 ...
- 【开发笔记】- Java写入、读取文本
文本写入 public static void createFile(String input) throws IOException { //设置文件路径 String filePath = &qu ...
- Django:内置组件Content-Type
12.Django组件之Content_Type 1.帮助我们生成了一张表,里面有所有表名.这样不再自建表在表中填表名,用Foreignkey获取 2.为了让我们快速进入插入数据,填写一个字段Gene ...
- linux技能点三 find grep
find: 1. 按文件名查找 find . -name "a*.txt" 注意双引号: 2. 按文件大小查找 find .-size [+/-] ...
- day 08作业 预科
有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中 lt=[11,22,3 ...
- 二十五、sql中where条件在数据库中提取与应用浅析
问题描述 一条SQL,在数据库中是如何执行的呢?相信很多人都会对这个问题比较感兴趣.当然,要完整描述一条SQL在数据库中的生命周期,这是一个非常巨大的问题,涵盖了SQL的词法解析.语法解析.权限检查. ...
- python函数调用时参数传递方式
python函数调用时参数传递方式 C/C++参数传递方式 对于C程序员来说,我们都知道C在函数调用时,采用的是值传递,即形参和实参分配不同的内存地址,在调用时将实参的值传给实参,在这种情况下,在函数 ...