Minimize The Difference

 

HDU 2272 Minimize The Difference(贪心)  

2011-04-07 08:35:24|  分类: ACM HDU |  标签: |字号 订阅

Problem Description
Recently, Kiki are asked to arrange a competition. Before the competition, everyone 

have an expectation of the ranking. To make all the players happy , Kiki wants to 

minimize the sum of all the players’ difference. We define the difference of a player 

as |ai-bi|, ai stands for one’s expected ranking ,and bi stands for one’s final 

ranking.
 

Input
There are several test cases.
Each case begins with an integer n ,(n<= 100)stands for the number of players in this 

competition.
Then follows n lines ,each line contains a string s and an integer m,(the string will 

contains no more than 20 characters and no space in it, 1<=m<=n) stands for the name of 

the player and his expected ranking.
 

Output
The output contains one line for each data set : the minimum sum of differences Kiki 

can get.
 

Sample Input
7
a 1
b 2
c 2
d 1
e 5
g 7
f 7
 
 
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
int n;
struct node
{
    char na[30];
    int r;
}person[200];
int cmp(struct node aa,struct node bb)
{
    if(aa.r==bb.r)
        return strcmp(aa.na,bb.na)<0;
        return aa.r<bb.r;
}
int main()
{
    int sum;
    int a;
    while(cin>>n)
    {
        sum=0;
        for(a=1;a<=n;a++)
        {
            cin>>person[a].na>>person[a].r;
        }
        sort(person+1,person+1+n,cmp);
        for(a=1;a<=n;a++)
            sum+=abs(a-person[a].r);
        cout<<sum<<endl;
    }
 return 0;
}

你可能感兴趣的:(Minimize The Difference)