1007 Olympics sorting

Source: Internet
Author: User

Question 1007: Time Limit for Olympics sorting: 1 second
Memory limit: 32 MB
Special question: No
Submit: 2972
Solution: 618

Description:
Rank countries as required.

Input:
There are multiple groups of data.
The first line gives the country N, the number of countries in the ranking M, the country number from 0 to N-1.
The number of Olympic gold medals, medals, and population (millions) in the N rows starting from the second row ).
The next line is the M country number.
Output:
There are four sorting methods: Total number of gold medals, total number of gold medals, proportion of population of gold medals, proportion of population of gold medals
Best ranking method and final ranking for each country
Format: ranking: Ranking Method
If there is the same final ranking, the rank with the smallest ranking method will be output. For the ranking method, the total number of gold medals <total number of medals <proportion of gold medal population <proportion of medal population
If there is a parallel ranking, that is, if the total number of gold medals is, 90, 90, 80, the ranking is 1, 2, 4.
Add an empty row to each group of data.
Sample input:
4
4 8 1
6 6 2
4 8 2
2 12 4
0 1 2 3
4 2
8 10 1
8 11 2
8 12 3
8 13 4
0 3 sample output:
1: 3
1:1
2:1

1:1
1:1 [cpp]
 

//************************************** * ************************ // If the same final ranking exists, the rank with the smallest ranking method is output,
// For the ranking method, the total number of gold medals <total number of medals <proportion of gold medal population <proportion of medal population
// Meaning that all countries are sorted by four methods, where n (1, 2, 3, 4) rank the same, which of the following is a small Ranking Method for output from a small scale?
// Note: The last sorted country number entered is not necessarily ordered.
// Train of thought: sort the countries to be sorted by four methods, calculate the ranking in each mode, and output four sorting modes for each country.
// The maximum ranking in the mode (1> 2> 3……> N), if there is the same ranking, the rank with the smallest ranking method will be output.
# Include <stdio. h>
# Include <stdlib. h>
# Include <math. h>
Const int MAX_SIZE = 1000;
Struct Country {
Int iCountryId; // The Country Number entered cannot be sorted by subscript.
Int dGold; // The total number of gold medals.
Int dPrize; // The total number of medals.
Int dPopulation; // population
Double dGoldRatio; // gold medal population ratio
Double dPrizeRatio; // medal population ratio
} Countrys [MAX_SIZE], RankCountrys [MAX_SIZE];

Struct Rank {
Int iCountryId;
Int iRank;
} Ranks [4] [MAX_SIZE]; // ranking in four ranking Modes

Int cmp1 (const void * a, const void * B );
Int cmp2 (const void * a, const void * B );
Int cmp3 (const void * a, const void * B );
Int cmp4 (const void * a, const void * B );

