比赛链接:https://codeforces.com/contest/1343

A - Candies

题意

有一数列 x + 2x + 4x + ... + 2k-1x = n,输出 k ≥ 2 时任一满足该等式的一个 x 值。

思路

等比数列求和得 (2k-1) x = n,枚举 k 即可。

代码

#include <bits/stdc++.h>
using namespace std; void solve() {
int n; cin >> n;
for (int i = 2; i < 32; i++) {
if (n % ((1 << i) - 1) == 0) {
cout << n / ((1 << i) - 1) << "\n";
return;
}
}
} int main() {
int t; cin >> t;
while (t--) solve();
}

B - Balanced Array

题意

构造一个数组,要求满足:

  • 前一半为偶数,后一半为奇数
  • 所有数两两不同且都为正整数
  • 前一半的数之和与后一半的数之和相等

思路

贪心,从 2 起构造前一半,从 1 起构造后一半,后一半最后一个元素特别构造。

代码

#include <bits/stdc++.h>
using namespace std; void solve() {
int n; cin >> n;
n /= 2;
if (n & 1) cout << "NO" << "\n";
else {
cout << "YES" << "\n";
for (int i = 1; i <= n; i++) {
cout << 2 * i << ' ';
}
for (int i = 1; i <= n - 1; i++) {
cout << 2 * i - 1 << ' ';
}
cout << 3 * n - 1 << "\n";
}
} int main() {
int t; cin >> t;
while (t--) solve();
}

C - Alternating Subsequence

题意

在一个数组中寻找一个正负相间的最长的子序列,且该子序列元素之和为该长度所有子序列的最大值。

思路

长度优先——贪心地取所有不同正负的元素

最大和——同正负的元素利用双指针局部排序,每次取最大值

代码

#include <bits/stdc++.h>
using ll = long long;
using namespace std; void solve() {
int n; cin >> n;
ll a[n]; for (ll &i : a) cin >> i;
ll ans = 0;
for (int i = 0; i < n; ) {
int j = i + 1;
while (j < n and a[i] * a[j] > 0) ++j;
sort(a + i, a + j);
ans += a[j - 1];
i = j;
}
cout << ans << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}

D - Constant Palindrome Sum

题意

对一个长度为偶数,元素大小在 [1, k] 之间的数组元素做变换,每次可以用 [1, k] 间的一个数替换一个元素,要求最终满足,所有的 a[i] + a[n - 1 - i] 相等 (0-indexed),求最少需要的变换次数。

思路

a[i] + a[n - 1 - i] 的范围为 [2, 2 * k],我们可以假设每个和一开始都需要变换所有元素,然后计算每个对称和所能变换的最小值和最大值,在此区间内的和只需变换两个元素中的一个,为了在线性时间内访问所有需要减一的区间,可以用一个差分数组记录每个需要变换区间的左右端点,最后对于初始时的和因为两个子元素都不用变换所以需要再减一。

代码

#include <bits/stdc++.h>
using namespace std; const int M = 4e5 + 100;
int n, k;
int a[M], cnt[M], sub[M]; void solve() {
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 2; i <= 2 * k; i++) cnt[i] = n, sub[i] = 0;
for (int i = 0; i < n / 2; i++) {
int x = a[i], y = a[n - 1 - i];
int mi = min(x, y) + 1;
int mx = max(x, y) + k;
--cnt[x + y];
--sub[mi];
++sub[mx + 1];
}
for (int i = 2; i <= 2 * k; i++) {
sub[i] += sub[i - 1];
cnt[i] += sub[i];
}
int ans = INT_MAX;
for (int i = 2; i <= 2 * k; i++)
ans = min(ans, cnt[i]);
cout << ans << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}

Codeforces Round #636 (Div. 3)的更多相关文章

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

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

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

  3. Codeforces Round #368 (Div. 2)

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

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

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

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

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

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

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

  8. Codeforces Round #371 (Div. 1)

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

  9. Codeforces Round #268 (Div. 2) ABCD

    CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...

随机推荐

  1. Head First 设计模式 —— 11. 组合 (Composite) 模式

    思考题 我们不仅仅要支持多个菜单,升值还要支持菜单中的菜单.你如何处理这个新的设计需求? P355 [提示]在我们的新设计中,真正需要以下三点: P354 我们需要某种属性结构,可以容纳菜单.子菜单和 ...

  2. SpringCloud系列之SpringCloud Stream

    SpringCloud Stream SpringCloud Config SpringCloud Gatewa SpringCloud Hystrix SpringCloud 第一部分 文章目录 S ...

  3. (二)数据源处理1-configparser读取.ini配置文件

    import osimport configparsercurrent_path =os.path.dirname(__file__)#获取config当前文件路径config_file_path = ...

  4. 用percona monitoring plugins 监控mysql

    下载:http://www.percona.com/redir/downloads/percona-monitoring-plugins/1.1.1/percona-zabbix-templates- ...

  5. 攻防世界—pwn—guess_num

    题目分析 checksec检查文件保护机制 这个结果看的我满是问号??? \n ida分析程序 是一个猜数字的游戏,需要全部输入正确才能得到flag 根据大佬的wp得出一下内容 先使用srand()进 ...

  6. Os-hackNos-特权文件提权

    一 信息收集 netdiscover -i eth0 -r 10.10.10.0/24 扫描ip nmap -sP 192.168.43.0/24 扫描开放的端口 使用"-sP"选 ...

  7. django使用缓存之drf-extensions

    使用方法:1.直接添加装饰器@cache_response该装饰器装饰的方法有两个要求: 它必须是继承了rest_framework.views.APIView的类的方法 它必须返回rest_fram ...

  8. Hadoop2.7.7阿里云安装部署

    阿里云的网络环境不需要我们配置,如果是在自己电脑上的虚拟机,虚拟机的安装步骤可以百度.这里是单机版的安装(也有集群模式的介绍)使用Xshell连接阿里云主机,用命令将自己下载好的安装包上传到服务器 # ...

  9. LSM(Log Structured Merge Trees ) 笔记

    目录 一.大幅度制约存储介质吞吐量的原因 二.传统数据库的实现机制 三.LSM Tree的历史由来 四.提高写吞吐量的思路 4.1 一种方式是数据来后,直接顺序落盘 4.2 另一种方式,是保证落盘的数 ...

  10. css-前端实现左中右三栏布局的常用方法:绝对定位,圣杯,双飞翼,flex,table-cell,网格布局等

    1.前言 作为一个前端开发人员,工作学习中经常会遇到快速构建网页布局的情况,这篇我整理了一下我知道的一些方法.我也是第一次总结,包括圣杯布局,双飞翼布局,table-cell布局都是第一次听说,可能会 ...