📄 toj_2881.cpp
字号:
/*2881. Biased Standings Time Limit: 3.0 Seconds Memory Limit: 65536KTotal Runs: 424 Accepted Runs: 157 Multiple test filesUsually, results of competitions are based on the scores of participants. However, we are planning a change for the next year of IPSC. During the registration each team will be able to enter a single positive integer - their preferred place in the ranklist. We would take all these preferences into account, and at the end of the competition we will simply announce a ranklist that would please all of you.But wait... How would that ranklist look like if it won't be possible to satisfy all the requests?Suppose that we already have a ranklist. For each team, compute the distance between their preferred place and their place in the ranklist. The sum of these distances will be called the badness of this ranklist.Problem specificationGiven team names and their preferred placements find one ranklist with the minimal possible badness.InputThe first line of the input file contains an integer T specifying the number of test cases. Each test case is preceded by a blank line.Each test case looks as follows: The first line contains N - the number of teams participating in the competition. Each of the next N lines contains a team name (a string of letters and numbers) and its preferred place (an integer between 1 and N, inclusive). No two team names will be equal.You can assume that N ≤ 100,000 and the length of the team name is no more than 20.OutputFor each of the test cases output a single line with a single integer - the badness of the best ranklist for the given teams.Sample Input207noobz 1llamas 2Winn3rz 25thwheel 1NotoricCoders 5StrangeCase 7WhoKnows 73ThreeHeadedMonkey 1MoscowSUx13 1NeedForSuccess 1Sample Output53Hint: In the first test case, one possible ranklist with the minimal badness is:1. noobz2. llamas3. Winn3rz4. 5thwheel5. NotoricCoders6. WhoKnows7. StrangeCaseIn the second test case all ranklists are equally good.Source: Internet Problem Solving Contest*/#include<cstdio>#include<cstdlib>int const MAX = 100010;int comparePosition( void const *a , void const *b ){ int *arg1 = ( int * ) a; int *arg2 = ( int * ) b; if ( *arg1 > *arg2 ) return 1; else if ( *arg1 < *arg2 ) return -1; else return 0;}/*void printState( int badness , int j , int exPosition[] , int position[] , int positionTop , int stack[] , int top ){ int k; printf( "badness= %d exposition[ %d ] = %d positionTop = %d\n" , badness , j , exPosition[ j ] , positionTop ); printf( "stack " ); for ( k = 0; k <= top; k++) { printf( "%d " , stack[ k ] ); } printf( " " ); for ( k = 1; k < positionTop; k++ ) { printf( "%d " , position[ k ] ); } putchar( '\n' );}*/int main(){ int exPosition[ MAX ] , stack[ MAX ]; int nOfCase , nOfTeam , positionTop , i , j , k , top; long long badness; scanf( "%d" , &nOfCase ); for ( i = 0; i < nOfCase; i++ ) { scanf( "%d" , &nOfTeam ); for ( j = 0 ; j < nOfTeam; j++ ) { scanf( "%*s%d" , &exPosition[ j ] ); } qsort( exPosition , nOfTeam , sizeof( int ) , comparePosition ); for ( badness = 0 , positionTop = 1 , top = -1 , j = 0; j < nOfTeam; j++ ) { if ( exPosition[ j ] == positionTop ) { positionTop++; } else if ( exPosition[ j ] < positionTop ) { top++; stack[ top ] = exPosition[ j ]; } else if ( exPosition[ j ] > positionTop ) { while ( top >= 0 ) { badness += abs( positionTop - stack[ top ] ); positionTop++; top--; } badness += abs( exPosition[ j ] - positionTop ); positionTop++; } } while ( top >= 0 ) { badness += abs( positionTop - stack[ top ] ); positionTop++; top--; } printf( "%lld\n" , badness ); } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -