A. Programming Contest

签到题.

输入输出读完应该就懂了:

从y1枚举到y2,若枚举的年份不在停办年份里则答案加一

void solve() {
int n,m;
cin >> n;
vector<int> a(N),year(N);
cin >> m;
for(int i = 0;i < m;i++){
int y;
cin >> y;
year[y] = 1;
}
int y;
cin >> y;
int ans = 0;
for(int i = n;i <= y;i ++)
if(!year[i])
ans++;
cout << ans << endl;
}

C. Trading

每次应该从价格最便宜的商店购买货物,并卖给价格最贵的商店。用双指针模拟这一贪心策略即可.

#include<bits/stdc++.h>
using namespace std; #define LL long long
void solve(){
LL n;
cin >> n;
vector<pair<LL,LL>> a;
for(LL i = 0;i < n;i ++){
LL x,y;
cin >> x >> y;
a.push_back({x,y});
}
LL sum = 0;
sort(a.begin(), a.end());
for(LL i = 0, j = n - 1; i < j; ){
LL x = min(a[i].second, a[j].second);
sum += (a[j].first - a[i].first) * x;
a[i].second -= x;
a[j].second -= x;
if(a[i].second == 0)
i++;
if(a[j].second == 0)
j--; }
cout << sum << endl;
} int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
LL T;
cin>>T;
while(T--){
solve();
}
return 0;
}

D. New Houses

如果已知 \(k (2\le k \le n)\)个人有邻居,剩下的人没有邻居,怎样选择有邻居的人才能使总满意度最大化?

这是一个经典问题。先假设所有人都是没邻居的,得到总满意度 \(\sum\limits_{i = 1}^n b_i\) 。当第 \(i\) 个人从没邻居变成有邻居时,总满意度将增加 \((a_i - b_i)\)。因此选择 \((a_i - b_i)\) 最大的 \(k\) 个人变成有邻居的即可。排序后可以在 \(\mathcal{O}(n)\)的复杂度内一次性算出 $k = 2, \cdots, n $ 的最大总满意度。

如果 \(k\) 个人有邻居,剩下的人没有邻居,这样的布局至少需要 \(k + 2(n - k) = 2n - k\) 栋房子(即有邻居的人都住在最左边,然后每隔一栋房子住一个没邻居的人)。因此只有满足 \(2n - k \le m\) 才能考虑。

最后,别忘了考虑所有人都没有邻居的情况。这要求 \(m \ge 2n - 1\)。

#include <bits/stdc++.h>
#define endl '\n'
#define int long long using namespace std; const int N = 1e6+10, M = 998244353; typedef unsigned long long ll;
typedef pair<int,int> PII; int n,m,t,k;
map<int,int> mp;
void solve() {
cin >> n >> m;
vector<int> A(n + 1), B(n + 1),ve;
for(int i = 1;i <= n;i ++){
cin >> A[i] >> B[i];
ve.push_back(A[i] - B[i]);
} sort(ve.begin(), ve.end()); int ans = 0, now = 0;
for(int i = 1;i <= n;i ++)
now += B[i]; if(m >= 2 * n - 1)
ans = now; now += ve[n - 1];
for(int i =2;i <= n;i ++){
now += ve[n - i];
if(2 * n - i <= m)
ans = max(ans, now);
} cout << ans << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int Ke_scholar = 1;
cin >> Ke_scholar;
while(Ke_scholar--)
solve();
return 0;
}
/*
*/
 

I. Path Planning

假设答案为 \(x\),那么存在一条路径,使得从 \(0\) 到 \((x-1)\) 的每个整数都在路径上。这一条件满足二分性,因此我们可以二分答案 \(x\),并检查是否存在这样的路径。

由于每一步只能往右或者往下走,因此将路径上每个格子的坐标按行为第一关键字,列为第二关键字排序后,排在前面的坐标的列编号,一定小于等于排在后面的坐标的列编号。

因此,将从 0 到 \((x-1)\) 的每个整数所在的格子的坐标排序,并检查列编号是否满足以上条件,即可判断是否存在一条路径,使得这些整数都在路径上。实际实现时,不需要使用排序函数。只要依此枚举每个格子,若格子里的整数小于 \(x\) 则把格子加入 vector,这样得到的 vector 就已经按枚举的顺序排序了。

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define endl '\n'
#define int long long using namespace std; const int N = 1e6+10, M = 998244353; typedef unsigned long long ll;
typedef pair<int,int> PII; int n,m,t,k,a[N];
// 将从 0 到 x - 1 所在的格子坐标“排序”,检查前面的列坐标是否小于等于后面的列坐标
bool check(int x){
// 实际实现时,不需要使用排序函数,
// 直接按顺序枚举每个格子,若格子里的整数小于 x 则把格子加入 vector,
// 这样得到的 vector 就已经按枚举的顺序排序了
// 而且甚至连 vector 也不用真的维护,
// 因为我们只关心 vector 最后一个元素的列坐标,和当前列坐标的大小关系,
// 直接用变量 last 维护最后一个元素的列坐标即可
int last = 0;
for(int i = 0;i <n;i ++){
for(int j = 0;j < m;j ++){
if(a[i * m + j] < x){
if(last > j)
return false;
last = j;
}
}
} return true;
}
void solve() {
cin >> n >> m;
for(int i = 0;i < n;i ++)
for(int j = 0;j <m ;j++)
cin >> a[i * m + j]; int l = 0, r = n * m;
while(l < r){
int mid = (l + r + 1) >> 1;
if(check(mid)) l = mid ;
else r = mid - 1;
} cout << l << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int Ke_scholar = 1;
cin >> Ke_scholar;
while(Ke_scholar--)
solve();
return 0;
}
/*
*/
 

