题面

A B C D

难度:红 橙 橙 黄

题解

A

题目大意:

判断一个正整数 \(w\) 能否表示成两个正偶数之和。

解题思路:

考虑分类讨论 \(w\)。

对于 \(1\) 和 \(2\),显然为 NO

对于 \(w \ge 3\),考虑将其表示为 \(x + 2\)。根据题意,若 \(x\) 为偶数,则答案显然必为 YES;否则必然为 NO。因为 \(2\) 是偶数,所以 \(w\) 与 \(x\) 同奇偶。结论的证明留作思考题。

综上,若 \(w \ge 3\) 且 \(w\) 为偶数则答案为 YES,否则为 NO

#include <bits/stdc++.h>
using namespace std;
int main() {
int w;
scanf("%d", &w);
if (w % 2 == 0 && w != 2) puts("YES");
else puts("NO");
return 0;
}

B

题目大意:

构造一个数组 \(a\),使得 \(\text {minTime}_i \le a_i \le \text{maxTime}_i(1 \le i \le d)\),且 \(\sum _{i=1} ^ d a_i = \text{sumTime}\),或输出 NO 表示不存在这样的数组。

解题思路:

先求出 \(x = \sum _{i=1} ^ d \text{minTime}_i\) 和 \(y = \sum _{i=1} ^ d \text{maxTime}_i\)。若满足 \(x \le d \le y\),则存在这样的数组,否则不存在。下面是一种构造思路:

接下来,考虑先设 \(a_i = \text{minTime}_i\),然后求 \(z = \text{sumTime} - x\)。接下来,考虑从 \(1 \le i \le d\),将 \(a_i\) 的值设为 \(\text{maxTime}_i\),并将 \(z\) 减去 \(\text{maxTime}_i - \text{minTime}_i\),直到 \(z \le 0\)。此时令 \(a_i \leftarrow a_i + z\),即为答案。证明过程略。

#include <bits/stdc++.h>
using namespace std;
int sum, d, x, y, z;
int a[100010], b[100010], c[100010];
int main() {
scanf("%d%d", &d, &sum);
for (int i = 1; i <= d; i++) scanf("%d%d", &b[i], &c[i]), x += b[i], y += c[i];
if (x <= sum && sum <= y) {
puts("YES");
if (x == sum) {
for (int i = 1; i <= d; i++) printf("%d ", b[i]);
putchar('\n');
} else if (y == sum) {
for (int i = 1; i <= d; i++) printf("%d ", c[i]);
putchar('\n');
} else {
for (int i = 1; i <= d; i++) a[i] = b[i];
z = sum - x;
for (int i = 1; i <= d; i++) {
int tmp = c[i] - b[i];
if (z > tmp) {
z -= tmp;
a[i] += tmp;
} else {
a[i] += z;
break;
}
}
for (int i = 1; i <= d; i++) printf("%d ", a[i]);
putchar('\n');
}
} else {
puts("NO");
}
return 0;
}

C

题目大意:

给定 \(n\) 次操作,每次向集合中加入一个字符串(当集合中没有这个字符串时),或加入【该字符串,连接,最小能使其不重复的正整数】的字符串。

解题思路:

这道题可以用 STL 中的 map 解决。使用 map 统计字符串出现的次数即可。

#include <bits/stdc++.h>
using namespace std;
int n;
map<string, int> mp;
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
while (n--) {
string s;
cin >> s;
if (!mp[s]) {
mp[s] = 1;
cout << "OK\n";
} else {
cout << s << mp[s] << '\n';
mp[s]++;
}
}
return 0;
}

D

题目大意:

求去除一些元素之后,严格二维上升子排列。

解题思路:

注意到 \(O(n^2)\) 可过。记忆化搜索即可。

可以使用链表存储序列。

