CodeForces 710B Optimal Point on a Line(首场完美被虐,安安睡吧+总结)



B. Optimal Point on a Line
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 line contains n integers xi ( - 109 ≤ xi ≤ 109) — the coordinates of the given n points.

Output

Print the only integer x — the position of the optimal point on the line. If there are several optimal points print the position of the leftmost one. It is guaranteed that the answer is always the integer.

Example
input
4
1 2 3 4
output
2



题意:

很简单的题意,给定一些坐标在数轴上,问给所有的点距离数轴的哪个点的距离之和最小。

2016年8月23日11:36:53:

求的是距离给定的点的最小和!


思路:

大哭大哭大哭在我的Wrong中找亮点!!

不玩了,睡觉。注册账号半年了,第一次玩CF,刚开始不会提交,语言选错了。

好不容易测过了第二个测试数据,卡在了第三个。还有分吗?????


总结:

2016年8月23日11:36:28

数学专场吗?挺有意思的,下午就是数学场积分赛了,无论27号我还在不在ACM协会,我认为自己都有了对算法的兴趣!潜心默行,慢慢努力。

说说比赛,昨天晚上22点场(不用熬夜的),兴致勃勃的一看是数学,就在那里敲啊敲的!

一直卡壳,刚才2016年8月23日11:35:05突然发觉,数组的起始是 0,但是自己的结论是数字标号是 1 开始的,

慢慢来吧,慢慢的积累经验。

AC Code:

2016年8月23日11:32:17

#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
typedef __int64 LL;
const int MYDD=1103+3e5;

int a[MYDD];

int main() {
	int n;
	scanf("%d",&n);
	for(int j=1; j<=n; j++)
		scanf("%d",&a[j]);
	sort(a+1,a+n+1);
	int ans=a[(n+1)/2];
	printf("%d\n",ans);
	return 0;
}



Wrong Code1:

在坐标轴上找点

#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
typedef __int64 LL;
const int MYDD=1103+3e5;

int a[MYDD];

int main() {
	int n;
	scanf("%d",&n);
	for(int j=1; j<=n; j++)
		scanf("%d",&a[j]);
	sort(a+1,a+n+1);
	int ans,v1,v2;
	LL ans1=0,ans2=0;
	if(n%2) ans=a[(n+1)/2];/*奇数*/
	else    {/*偶数*/
		ans=(a[n/2]+a[n/2+1])/2;
	}
	printf("%d\n",ans);
	return 0;
}
/*
6
0 4 13 25 37 46
25 91
13 91
*/

Wrong Code2:

在给定的坐标找点

#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
typedef __int64 LL;
const int MYDD=1103+3e5;

int a[MYDD];

int main() {
	int n;
//	while(1) {
		scanf("%d",&n);
		for(int j=0; j<n; j++)
			scanf("%d",&a[j]);
		sort(a,a+n);
		int ans,v1,v2;
		LL ans1=0,ans2=0;
		if(n%2) ans=a[(n+1)/2];/*����*/
		else    {/*ż��*/
			if(n>1) {
				v1=a[n/2-1];
				v2=a[n/2];
				for(int j=0; j<n; j++) {
					ans1=ans1+abs(a[j]-v1);
					ans2=ans2+abs(a[j]-v2);
				}
				if(ans1<=ans2)   ans=v1;
				else            ans=v2;
			} else
				ans=a[n/2];
		}
		printf("%d\n",ans);
//	}
	return 0;
}
/*
6
0 4 13 25 37 46
25 91
13 91
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值