Int main ()
{
Int n, m, I, j, k, Id, RankCountryId [MAX_SIZE]; // The country number that requires ranking
While (scanf ("% d", & n, & m )! = EOF ){
For (I = 0; I <n; I ++ ){
Scanf ("% d", & Countrys [I]. dGold, & Countrys [I]. dPrize, & Countrys [I]. dPopulation );
Countrys [I]. dGoldRatio = (double) Countrys [I]. dGold/Countrys [I]. dPopulation;
Countrys [I]. dPrizeRatio = (double) Countrys [I]. dPrize/Countrys [I]. dPopulation;
Countrys [I]. iCountryId = I;
}
For (I = 0; I <m; I ++) {// filter countries to be sorted
Scanf ("% d", & Id );
RankCountryId [I] = Id; // write down the country number to be sorted
RankCountrys [I] = Countrys [Id];
}

// Sort by gold medal
Qsort (RankCountrys, m, sizeof (Country), cmp1 );
Ranks [0] [0]. iCountryId = RankCountrys [0]. iCountryId;
Ranks [0] [0]. iRank = 1;
For (I = 1; I <m; I ++ ){
Ranks [0] [I]. iCountryId = RankCountrys [I]. iCountryId;
If (RankCountrys [I-1]. dGold = RankCountrys [I]. dGold)
Ranks [0] [I]. iRank = Ranks [0] [I-1]. iRank; // rank
Else
Ranks [0] [I]. iRank = I + 1; // actual ranking
}

// Sort by medals
Qsort (RankCountrys, m, sizeof (Country), cmp2 );
Ranks [1] [0]. iCountryId = RankCountrys [0]. iCountryId;
Ranks [1] [0]. iRank = 1;
For (I = 1; I <m; I ++ ){
Ranks [1] [I]. iCountryId = RankCountrys [I]. iCountryId;
If (RankCountrys [I-1]. dPrize = RankCountrys [I]. dPrize)
Ranks [1] [I]. iRank = Ranks [1] [I-1]. iRank;
Else
Ranks [1] [I]. iRank = I + 1;
}

// Sort by gold medal population ratio
Qsort (RankCountrys, m, sizeof (Country), cmp3 );
Ranks [2] [0]. iCountryId = RankCountrys [0]. iCountryId;
Ranks [2] [0]. iRank = 1;
For (I = 1; I <m; I ++ ){
Ranks [2] [I]. iCountryId = RankCountrys [I]. iCountryId;
If (RankCountrys [I]. dGoldRatio = RankCountrys [I-1]. dGoldRatio)
Ranks [2] [I]. iRank = Ranks [2] [I-1]. iRank;
Else
Ranks [2] [I]. iRank = I + 1;
}

// Sort by medal population ratio
Qsort (RankCountrys, m, sizeof (Country), cmp4 );
Ranks [3] [0]. iCountryId = RankCountrys [0]. iCountryId;
Ranks [3] [0]. iRank = 1;
For (I = 1; I <m; I ++ ){
Ranks [3] [I]. iCountryId = RankCountrys [I]. iCountryId;
If (RankCountrys [I]. dPrizeRatio = RankCountrys [I-1]. dPrizeRatio)
Ranks [3] [I]. iRank = Ranks [3] [I-1]. iRank;
Else
Ranks [3] [I]. iRank = I + 1;
}

// Find the optimal sorting method. The output must traverse each element in each ranking. Otherwise, it will be missed.
Int rank, way;
Bool first;
For (I = 0; I <m; I ++ ){
First = true;
For (j = 0; j <4; j ++)
For (k = 0; k <m; k ++ ){
If (Ranks [j] [k]. iCountryId = RankCountryId [I]) {
If (first ){
Rank = Ranks [j] [k]. iRank;
Way = j;
First = false;
} Else {
If (Ranks [j] [k]. iRank <rank ){
Rank = Ranks [j] [k]. iRank;
Way = j;
}
}
}
}
Printf ("% d: % d \ n", rank, way + 1 );
}
Printf ("\ n ");
}
Return 0;
}
// Sort by gold card quantity in descending order
Int cmp1 (const void * a, const void * B)
{
Country * c1 = (Country *);
Country * c2 = (Country *) B;
Return c2-> dGold-c1-> dGold;
}
// Sort by the number of prizes
Int cmp2 (const void * a, const void * B)
{
Country * c1 = (Country *);
Country * c2 = (Country *) B;
Return c2-> dPrize-c1-> dPrize;
}
// Sort by gold medal population ratio
Int cmp3 (const void * a, const void * B)
{
Country * c1 = (Country *);
Country * c2 = (Country *) B;
Return c2-> dGoldRatio> c1-> dGoldRatio? 1:-1;
}
// Sort by medal population ratio
Int cmp4 (const void * a, const void * B)
{
Country * c1 = (Country *);
Country * c2 = (Country *) B;
Return c2-> dPrizeRatio> c1-> dPrizeRatio? 1:-1;
}
/*************************************** ***********************
Problem: 1007
User: windzhu
Language: C ++
Result: Accepted
MS Time: 10
Memory: 1108 kb
**************************************** ************************/

 

Related Article

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.