SMU Summer 2023 Contest Round 5

A. Points in Segments

\(\mathcal{O}(n \times m)\) 做法数据范围小,直接把每次的\(l - r\)跑一遍标记一下,最后跑一遍循环统计哪些没有被标记的并且输出就好了

#include <bits/stdc++.h>
#define int long long using namespace std; signed main() { ios::sync_with_stdio(false);cin.tie(nullptr); int n,m;
cin >> n >> m;
vector<int> a(m + 1);
int ans = 0;
for(int i = 0;i < n;i ++){
int x,y;
cin >> x >> y;
for(int j = x;j <= y;j ++){
a[j] = 1;
}
} for(int i = 1;i <=m;i ++ ){
ans += (a[i] == 0);
} if(!ans){
cout << 0 << endl;
}else{
cout << ans << endl;
for(int i = 1;i <= m;i ++)
if(!a[i])
cout << i << ' ';
} return 0;
}

\(\mathcal{O}(n + m)\) 做法数据再大点就可以开个前缀和数组,让每次输入的\(l\)所在的值为1,\(r\)的值为-1,然后跑一遍前缀和,统计为0的个数就行

#include <bits/stdc++.h>

using namespace std;

int main() {

	int n, m;
cin >> n >> m;
vector<int> cnt(m + 2);
for (int i = 0; i < n; ++i) {
int l, r;
cin >> l >> r;
++cnt[l];
--cnt[r + 1];
}
for (int i = 1; i <= m; ++i)
cnt[i] += cnt[i - 1]; vector<int> ans;
for (int i = 1; i <= m; ++i) {
if (cnt[i] == 0)
ans.push_back(i);
} cout << ans.size() << endl;
for (auto it : ans) cout << it << " ";
cout << endl; return 0;
}

B. Obtaining the String

\(\mathcal{O}(n^3)\) 长度只有50,直接跑暴力了,每次对比s和t是否相同,不同就去s后面找到当前与t相同的,然后从后一个一个交换过来,判断s能不能变成b就看s里每个字符数量和b是不是一样

#include <bits/stdc++.h>
#define int long long using namespace std; signed main() { ios::sync_with_stdio(false);cin.tie(nullptr); int n;
cin >> n;
string s,t;
cin >> s >> t; if(s == t){
cout << 0 << endl;
return 0;
} vector<int> numa(100),numb(100);
for(int i = 0;i < s.size();i ++){
numa[s[i] - 'a']++;
numb[t[i] - 'a']++;
} for(int i = 0;i <= 100;i ++){
if(numa[i] != numb[i]){
cout << -1 << endl;
return 0;
}
} int ans = 0;
vector<int> res;
for(int i = 0;i < n - 1;i ++){
if(s[i] == t[i]) continue;
for(int j = i + 1;j < n;j ++){
if(s[j] == t[i]){
for(int k = j;k > i;k--){
swap(s[k],s[k - 1]);
ans++;
res.push_back(k);
}
break;
}
}
} cout << ans << endl;
for(auto i : res)
cout << i << ' '; return 0;
}

\(\mathcal{O}(n^2)\) 做法就是在后面找到了之后单独拿出来从后跑一遍

#include <bits/stdc++.h>

using namespace std;

int main() {

	int n;
string s, t;
cin >> n >> s >> t; vector<int> ans;
for (int i = 0; i < n; ++i) {
if (s[i] == t[i]) continue;
int pos = -1;
for (int j = i + 1; j < n; ++j) {
if (s[j] == t[i]) {
pos = j;
break;
}
}
if (pos == -1) {
cout << -1 << endl;
return 0;
}
for (int j = pos - 1; j >= i; --j) {
swap(s[j], s[j + 1]);
ans.push_back(j);
}
} cout << ans.size() << endl;
for (auto it : ans) cout << it + 1 << " ";
cout << endl; return 0;
}

C. Songs Compression

\(\mathcal{O}(nlog_2n)\) 先把所有歌都压缩,如果还是小于\(m\)的话直接输出-1,否则就将内存的差值从小到大排序,每次从最小的差值开始加,看看能还原多少首歌,最后用总数去减掉还原的就是压缩的个数了

#include <bits/stdc++.h>
#define int long long using namespace std; typedef pair<int,int> PII; signed main() { ios::sync_with_stdio(false);cin.tie(nullptr); int n,m;
cin >> n >> m; vector<PII> a(n);
for(auto& [x,y] : a)
cin >> x >> y; sort(a.begin(),a.end(),[](PII x,PII y){
return x.first - x.second < y.first - y.second;
}); int ans = 0, now = 0;
for(auto i : a){
now += i.second;
}
if(now > m){
cout << -1 << endl;
return 0;
} for(auto i : a){
if(i.first - i.second + now <= m){
now += i.first - i.second;
ans ++;
}else
break;
}
cout << n - ans << endl; return 0;
}

D. Walking Between Houses

\(\mathcal{O}(k)\) 首先\(s < k\)和\(s < (n - 1) \times k\)这两种情况肯定是不行的,至于为什么,可以自己推导一下,然后就是一个贪心的思想,每次走当前能走的最远距离,即每次能走的距离为\(min(n-1,s-(k-1))\),然后s也要跟着更新

#include <bits/stdc++.h>
#define int long long using namespace std; signed main() { ios::sync_with_stdio(false);cin.tie(nullptr); int n,k,s;
cin >> n >> k >> s; if(s > (n - 1) * k || s < k){
cout << "NO" << endl;
return 0;
} cout << "YES" << endl;
int sum = 0, now = 1;
for(; k;k-- ){
int i = min(s - k + 1, n - 1);
s -= i;
if( i + now > n){
cout << now - i << ' ';
now -= i;
}else {
cout << now + i << ' ';
now += i;
}
} return 0;
}

