Wednesday, September 1, 2010

poj_1723_SOLDIERS.cpp

Problem Links:

poj1723,

Problem:

SOLDIERS
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 4758
Accepted: 1761
Description
N soldiers of the land Gridland are randomly scattered around the country.
A position in Gridland is given by a pair (x,y) of integer coordinates. Soldiers can move - in one move, one soldier can go one unit up, down, left or right (hence, he can change either his x or his y coordinate by 1 or -1).
The soldiers want to get into a horizontal line next to each other (so that their final positions are (x,y), (x+1,y), ..., (x+N-1,y), for some x and y). Integers x and y, as well as the final order of soldiers along the horizontal line is arbitrary.

The goal is to minimise the total number of moves of all the soldiers that takes them into such configuration.

Two or more soldiers must never occupy the same position at the same time.
Input
The first line of the input contains the integer N, 1 <= N <= 10000, the number of soldiers.
The following N lines of the input contain initial positions of the soldiers : for each i, 1 <= i <= N, the (i+1)st line of the input file contains a pair of integers x[i] and y[i] separated by a single blank character, representing the coordinates of the ith soldier, -10000 <= x[i],y[i] <= 10000.
Output
The first and the only line of the output should contain the minimum total number of moves that takes the soldiers into a horizontal line next to each other.
Sample Input
5
1 2
2 2
1 3
3 -2
3 3
Sample Output
8
Source
CEOI 1998

Solution:

Consider the x and y as independent, and then sort.
PS: 1st, take care of the second sort for x, that’s very necessary to get a smaller number of moves.

Source Code:

//Wed Jun  2 16:19:38 EDT 2010
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

int main(int argc, const char* argv[])
{
//  freopen("input.in", "r", stdin);
//  freopen("output.out", "w", stdout);
    int N;
    while (cin >> N)
    {
        vector<int> x(N);
        vector<int> y(N);
        int sum = 0;
        for (int i = 0; i < N; i++)
        {
            cin >> x[i] >> y[i];
        }
        sort(x.begin(), x.end());
        sort(y.begin(), y.end());
        for (int i = 0; i < N; i++)
        {
            x[i] -= i;
        }
        sort(x.begin(), x.end());
        int M = N / 2;
        for (int i = 0; i < M; i++)
        {
            sum += y[N - 1 - i] - y[i];
            sum += x[N - 1 - i] - x[i];
        }
        cout << sum << endl;
    }
//  fclose(stdin);
//  fclose(stdout);
    return 0;
}

No comments :