#include <bits/stdc++.h>
using namespace std;
int n, d, w, a[5005], b[5005], f[5005], nxt[5005];
bool nok[5005];
int dfs(int x) {
if (f[x]) return f[x];
f[x] = 1;
for (int i = 1; i <= n; i++) {
if (nok[i]) continue;
if (a[i] > a[x] && b[i] > b[x] && f[x] < dfs(i) + 1) {
f[x] = dfs(i) + 1;
nxt[x] = i;
}
}
return f[x];
}
int main() {
scanf("%d%d%d", &n, &d, &w);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &a[i], &b[i]);
if (a[i] <= d || b[i] <= w) nok[i] = 1;
}
printf("%d\n", dfs(0) - 1);
int pos = nxt[0];
while (pos) {
printf("%d ", pos);
pos = nxt[pos];
}
return 0;
}

Codeforces 4 A-D的更多相关文章

  1. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  2. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  3. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  4. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  5. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  6. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  7. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  8. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  9. CodeForces - 148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...

  10. CodeForces - 453A Little Pony and Expected Maximum

    http://codeforces.com/problemset/problem/453/A 题目大意: 给定一个m面的筛子,求掷n次后,得到的最大的点数的期望 题解 设f[i]表示掷出 <= ...

随机推荐

  1. 2. 从0开始学ARM-CPU原理,基于ARM的SOC讲解

    关于ARM的一些基本概念,大家可以参考我之前的文章: <到底什么是Cortex.ARMv8.arm架构.ARM指令集.soc?一文帮你梳理基础概念[科普]> 关于ARM指令用到的IDE开发 ...

  2. wifi基础(一):无线电波与WIFI信号干扰、衰减

    liwen01 2024.08.18 前言 无论是在产品开发还是在日常生活中,在使用无线网络的时候,都会经常遇到一些信号不好的问题,也会产生不少疑问: 为什么我们在高速移动的高铁上网络会变慢? 为什么 ...

  3. JavaScript设计模式样例二十二 —— 访问者模式

    访问者模式(Visitor Pattern) 定义:使用一个访问者类,改变元素类的执行算法.通过这种方式,元素的执行算法可以随着访问者改变而改变.目的:将数据结构与数据操作分离.场景:您在朋友家做客, ...

  4. 【Python爬虫】之爬取页面内容、图片以及用selenium爬取

    下面不做过多文字描述: 首先.安装必要的库 # 安装BeautifulSoup pip install beautifulsoup4 # 安装requests pip install requests ...

  5. 基于PaddleNLP信息抽取,uie微调打造自己专属的信息抽取模型

    基于PaddleNLP信息抽取,uie微调打造自己专属的信息抽取模型 UIE模型简介 UIE优势 应用示例 UIE开箱即用 UIE适用抽取示例 命名实体识别(Named Entity Recognit ...

  6. c++学习笔记(三):函数++

    函数PLUS 函数默认参数 在c++中,函数的形参列表中的形参是可以有默认值的.调用函数时,如果未传递参数的值(传入参数为空),则会使用默认值,如果指定了值,则会忽略默认值,使用传递的值. 语法:返回 ...

  7. Graph 学习

    Graph basic terms 里面介绍了常见的一些基本概念,如 directed/undirected, weighted, cyclic/acyclic, Adjacency Matrix, ...

  8. Seata 四大模式详解

    分布式事务 参考文章: 分布式事务实战方案汇总 https://www.cnblogs.com/yizhiamumu/p/16625677.html 分布式事务原理及解决方案案例https://www ...

  9. C++ STL queue容器——队列

    queue容器 基本概念 queue是一种**先进先出的数据结构,它有两个出口,queue容器允许从一端新增元素,从另一端移除元素. queue容器没有迭代器,所有元素进出都必须符合"先进先 ...

  10. Tauri2.0+Vite5聊天室|vue3+tauri2+element-plus仿微信|tauri聊天应用

    原创tauri2.0+vue3+pinai2仿QQ/微信客户端聊天Exe程序TauriWinChat. tauri2-vue3-winchat自研vite5+tauri2.0+vue3 setup+e ...