比赛链接: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)的更多相关文章

  1. Codeforces Round #637 (Div. 2) 题解

    A. Nastya and Rice 网址:https://codeforces.com/contest/1341/problem/A Nastya just made a huge mistake ...

  2. 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 ...

  3. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  4. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  5. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  6. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

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

  8. 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 ...

  9. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

随机推荐

  1. 白日梦的Elasticsearch笔记(一)基础篇

    目录 一.导读 1.1.认识ES 1.2.安装.启动ES.Kibana.IK分词器 二.核心概念 2.1.Near Realtime (NRT) 2.2.Cluster 2.3.Node 2.4.In ...

  2. IO软件层次结构与假脱机技术

    IO软件层次结构 用户层软件->设备独立性软件->设备驱动软件->中断处理程序->硬件 用户层软件实现与用户交互的接口,用户可直接使用该层提供的,与IO操作相关的库函数对设备进 ...

  3. redis持久化怎么选?成年人从来不做选择...

    前言 面试官:你知道 redis 是的怎么做持久化的吗? 我:我知道 redis 有两种方式,一种是 RDB,一种是 AOF. 面试官:那这两种方式具体是怎么做的,它们的区别是什么,生产环境中到底应该 ...

  4. 【Oracle】Script to Collect DRM Information (drmdiag.sql) (文档 ID 1492990.1)

    脚本对应如下: The following (drmdiag.sql) is a script to collect information related to DRM (Dyanamic Reso ...

  5. Nacos使用和注册部分源码介绍

    Nacos简单介绍 Nacos致力于帮助您发现.配置和管理微服务.Nacos提供了一组简单易用的特性集,帮助您快速实现动态服务发现.服务配置.服务元数据及流量管理.Nacos帮助您更敏捷和容易地构建. ...

  6. 通过logmnr找到被修改前的存储过程

    1.找到存储过程被修改时的归档日志 SELECT NAME FROM V$ARCHIVED_LOG WHERE FIRST_TIME BETWEEN TO_DATE('20191118080000', ...

  7. Nginx(七):location的使用以及nginx关闭原理

    上一篇中,我们了解了如何nginx的配置原则及解析框架,以及解析location配置的具体实现,相信大家对该部分已经有了比较深刻的认识. 本篇,我们进一步来了解下,解析之后的配置,如何应用到实际中的吧 ...

  8. Flink的状态与容错

    本文主要运行到Flink以下内容 检查点机制(CheckPoint) 状态管理器(StateBackend) 状态周期(StateTtlConfig) 关系 首先要将state和checkpoint概 ...

  9. Azure Terraform(七)利用Azure DevOps 实现自动化部署基础资源(补充)

    一,引言 之前一篇文章有讲解到利用 利用Azure DevOps 实现自动化部署基础资源,当时 TF 代码没有针对 Azure 各个资源的封装,所有的资源代码全部写在一个 main.tf 文件中.然后 ...

  10. JVM虚拟机垃圾回收(GC)算法及优缺点

    一.什么是GC   GC是jvm的垃圾回收,垃圾回收的规律和原则为:   次数上频繁收集新生区(Young)   次数上较少收集养老区(Old)   基本上不动永久区(Perm) 二.GC算法(分代收 ...