Getting Wrong Answer !?!

Here's the question :-
Siruseri happens to have some of the best chess players in India and one of the most important events in the Siruseri sports calendar is the Annual Chess Challenge where teams from Siruseri and Navalur compete.
The event is held alternately at Siruseri and Navalur. This year the tournament is to be held at Sirseri and the Secretary of the Siruseri, who is up for re-election this year, would like to ensure a win for Siruseri. Since starting a war or accusing Navalur of possessing weapons of mass destruction is beyond his scope (and he also has a few more brain cells than the average leader of the free world) he decided to do something clever. Draw Fixing!!
To understand what he intended to do, we need to know how the Chess Challege match is organised. Both Siruseri and Navalur are required to send N players. Every player from both teams participates in exactly one game (and so exactly N games are played). The host is expected to draw lots to determine who plays who. Our secretary plans to fix this draw so that the pairings are exactly as he wants them to be.
Each chess player has a ELO rating indicating how good he is. The higher the rating the better the player. The secretary knows the rating of each of the players in the Siruseri and Navalur teams and he would like to pair them up so that the number of pairs in which the Siruseri player has a higher ELO rating is maximised.
For example, suppose that N is 4 and the ratings of the players are as below:
Siruseri Team ELO Rating Navalur Team ELO Rating
Player 1 1873 Player 1 2450
Player 2 2134 Player 2 1860
Player 3 1900 Player 3 1700
Player 4 1600 Player 4 2120
Then the secretary can arrange for Siruseri to have the higher rating in 3 out of 4 games. He can do this, for example, by pairing them as (1,2), (2,4), (3,3) and (4,1), where (i,j) means Player i from Siruseri plays Player j from Navalur. This is the best he can do as no player from Siruseri can beat Player 1 from Navalur.
Your task is help the secretary to find a pairing that maximises the number of pairs with higher rating for Siruseri.
Input format
The first line of the input contains a single integer N, denoting the number of players in each team. The next N lines (lines 2 through N+1) list the ratings of the N players in the Siruseri team and the N following lines (lines N+2 through 2N+1) denote the ratings of the players in the Navalur team.
Output format
The first line of the output is an integer N (0 ≤ N ≤ N) representing the number of pairs where the Siruseri player has a higher rating in the optimal pairing. This should be followed by N lines, each containing an integer ni indicating the player chosen from the Navalur team to play against the ith player from Siruseri.
Test Data
You may assume that 1 ≤ N ≤ 50000. Further you may assume that in 50% of the inputs N ≤ 5000.
Example:
We illustrate the input and output format using the example above.
Sample input
4
1873
2134
1900
1600
2450
1860
1700
2120
Sample output
3
2
4
3
1


Here's my solution :-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <algorithm>
using namespace std;
int main () {
    int i,j,ms=0,n,ohost[50001],oenemy[50001],host[50001],enemy[50001],h=1,e=1,skipped[50001],skipc=0,tmpl[50001],tc=0,temphp,tempep;
    cin>>n;
    //INPUT
    for (i=1;i<=n;i++) {cin>>ohost[i];host[i]=ohost[i];}
    for (i=1;i<=n;i++) {cin>>oenemy[i];enemy[i]=oenemy[i];}
    //SORTING
    for (i=1;i<n;i++) {
        for (j=i+1;j<=n;j++) {
            if (host[i]<host[j]) swap(host[i],host[j]);
            if (enemy[i]<enemy[j]) swap(enemy[i],enemy[j]);
        }
    }
    //PAIRING
    while (h<=n && e<=n) {
          if (host[h]>enemy[e]) {                          //PAIRING FOUND
             tmpl[tc]=e;
             tc++;
             h++;
             e++;
             ms++;
          } else {                                         //SKIP PAIRING
             skipped[skipc]=e;
             e++;
             skipc++;
          }
    }
    //OUTPUTING IN CORRECT ORDER
    for (i=0;i<skipc;i++) tmpl[tc+i]=skipped[i];
    cout<<ms<<endl;
    for (i=1;i<=n;i++) {
        for (j=1;j<=n;j++) if (host[j]==ohost[i]) {temphp=j;break;}
        tempep=enemy[(tmpl[temphp-1])];
        for (j=1;j<=n;j++) if (oenemy[j]==tempep) {cout<<j<<endl;break;}
    }
}


It works correctly with given sample input , but on submitting the programme , it gives me correct answer for only 2 test cases and wrong answer for 3 test cases while gives Time Limit Exceeded for rest 5 test cases . Can anyone please give the fix or improve time limit by editing the code ?
Thanks in advanced
Last edited on
Topic archived. No new replies allowed.