K. Peg Solitaire

因为数据范围很小,直接模拟暴力就能过

#include<bits/stdc++.h>

using namespace std;

const int N = 1e6+10;

#define int long long

int ans = 100;
int u[] = {0,2,0,-2};
int v[] = {2,0,-2,0};
int uu[] = {0,1,0,-1};
int vv[] = {1,0,-1,0};
int n,m,k;
void dfs(int x,int y, vector<vector<int>>& gg ){ for(int i = 0;i < 4;i ++){
int dx = u[i] + x;
int dy = v[i] + y;
int d1 = uu[i] + x;
int d2 = vv[i] + y;
if(dx > 0 && dy > 0 && dx <= n && dy <= m && gg[d1][d2] && !gg[dx][dy]){
gg[dx][dy] = 1;
gg[x][y] = gg[d1][d2] = 0;
for(int i = 1;i <= n;i ++){
for(int j = 1;j <= m;j ++){
if(gg[i][j]){
auto ggg = gg;
dfs(i,j,ggg);
}
}
}
gg[dx][dy] = 0;
gg[x][y] = gg[d1][d2] = 1;
}
}
int sum = 0;
for(int i = 1;i <= n;i ++)
for(int j = 1;j <= m;j ++)
if(gg[i][j])
sum++;
ans = min(ans, sum);
return ;
}
void solve(){
ans = 100;
cin >> n >> m >> k;
vector<vector<int> > g(n + 1, vector<int> (m + 1, 0));
for(int i = 0;i < k; i++){
int x,y;
cin >> x >> y;
g[x][y] = 1;
}
for(int i = 1;i <= n;i ++){
for(int j = 1;j <= m;j ++){
if(g[i][j]){
auto gg = g;
dfs(i,j,gg);
}
}
}
cout << ans << endl;
} int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int T;
cin>>T;
while(T--){
solve();
}
return 0;
}

其余题解参考2023 广东省大学生程序设计竞赛 - SUA Wiki

因为咱很懒,所以有的题解直接拿来用了

SMU Spring 2023 Contest Round 7的更多相关文章

  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. Why to Not Not Start a Startup

    我花了周六,周日两天的时间,把这篇长文给阅读完了.很受益,改变了我的很多认知,也给我开拓了视野. 转载: Want to start a startup? Get funded by Y Combin ...

  8. Codeforces 240F. TorCoder 线段树

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

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

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

  10. [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 ...

随机推荐

  1. 可以把 FolkMQ 内嵌到 SpringBoot3 项目里(可内嵌的消息中间件,纯血国产)

    之前发了<把 FolkMQ 内嵌到 SpringBoot2 项目里(比如 "诺依" 啊)>.有人说都淘态了,有什么好内嵌的...所以再发个 SpringBoot3 Fo ...

  2. [AGC030C] Coloring Torus

    非常巧妙的一道构造题,发现对于所构造的 \(n\) 有上限,那么对于 \(K<=500\) 的情况,很好构造,每行全是一个数就行了,对于 \(K>500\) 的情况,显然每行都是 \(1, ...

  3. SpringMVC-01-回顾MVC架构

    1.什么是MVC MVC是模型(Model).视图(View).控制器(Controller)的简写,是一种软件架构模式. 它通过将业务逻辑.页面控制.显示视图分离的方法来组织代码. 主要作用是降低了 ...

  4. Linux 内核:RCU机制与使用

    Linux 内核:RCU机制与使用 背景 学习Linux源码的时候,发现很多熟悉的数据结构多了__rcu后缀,因此了解了一下这些内容. 介绍 RCU(Read-Copy Update)是数据同步的一种 ...

  5. 【创龙全国产T3核心板】赋能工业领域新发展

    在工业5.0时代浪潮持续推进并具备确定性的时代背景下,工业领域创新升级的需求日益增长,为满足各种工业环境下的应用需求,面向工业领域,创龙科技推出了基于全志T3处理器的元器件全国产化工业级核心板--SO ...

  6. css定位 position:sticky

    今天在做css定位的时候遇到一个问题,我想用fixed定位下来,但是发现这个时候定义的百分百宽度不随着父元素走了而是整个屏幕的百分百,这个就很尴尬了,也不能固定宽度吧,毕竟还要宽度自适应. 这个时候发 ...

  7. Go 如何对多个网络命令空间中的端口进行监听

    Go 如何对多个网络命令空间中的端口进行监听 需求为 对多个命名空间内的端口进行监听和代理. 刚开始对 netns 的理解不够深刻,以为必须存在一个新的线程然后调用 setns(2) 切换过去,如果有 ...

  8. 【ClickHouse问题】更新表字段类型为Nullable(Int32)的列值,最终结果都是固定一个值:93147008???

    问题描述: clickhouse更新表数据.更新的列数据类型是Nullable(Int32),不管更新为什么数值,最后查询的结果都是一个固定值:93147008 问题复现: 1:建一张测试表 CREA ...

  9. Redis常见的16个使用场景

    1.缓存 String类型 例如:热点数据缓存(例如报表.明星出轨),对象缓存.全页缓存.可以提升热点数据的访问数据. 2.数据共享分布式 String 类型,因为 Redis 是分布式的独立服务,可 ...

  10. Spring注解之生命周期@PostConstruct和@PreDestroy

    ****### 简介 从Java EE5规范开始,Servlet增加了两个影响Servlet生命周期的注解(Annotation):@PostConstruct和@PreConstruct. 这两个注 ...