CodeForces 710B Optimal Point on a Line】的更多相关文章

题目链接: http://codeforces.com/problemset/problem/710/B 题目大意: 给N个点的坐标,在X轴上找到最靠左的点使得这个点到N个点距离之和最小. 题目思路: [模拟] 先将N个点坐标排序,找夹在i中间的坐标即为答案.(中间2个数选左边的) 从点坐标是X的点往左移到X+1,代价是X右边的坐标数-左边的坐标数.当X不是给定的坐标时答案是不会变得. 所以最终寻找的这个点一定是N个点中个某个点.并且就在正中中间的位置.如果正中间有两个数,那么这两个数之间的任何…
题意:给定n个坐标,问你所有点离哪个近距离和最短. 析:中位数啊,很明显. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring>…
递推. 先对$a[i]$进行从小到大排序. 然后计算出每个点左边所有点到这个点的距离之和$L[i]$,以及右边每个点到这个点的距离之和$R[i]$. 这两个都可以递推得到. $L\left[ i \right] = L\left[ {i - 1} \right] + \left( {i - 1} \right)×(a\left[ i \right] - a[i - 1])$, $R\left[ i \right] = R\left[ {i + 1} \right] + \left( {n-i}…
题目链接: B. Optimal Point on a Line 题意: 给出n个点,问找出一个点使得这个点到所有的点的距离和最小; 思路: 所有点排序后的中位数;这是一个结论; AC代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h> #in…
[Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line with their coordinates xi. Find the point x so the sum of distances to the given points is minimal. 输入 The first line contains integer n (1 ≤ n ≤ 3·105)…
[Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的货币越优.如有1,2,5,10,20,50,那么我们尽量用面值为1的.如果我们把原始货币换成面值为x的货币,设汇率为d,那么需要的原始货币为dx的倍数.显然dx越小,剩下的钱,即n取模dx会尽量小. 然后就可以枚举换某一种货币的数量,时间复杂度\(O(\frac{n}{d})\) 代码 #inclu…
题意 You are given n points on a line with their coordinates $x_i$. Find the point x so the sum of distances to the given points is minimal. 分析 答案是直觉上显然的:输出数组的中位数即可(对于偶数个数的,则是中间靠左).这样的结果正确的原因也很显然.首先,答案肯定在$[x_{min},x_{max}]$中,那么点无论在哪里,对于$x_i,x_{n-i}$这一对…
Codeforces 题面传送门 & 洛谷题面传送门 分类讨论神题. 首先看到最大值最小,一眼二分答案,于是问题转化为判定性问题,即是否 \(\exists x_0,y_0,z_0\) 满足 \(\forall i,|x_0-x_i|+|y_0-y_i|+|z_0-z_i|\le mid\). 柿子中带个绝对值,不好直接转化.不过注意到对于任意实数 \(x\) 都有 \(x\le |x|,-x\le |x|\),因此 \(|x-y|\le v\) 的充要条件即是 \(x-y\le v,y-x\l…
题目链接 假设始终可以找到一种状态使得值为0, 那么两个1之间需要隔n-2个数, 两个2之间需要隔n-3个数, 两个3之间隔n-4个数. 我们发现两个三可以放到两个1之间, 同理两个5放到两个3之间....这样就构造好了. #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmat…
是一个简单构造题. 请观察公式: 绝对值里面的就是 |di-(n-i)|,即di与(n-i)的差值的绝对值. 事实上,对于任何n,我们都可以构造出来每一个i的di与(n-i)的差值为0. 换句话说,就是这个最小值一定可以构造出来是0. 假设输入是6:那么可以这样构造:1 3 5 5 3 1 2 4 6 4 2 6 假设输入是7:那么可以这样构造:1 3 5 7 5 3 1 2 4 6 6 4 2 7 从上面就能看出怎么构造了,n最后放空缺的两个位置,剩下的从小到大一个一个放,奇数放左边构造,偶数…
首先比较容易想到肯定是前k大的元素,那么我们可以先对其进行sort,如果数值一样返回下标小的(见题意),接下里处理的时候我们发现需要将一个元素下标插入到有序序列并且需要访问第几个元素是什么,那么我们可以离线处理,将所有询问存起来,每次插入一个元素的时候在对其进行查询,那么现在就变成了离线求第k大,那么可以直接上主席数,当然比较懒的做法就直接树状数组维护下标,二分查找答案. // ——By DD_BOND #include<bits/stdc++.h> #define fi first #def…
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定若干个三维空间的点 (xi, yi, zi),求一个坐标都为整数的点 P,使得 P 到这些点的最大曼哈顿距离最小. 原题传送门. @solution@ 显然三分套三分套三分. 看到最大值,把绝对值 |x| 拆成 max(x, -x).接着二分最大距离 d,则 max(...) ≤ d. 因此得到如下不等式组: \[\begin{cases} l_1 \leq…
题意 给定长度为\(n\)的序列\(a\),以及m个询问\(<k,pos>\),每次询问满足下列条件的子序列中第\(pos\)位的值为多少. 子序列长度为\(k\) 序列和是所有长度为\(k\)的子序列中最大的 字典序是所有满足上述两个条件的序列中最小的 解题思路 稍作分析即可得出,将序列按值的大小作为第一关键字(升序),下标作为第二关键字(降序)排序,所得序列的后\(k\)个元素即为询问长度为\(k\)时满足给定条件的序列中的元素,所以答案就是后\(k\)个元素中下标第\(pos\)小的元素…
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file for some programming competition problem. His input is a string consisting of n letters 'a'. He is too lazy to write a generator so he will manually ge…
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic progressions: a1k + b1 and a2l + b2. Find the number of integers x such that L ≤ x ≤ R andx = a1k' + b1 = a2l' + b2, for some integers k', l' ≥ 0. 输入 Th…
C. Pie or die Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/problem/C Description Volodya and Vlad play the following game. There are k pies at the cells of n  ×  m board. Each turn Volodya moves one pie to the neighbo…
A. King Moves water.= =. #include <cstdio> ,,,,,-,-,-}; ,-,,,-,,,-,}; #define judge(x,y) x >= 1 && x <= 8 && y >= 1 && y <= 8 int main() { char t; ; scanf("%c%d", &t, &y); x = t - ; ; i < ; i+…
710A. King Moves 给你图中一点求出它周围有几个可达的点: 除边界之外都是8个,边界处理一下即可: #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<cctype> #include<queue> #include<stack> #include<v…
Description You are given n points on a line with their coordinates xi. Find the point x so the sum of distances to the given points is minimal. Input The first line contains integer n (1 ≤ n ≤ 3·105) — the number of points on the line. The second li…
Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks. There is only one shower and there are multiple students who wish to have a shower in the morning. That's why ev…
Optimal Point on a Line CodeForces - 710B You are given n points on a line with their coordinates xi. Find the point x so the sum of distances to the given points is minimal. Input The first line contains integer n (1 ≤ n ≤ 3·105) — the number of poi…
Shower Line CodeForces - 431B Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks. There is only one shower and there are multiple students who wish to have a shower…
形象解释PID算法 小明接到这样一个任务: 有一个水缸点漏水(而且漏水的速度还不一定固定不变),要求水面高度维持在某个位置,一旦发现水面高度低于要求位置,就要往水缸里加水. 小明接到任务后就一直守在水缸旁边,时间长就觉得无聊,就跑到房里看小说了,每30分钟来检查一次水面高度.水漏得太快,每次小明来检查时,水都快漏完了,离要求的高度相差很远,小明改为每3分钟来检查一次,结果每次来水都没怎么漏,不需要加水,来得太频繁做的是无用功.几次试验后,确定每10分钟来检查一次.这个检查时间就称为采样周期. 开…
The clock shows 11:30 PM. The sports programmers of the institute of maths and computer science have just finished their training. The exhausted students gloomily leave their computers. But there's something that cheers them up: Misha, the kind coach…
[未完待续] A The only king stands on the standard chess board. You are given his position in format "cd", where c is the column from 'a' to 'h' and dis the row from '1' to '8'. Find the number of moves permitted for the king. King moves from the pos…
mk_raidstatus python版本 #!/usr/bin/env python # -*- encoding: utf-8; py-indent-offset: 4 -*- import subprocess, re def cfggen(): # 192.168.48.116 command = ['/opt/raid/cfggen 0 DISPLAY |egrep \'Controller type|Volume ID|Status of volume|RAID level|Siz…
D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You have array a that contains all integers from 1 to n twice. You can arbitrary permute any numbers in a. Let number i be in positions xi, yi (xi < yi) i…
2018-02-25 http://codeforces.com/contest/940/problem/A A. Points on the line time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output We've got no test cases. A big olympiad is coming up. But the pr…
[codeforces 549]G. Happy Line 试题描述 Do you like summer? Residents of Berland do. They especially love eating ice cream in the hot summer. So this summer day a large queue of n Berland residents lined up in front of the ice cream stall. We know that ea…
Happy Line Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 549G Description Do you like summer? Residents of Berland do. They especially love eating ice cream in the hot summer. So this…