Soldiers

Soldiers
1000(ms)
65535(kb)
864 / 2350

在一个划分成网格的操场上,n个士兵散乱地站在网格点上。网格点由整数坐标(x,y)表示。士兵们可以沿网格边上、下、左、右移动一步,但在同一时刻任一网格点上只能有一名士兵。按照军官的命令,士兵们要整齐地列成一个水平队列,即排列成(x,y),(x+1,y),…,(x+n-1,y)。如何选择x 和y的值才能使士兵们以最少的总移动步数排成一列。计算使所有士兵排成一行需要的最少移动步数。

输入

第1 行是士兵数n,1< =n< =10000。接下来n 行是士兵的位置,每行2个整数x和y,-10000< =x,y< =10000。

输出

第1 行中的数是士兵排成一行需要的最少移动步数。

样例输入

5
1 2
2 2
1 3
3 -2
3 3

样例输出

8

#include<iostream> 
#include<stdio.h> 
#include<malloc.h>
#include<string.h>
#include<string>
#include<stack>
#include<math.h>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;

//在保证数量最多的情况下这个长度最长 
int main() {
	
	int x[10005],y[10005];
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>x[i]>>y[i];
	}
	sort(x,x+n);
	sort(y,y+n);
	int midY=y[(n+1)/2-1];
	for(int i=0;i<n;i++)
		x[i]-=i;
	sort(x,x+n);
	int midX=x[(n+1)/2-1];
	int total=0;
	for(int i=0;i<n;i++){
		total+=fabs(x[i]-midX)+fabs(y[i]-midY);
	} 
	cout<<total<<endl;
	return 0;
}


阅读更多
换一批

Sky Soldiers

11-22

Problem DescriptionnAn airplane carried k soldiers with parachute is plan to let these soldiers jump off the plane along a straight air route. The landing point of these soldiers is undetermined because of various weather conditions. However, the statisticians of the army can analysis the probability of landing in a certain place through landing history records. To make it simple, the statistician suggests that these sky soldiers will land on finite discrete points of a straight line.nnThis mission plans to place m provisions for the soldiers on the line for landing. These soldiers will be informed the direction of the nearest provision point by a special device after landing, and then walk to the point. The manager of this mission is asking you for help: to determine m points for provisions so that the expected sum of walking distance should be minimized. You can put provisions on any point of the landing line.n nnInputnThere are multiple test cases. For each case, the first line contains two integers k and m (1 ≤ k ≤ 1,000, 1 ≤ m ≤ 50), which represent the number of sky soldiers and the number of positions to place provisions separately.nnThe following k lines contain descriptions of landing parameters for the soldiers numbered from 1 to k. Each description consists of an integer L followed by L pairs of (x, p), which indicates that the probability of the soldier's landing on integer coordination x is p. It is guaranteed that all the p values are positive real numbers, and the sum of p in a single line is exactly 1. The same x may appear more than once on the same line which you should simply add up all the probability p of the pairs with equal x.nThe number of places on which all the soldiers could land is no more than 1000 and it can not be less than m.nThe input ends with k=m=0.n nnOutputnFor each test case, output a line containing only one real number which indicates the minimum expected sum of distance these soldiers will move and should be rounded to two digits after the decimal point.n nnSample Inputn2 1n2 0 0.5 1 0.5n2 1 0.1 3 0.9n0 0n nnSample Outputn2.30

没有更多推荐了,返回首页