题目大意

给定 $n$($n$ 是偶数,$2\le n\le 2\times 10^{5}$)个非负整数 $a_1,\dots, a_n$($a_i\le 10^9$)。
要求将其中 $n/2$ 个数变成平方数,另外 $n/2$ 个数变成非平方数,变化后的数必须仍是非负整数。
将 $x$ 变成 $x'$ 的代价为 $|x-x'|$ 。
求最小的总代价。

比赛时我的思路

用 $c_{i,0}$ 表示将 $a_i$ 变成平方数的最小代价,$c_{i,1}$ 表示将 $a_i$ 变成非平方数的最小代价,不难求出这两个值。
那么问题转化为

从 $c_{1,0}, c_{2,0}, \dots, c_{n,0}$ 和 $c_{1,1}, c_{2,1}, \dots, c_{n,1}$ 中各取出 $n/2$ 个数,限制条件是:$c_{i,0}$ 和 $c_{i,1}$ 不能都取。
求取出的数的最小和。

比赛时我想到这里就进展不下去了。

转化后的问题的解法

对于任意一个合法的取数方案,如果不是最优解,则必然存在 $i, j$ 满足:

  1. $c_{i,0}, c_{j,1}$ 在所取的数中,且
  2. $c_{i,0} + c_{j,1} > c_{i,1} + c_{j,0}$ 。

第二个条件可化为
$c_{i,1} - c_{i,0} < c_{j,1} - c_{j,0}$

这样便得出这个问题的解法:

将下标 $1, 2,\dots,i,\dots, n$ 按 $c_{i,1} - c_{i,0} $ 从小到大排序,对前 $n/2$ 个下标取 $c_{i,1}$,对后 $n/2$ 个下标取 $c_{i,0}$ 。

题解上解法

统计输入的 $n$ 个数中平方数和非平方数的个数,分别记做 $c_0, c_1$。
若 $c_0 = c_1$ 则无需改变。
若 $c_0 > c_1$ 则需要把 $(c_0 - c_1)/2$ 个平方数变成非平方数。对于非零的平方数,加 1 即可,零则须加 2 。所以,优先改变非零的平方数。
若 $c_0 < c_1$ 则需把 $(c_0 - c_1)/2 $ 个非平方数变成平方数。

Codeforces 898E Squares and not squares的更多相关文章

  1. Codeforces Round #451 (Div. 2) [ D. Alarm Clock ] [ E. Squares and not squares ] [ F. Restoring the Expression ]

    PROBLEM D. Alarm Clock 题 OvO http://codeforces.com/contest/898/problem/D codeforces 898d 解 从前往后枚举,放进 ...

  2. 【27.40%】【codeforces 599D】Spongebob and Squares

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. Codeforces 599D:Spongebob and Squares

    D. Spongebob and Squares time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  4. Codeforces 314 E. Sereja and Squares

    http://codeforces.com/contest/314/problem/E 题意: 原本有一个合法的括号序列 擦去了所有的右括号和部分左括号 问有多少种填括号的方式使他仍然是合法的括号序列 ...

  5. Marching squares &amp; Marching cubes

    提要 Marching squares 主要是用于从一个地图(用二维数组表示)生成轮廓的算法.Marching cubes则相应的是在空间生成网格的方法.最常见的应用就是天气预报中气压图的生成.还经常 ...

  6. Codeforces Round #451 (Div. 2) A B C D E

    Codeforces Round #451 (Div. 2) A Rounding 题目链接: http://codeforces.com/contest/898/problem/A 思路: 小于等于 ...

  7. Codeforces Gym 100650D Queens, Knights and Pawns 暴力

    Problem D: Queens, Knights and PawnsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu ...

  8. Codeforces 193A. Cutting Figure

    看起来非常神,但仅仅有三种情况 -1 , 1 ,2..... A. Cutting Figure time limit per test 2 seconds memory limit per test ...

  9. Codeforces Round #451 & Codeforces Round #452

    Rounding Solution Proper Nutrition 枚举 Solution Phone Numbers 模拟 Solution Alarm Clock 贪心,好像不用线段树也可以,事 ...

随机推荐

  1. event loop、进程和线程、任务队列

    本文原链接:https://cloud.tencent.com/developer/article/1106531 https://cloud.tencent.com/developer/articl ...

  2. 跑edgebox

    这是edge的作者的代码:https://github.com/pdollar/edges 这是matlab写的,还需要装Matlab Image Processing Toolbox和Piotr's ...

  3. vector 下标操作

    比如:vector<int> ivec(3).. 当采用下标操作ivec[10]的时候,该操作是未定义的,在自己的机器上输出的值是零.建议使用迭代器进行操作.

  4. Nginx: ubuntu系统上如何判断是否安装了Nginx?

    问题描述:ubuntu系统上,如何查看是否安装了Nginx? 解决方法:输入命令行:ps -ef | grep nginx master process后面就是Nginx的安装目录. 延伸:1. 如何 ...

  5. java,求1-100之和。

    package study01; public class TestWhile { public static void main(String[] args) { int sum = 0; int ...

  6. Matlab-plot绘图

    plot函数 形式 字符控制 常用的图形标记函数 subplot命令拆分窗口 其他常见命令 三维绘图plot3 mesh和contour命令 plot函数 形式 plot(a,'-s')如果a是实数矩 ...

  7. PostgreSQL学习(1)-- 安装pageinspect extension

    1.源码编译 pageinspect的源码在postgre源码包的contrib目录下,解压postgre源码包后进入对应的目录. [root@localhost pageinspect]# pwd ...

  8. Jquery之 Ajax /json

    前言: Ajax = Asynchronous JavaScript and XML(异步的JavaScript和XML) Ajax不是新的编程语言,而是一种使用现有标准的新方法. Ajax最大的优点 ...

  9. 分享几个简单的技巧让你的 vue.js 代码更优雅

    1. watch 与 computed 的巧妙结合 一个简单的列表页面. 你可能会这么做: created(){ this.fetchData() }, watch: { keyword(){ thi ...

  10. 【mysql】mysql has gone away

    原文 http://www.jb51.net/article/23781.htm MySQL server has gone away 问题的解决方法 投稿:mdxy-dxy 字体:[增加 减小] 类 ...