【模拟】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个点中个某个点。并且就在正中中间的位置。如果正中间有两个数,那么这两个数之间的任何值都是最优的,输出最左的。

 1 //
 2 //by coolxxx
 3 //#include<bits/stdc++.h>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<string>
 7 #include<iomanip>
 8 #include<map>
 9 #include<memory.h>
10 #include<time.h>
11 #include<stdio.h>
12 #include<stdlib.h>
13 #include<string.h>
14 //#include<stdbool.h>
15 #include<math.h>
16 #define min(a,b) ((a)<(b)?(a):(b))
17 #define max(a,b) ((a)>(b)?(a):(b))
18 #define abs(a) ((a)>0?(a):(-(a)))
19 #define lowbit(a) (a&(-a))
20 #define sqr(a) ((a)*(a))
21 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
22 #define mem(a,b) memset(a,b,sizeof(a))
23 #define eps (1e-8)
24 #define J 10
25 #define mod 1000000007
26 #define MAX 0x7f7f7f7f
27 #define PI 3.14159265358979323
28 #define N 300004
29 using namespace std;
30 typedef long long LL;
31 int cas,cass;
32 int n,m,lll,ans;
33 int a[N];
34 int main()
35 {
36     #ifndef ONLINE_JUDGE
37 //    freopen("1.txt","r",stdin);
38 //    freopen("2.txt","w",stdout);
39     #endif
40     int i,j,k;
41 //    for(scanf("%d",&cas);cas;cas--)
42 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
43 //    while(~scanf("%s",s+1))
44     while(~scanf("%d",&n))
45     {
46         for(i=1;i<=n;i++)
47         {
48             scanf("%d",&a[i]);
49         }
50         sort(a+1,a+1+n);
51         printf("%d\n",a[(n+1)/2]);
52     }
53     return 0;
54 }
55 /*
56 //
57
58 //
59 */

时间: 2024-12-21 23:00:31

【模拟】Codeforces 710B Optimal Point on a Line的相关文章

CodeForces 710B Optimal Point on a Line (数学,求中位数)

题意:给定n个坐标,问你所有点离哪个近距离和最短. 析:中位数啊,很明显. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring>

codeforces 710B B. Optimal Point on a Line(数学)

题目链接: B. Optimal Point on a Line 题意: 给出n个点,问找出一个点使得这个点到所有的点的距离和最小; 思路: 所有点排序后的中位数;这是一个结论; AC代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h> #in

模拟 Codeforces Round #297 (Div. 2) A. Vitaliy and Pie

题目传送门 1 /* 2 模拟:这就是一道模拟水题,看到标签是贪心,还以为错了呢 3 题目倒是很长:) 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <iostream> 8 #include <algorithm> 9 #include <cstring> 10 using namespace std; 11 12 const int MAXN = 2e5 + 10; 13

queue+模拟 Codeforces Round #304 (Div. 2) C. Soldier and Cards

题目传送门 1 /* 2 题意:两堆牌,每次拿出上面的牌做比较,大的一方收走两张牌,直到一方没有牌 3 queue容器:模拟上述过程,当次数达到最大值时判断为-1 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 #include <string> 10 #include <stack> 11 #in

Codeforces 685C Optimal Point (二分、不同类型距离的相互转换)

题目链接 https://codeforces.com/contest/685/problem/C 题解 我怎么又还差最后一步的时候放弃了然后往别的方向上想了一小时才发现这个思路能做-- 首先二分答案,转化为所有点半径为\(mid\)的曼哈顿距离区域内是否有交. 考虑二维问题,显然可以用曼哈顿转切比雪夫来做,把\((x,y)\)变成\((x+y,x-y)\)那么原来两点的曼哈顿距离就等于后来两点的切比雪夫距离.考虑一下这个做法的证明: \[|x_1-x_2|+|y_1-y_2|=\max(x_1

codeforces 622. Optimal Number Permutation 构造

题目链接 假设始终可以找到一种状态使得值为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

CodeForces 622D Optimal Number Permutation

是一个简单构造题. 请观察公式: 绝对值里面的就是 |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最后放空缺的两个位置,剩下的从小到大一个一个放,奇数放左边构造,偶数

Educational Codeforces Round 16

A. King Moves water.= =. 1 #include <cstdio> 2 int dx[] = {0,0,1,1,1,-1,-1,-1}; 3 int dy[] = {1,-1,0,1,-1,0,1,-1,0}; 4 #define judge(x,y) x >= 1 && x <= 8 && y >= 1 && y <= 8 5 int main() 6 { 7 char t; 8 int x, y,

Educational Codeforces Round 16 B

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