Codeforces Round #637 (Div. 2)
比赛链接:https://codeforces.com/contest/1341
A - Nastya and Rice
题意
有 n 堆米,每堆质量在 [a-b,a+b] 之间,这些米的总质量是否可能在 [c-d,c+d] 之间。
思路
n 堆米的最小总质量为 n*(a-b),最大总质量为 n*(a+b),即判断区间 [n*(a-b),n*(a+b)] 是否与 [c-d,c+d] 相交。
代码
#include <bits/stdc++.h>
using namespace std; void solve() {
int n, a, b, c, d; cin >> n >> a >> b >> c >> d;
int mi = n * (a - b);
int mx = n * (a + b);
if (c + d < mi or c - d > mx)
cout << "No" << "\n";
else
cout << "Yes" << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}
B - Nastya and Door
题意
有一数组 a,定长 k 最多能完整覆盖多少 a[i-1] < a[i] 且 a[i] > a[i+1] 的长为 3 的区间,不考虑数组两端。
思路一
前缀和记录每个端点为止的符合条件的区间数量,之后枚举定长的起始点,覆盖区间的右端点需要再减一来错峰。
代码一
#include <bits/stdc++.h>
using namespace std; void solve() {
int n, k; cin >> n >> k;
int a[n + 1]= {};
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int cnt[n + 1] = {};
for (int i = 1; i <= n; i++) {
cnt[i] += cnt[i - 1];
if (a[i] > a[i - 1] and a[i] > a[i + 1])
++cnt[i];
}
int mx = 1, st = 1;
for (int i = 1; i + k - 1 <= n; i++) {
int l = i, r = i + k - 2;
int sub = cnt[r] - cnt[l] + 1;
if (sub > mx) mx = sub, st = l;
}
cout << mx << ' ' << st << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}
思路二
记录所有符合条件区间的左右端点,对于每个枚举的起始点二分查找最近的区间左右端点。
代码二
#include <bits/stdc++.h>
using namespace std; void solve() {
int n, k; cin >> n >> k;
int a[n + 1]; for (int i = 1; i <= n; i++) cin >> a[i];
vector<int> l, r;
for (int i = 2; i <= n - 1; i++) {
if (a[i - 1] < a[i] and a[i] > a[i + 1]) {
l.push_back(i - 1);
r.push_back(i + 1);
}
}
int mx = 1, st = 1;
for (int i = 1; i <= n; i++) {
int lf = lower_bound(l.begin(), l.end(), i) - l.begin();
int rt = upper_bound(r.begin(), r.end(), i + k - 1) - r.begin();
if (rt > 0 and rt - lf + 1 > mx) {
mx = rt - lf + 1;
st = i;
}
}
cout << mx << ' ' << st << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}
C - Nastya and Strange Generator
题意
[1,2,3,...,n] 中每次可从一点起向右连续选取所有未选取的位置并依次放置 1 ~ n,问所有可能的放置中是否有当前排列。
思路
因为是连续放置的所以 a[i - 1] + 1 = a[i] 或 a[i - 1] > a[i],即 a[i] - a[i - 1] ≤ 1。
代码
#include <bits/stdc++.h>
using namespace std; void solve() {
int n; cin >> n;
int a[n]; for (int &i : a) cin >> i;
for (int i = 0; i + 1 < n; i++) {
if (a[i + 1] - a[i] > 1) {
cout << "No" << "\n";
return;
}
}
cout << "Yes" << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}
D - Nastya and Scoreboard
题意
有一液晶数字板,其中亮着一些段,问再点亮 k 段所能构成的最大数字。
思路
先确定每位可以用掉几段,ok[i][j] 表示第 i 位可以用掉 j 段,之后从后向前构造,dp[i][j] 表示构造到第 i 位时用 j 段是否可行,dp[0][k] 可行即有解(构造 n-1 → 0 位)。因为在 dp 的过程中每一位每一种可行的情况都是与之后一系列的位相关联的,所以当输出第一位的最大可行数字后后面一系列的位就已经确定了。
代码
#include <bits/stdc++.h>
using namespace std; const int M = 2200; string str[10] = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
int a[M], s[10];
bool ok[M][8], dp[M][M]; int main() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 7; j++) {
s[i] = 2 * s[i] + str[i][j] - '0';
}
}
int n, k; cin >> n >> k;
for (int i = 0; i < n; i++) {
int a_i = 0;
for (int j = 0; j < 7; j++) {
char c; cin >> c;
a_i = 2 * a_i + c - '0';
}
a[i] = a_i;
for (int j = 0; j < 10; j++) {
if ((a[i] & s[j]) == a[i]) {
int cnt = __builtin_popcount(a[i] ^ s[j]);
ok[i][cnt] = true;
}
}
}
dp[n][0] = true;
for (int i = n - 1; i >= 0; i--)
for (int j = 0; j < M; j++)
if (dp[i + 1][j])
for (int k = 0; k <= 7; k++)
if (ok[i][k])
dp[i][j + k] = true;
if (!dp[0][k]) cout << "-1";
else {
for (int i = 0; i < n; i++)
for (int j = 9; ; j--)
if ((a[i] & s[j]) == a[i]) {
int cnt = __builtin_popcount(a[i] ^ s[j]);
if (dp[i + 1][k - cnt]) {
k -= cnt; cout << j;
break;
}
}
}
}
Codeforces Round #637 (Div. 2)的更多相关文章
- Codeforces Round #637 (Div. 2) 题解
A. Nastya and Rice 网址:https://codeforces.com/contest/1341/problem/A Nastya just made a huge mistake ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
- Codeforces Round #262 (Div. 2) 1004
Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...
- Codeforces Round #371 (Div. 1)
A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...
随机推荐
- 【JavaWeb】EL 表达式
EL 表达式 简介 EL(Expression Language),即表达式语言. EL 表达式主要是代替 jsp 页面中 表达式脚本 在 jsp 页面中进行数据的输出,因为 EL 表达式在输出数据的 ...
- 【C++】《Effective C++》第六章
第六章 继承与面向对象设计 条款32:确定你的public继承塑模出is-a关系 public隐含的寓意:每个派生类对象同时也是一个基类对象,反之不成立.只不过基类比派生类表现出更一般化的概念,派生类 ...
- servlet+jsp完成简单登录
将用户在注册界面中的数据填充到数据库相对应的表格中.当用户再次登录时,从数据库中拿到相应的数据查询并与页面的数据做对比,判断是否登陆成功. 需要在HTML文件中将form表单上的action属性值设置 ...
- 深入理解static、volatile关键字
static 意思是静态的,全局的.被修饰的东西在一定范围内是共享的,被类的所有实例共享,这时候需要注意并发读写的问题. 只要这个类被加载,Java虚拟机就能根据类名在运行时数据区的方法区内找到他们. ...
- ctfshow—web—web4
打开靶机 发现与web3很相似,测试文件包含未成功 此题有两种解决方法 一.日志注入 查看日志的默认目录,得到了日志文件 ?url=/var/log/nginx/access.log 进行日志注入 & ...
- Spring Initializr中生成的mvnw是干吗的?
当我们使用Spring Initializr来创建Spring Boot工程的时候,有没有发现在工程根目录下有两个名为mvnw的文件: 从命名.图标.扩展名来猜测,这两个文件的作用应该是一样的,只是c ...
- 【高并发】ReadWriteLock怎么和缓存扯上关系了?!
写在前面 在实际工作中,有一种非常普遍的并发场景:那就是读多写少的场景.在这种场景下,为了优化程序的性能,我们经常使用缓存来提高应用的访问性能.因为缓存非常适合使用在读多写少的场景中.而在并发场景中, ...
- 量子化学Gaussian技术实战课 2021年4月9号--12号 远程在线教学
材料模拟分子动力学课程 3月19号--22号 远程在线课 lammps分子动力学课程 3月12号--15号 远程在线课 第一性原理VASP实战课 3月25号-28号 远程在线课 量子化学Gaussia ...
- matlab gui matlab gui 鼠标点击显示图像颜色值
首先看看效果 首先功能说明下,运行后通过myfile菜单打开一幅图片之后在axes中显示,由于要使用图片的放大缩小等功能将figure 的菜单栏与工具栏都一并打开了. 界面编程主要是callbac ...
- 大数据系列2:Hdfs的读写操作
在前文大数据系列1:一文初识Hdfs中,我们对Hdfs有了简单的认识. 在本文中,我们将会简单的介绍一下Hdfs文件的读写流程,为后续追踪读写流程的源码做准备. Hdfs 架构 首先来个Hdfs的架构 ...