后面摆了,反正cf分还没到那个段位(\(Orz\)

SMU Summer 2023 Contest Round 5的更多相关文章

  1. 2015 Astar Contest - Round 3 题解

    1001 数长方形 题目大意 平面内有N条平行于坐标轴的线段,且不会在端点处相交 问共形成多少个矩形 算法思路 枚举4条线段的全部组合.分别作为矩形四条边.推断是否合法 时间复杂度: O(N4) 代码 ...

  2. Contest Round #451 (Div. 2)F/Problemset 898F Restoring the Expression

    题意: 有一个a+b=c的等式,去掉两个符号,把三个数连在一起得到一个数 给出这个数,要求还原等式,length <= 1e6 三个数不能含有前导0,保证有解 解法: 铁头过题法,分类然后各种判 ...

  3. Codeforces Round #284 (Div. 2)A B C 模拟 数学

    A. Watching a movie time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. Sending messages to non-windowed applications -- AllocateHWnd, DeallocateHWnd

    http://delphi.about.com/od/windowsshellapi/l/aa093003a.htm Page 1: How Delphi dispatches messages in ...

  5. Codeforces 240 F. TorCoder

    F. TorCoder time limit per test 3 seconds memory limit per test 256 megabytes input input.txt output ...

  6. cf499B-Lecture 【map】

    http://codeforces.com/problemset/problem/499/B B. Lecture     You have a new professor of graph theo ...

  7. Codeforces 240F. TorCoder 线段树

    线段树统计和维护某一区间内的字母个数.. . . F. TorCoder time limit per test 3 seconds memory limit per test 256 megabyt ...

  8. 物联网学生科协第三届H-star现场编程比赛

    问题 A: 剪纸片 时间限制: 1 Sec 内存限制: 128 MB 题目描写叙述 这是一道简单的题目,假如你身边有一张纸.一把剪刀.在H-star的比赛现场,你会这么做: 1. 将这张纸剪成两片(平 ...

  9. [cf contest 893(edu round 33)] F - Subtree Minimum Query

    [cf contest 893(edu round 33)] F - Subtree Minimum Query time limit per test 6 seconds memory limit ...

  10. 水题 Codeforces Round #307 (Div. 2) A. GukiZ and Contest

    题目传送门 /* 水题:开个结构体,rk记录排名,相同的值有相同的排名 */ #include <cstdio> #include <cstring> #include < ...

随机推荐

  1. Cython编译报错“numpy/arrayobject.h: No such file or directory”解决方案

    问题背景 Cython是用来加速Python程序性能的一个工具,其基本使用逻辑就是将类Python代码(*.pyx扩展格式)编译成\(*.c,*.so\)动态链接库文件,然后就可以在正常的Python ...

  2. hive第三课:Hive函数学习

    Hive函数学习 目录 Hive函数学习 SQL练习 Hive 常用函数 关系运算 数值计算 条件函数(主要使用场景是数据清洗的过程中使用,有些构建表的过程也是需要的) 日期函数重点!!! 字符串函数 ...

  3. python基础-基本语句

    1 条件语句 在进行逻辑判断时,我们需要用到条件语句,Python 提供了 if.elif.else 来进行逻辑判断.格式如下所示: 1 if 判断条件1: 2 执行语句1... 3 elif 判断条 ...

  4. 【论文阅读】GRI: General Reinforced Imitation and its Application to Vision-Based Autonomous Driving

    Column: December 30, 2021 11:01 PM Last edited time: January 10, 2022 4:45 PM Sensor/组织: 3 RGB; 曾经短暂 ...

  5. 是什么导致了客户端TCP连不上服务器?

      这几年做了很多基于 GGTalk开源即时通讯系统 的定制开发项目,经常会碰到如下两个问题,分享出来,应该对大家会有所帮助: (1)定制开发完成后,在给客户部署GGTalk即时通讯服务端到正式的服务 ...

  6. CF1864C 题解

    \(x = 2^k\) 是好做的,每次以 \(2^{k-1}\) 为因数即可. 对于其他情况,考虑每次让 \(x\) 减去其二进制下最低位的 \(1\) 直至变成 \(2^k\). 这种策略下显然每个 ...

  7. frp内网穿透:基于centos8 云服务器和debian12客户端服务器

    前言 入了一台本地工控机盒子,刷成了debian12系统,性能比云服务器要好一点,现在想要远程访问这台盒子,但是盒子又没有公网地址,所以想通过内网穿透的方式,通过云服务器转发请求实现内网穿透.原来体验 ...

  8. Java常见问题-多线程

    现在有 T1.T2.T3 三个线程,你怎样保证 T2 在 T1 执行完后执行,T3 在 T2 执行完后执行? 这个多线程问题比较简单,可以用 join 方法实现. 在 Java 中 Lock 接口比 ...

  9. C#开发一个可被带参数调用并返回数据的控制台应用程序

    1 标准输出流控制台程序: namespace ReturnConsoleWrite { class Program { static void Main(string[] args) { //程序功 ...

  10. Apache Kyuubi 在B站大数据场景下的应用实践

    01 背景介绍 近几年随着B站业务高速发展,数据量不断增加,离线计算集群规模从最初的两百台发展到目前近万台,从单机房发展到多机房架构.在离线计算引擎上目前我们主要使用Spark.Presto.Hive ...