Number of Median

Source: Internet
Author: User

t1-uva-Mining

oj:http://acm.hust.edu.cn/vjudge/problem/40564

One day Gameboy play Warcraft RPG. One task is to build a base on a small, gold-rich circular island that collects all the gold deposits on the island at the fastest speed. There are N (0<n<1000000) gold deposits on the island, and the amount of mineral deposits in each gold mine is equal. And the island's topography is very flat, so the base can be built anywhere on the island, and the mining speed of each gold mine is only related to the length of the mineral-to-base distance. In order not to let this task too boring, the game designers to the island to a "magic", the rule of the miners on the island only South is Xi Jongdong walk. That means the miners can't walk on the island in an oblique way.

This small island is described in a two-dimensional Cartesian coordinate system.

Your job is to help Gameboy find a place to build a base so that the miners can pick up all the mines at the fastest speed.

There are multiple sets of input data. The first row of each set of data is a positive integer n (0<n<1000000), indicating that there are N gold deposits on the island. In the next n rows, each row has two real numbers x, y, representing the coordinates of one of the gold mines. N=0 indicates the end of the input data.

Each set of input data corresponds to a row of output, outputting two real numbers x, Y (two digits after the decimal point), which is the location coordinates of the build base you find. If the coordinates are not unique, you can select one output.

Feel like using the shortest-circuiting ... Enumeration? In fact, as long as the horizontal vertical line in order to find the median number of lines ... t_t

//excerpt from a blog from Daniel#include <cstdio>#include<iostream>#include<algorithm>using namespacestd;Const intmaxn=1000005;DoubleA[MAXN],B[MAXN];intMain () {intI,n;  while(SCANF ("%d",&N), N) { for(i=0; i<n;i++) scanf ("%LF%LF",&a[i],&B[i]); Sort (A,a+N); Sort (b,b+N); printf ("%.2LF%.2lf\n", a[n/2],b[n/2]); }       return 0; }

t2-uva11300-cent gold coins (after mining the penny? )

The main idea is that n people in a circle, everyone has some gold, each person to the left and right on both sides of a number of gold coins, so that the last person's gold number equal, seek to transfer the minimum value of gold coins (total number of coins can always divide n)

There are multiple sets of data, see here: http://acm.hust.edu.cn/vjudge/problem/33899

First of all, for the next two people (such as 1 and 2), if 1 gave 2 some 2 also gave 1 some, then this must not be optimal, so the coin flow between two adjacent individuals can be converted into one-way, set Xn said X person gave his left xn a gold coin, This value can be positive (negative numbers mean he gets the gold from the person on the left)

For the first person: A1 (original gold coin)-x1+x2=m (average) =>x2=m-a1+x1=x1-c1 (set C1=a1-m), later people are similar

So Xn=x1-c (n-1)

We're asking for the smallest absolute value of all x.

CN can be recursive to get, we only need to obtain the best X1 can

It can be found that when X1 is the median of all C-values is optimal (Tip: absolute value is the distance between the two points on the axis, isn't it ~ ~)

The rest is simple.

1#include <iostream>2#include <algorithm>3#include <cmath>4 using namespacestd;5 Const intmaxn=1000000+Ten;6 intN,M,A[MAXN],C[MAXN];7 intMain ()8 {9     Long Longsum;Ten      while(cin>>N) { Onesum=0; A          for(intI=1; i<=n;i++) cin>>a[i],sum+=A[i]; -m=sum/n; c[0]=0; -          for(intI=1; i<n;i++) c[i]=c[i-1]+a[i]-M; theSort (c,c+N); -         Long Longpos=c[n/2],/*Median number of the obtained*/ans=0; -          for(intI=0; i<n;i++) Ans+=abs (pos-c[i]); -cout<<ans<<Endl; +     } -     return 0; +}

Number of Median

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.