csuoj-1722-Race

来源:互联网 发布:js继承面试题详解 编辑:程序博客网 时间:2024/06/08 00:22

Description

Johnson and Bob are crazy motorcycle collecting enthusiasts. In order to prove their own motorcycles is the best, they issued a challenge to each other. Johnson wants to win the challenge.As a good friend of Johnson’s, you need to give advice to Johnson. Johnson and Bob each can select their best n motorcycles. And each motorcycle has a value v. There are n matches in total. Any motorcycle can be chosen optionally to participate in the match(but each motorcycle can only participate once). Each time the motorcycle with high value will win. Johnson gets the order of Bob’s motorcycles in advance. Can you help Johnson arrange the competition order of motorcycles in order to win the most of the matches?

 

Input

First line input an integer T(mean there are T cases)
In each case , first line input an integer n (mean there are n motorcycles) (0<n<=10000)
Next line input n integers (mean the value of Johnson’s n motorcycles)
Next line n integers (mean the value of Bob’s n motorcycles )

Output

Every case output an integer mean the most match can win.

Sample Input

156 4 5 1 38 9 3 4 7

Sample Output

2

HINT


不记得是poj,还是hdoj有一道差不多的题,田忌赛马吧

直接把那个题找了出来然后改动一下就ok

#include <bits/stdc++.h>using namespace std;const int maxn = 10005;int john[maxn], bob[maxn];int n; int main(){    #ifdef LOCAL    freopen("input.txt", "r", stdin);    freopen("output.txt", "w", stdout);    #endif    int T;    scanf("%d", &T);    while (T--){        scanf("%d", &n);        for (int i=0; i<n; ++i)            scanf("%d", john+i);        for (int i=0; i<n; ++i)            scanf("%d", bob+i);        sort(john, john+n);        sort(bob, bob+n);        int john_high=n-1, john_low=0;        int bob_high=n-1, bob_low=0;        int sum = 0;        while (john_low<=john_high && bob_low<=bob_high){            if (john[john_low] > bob[bob_low]){                ++sum;                ++john_low;                ++bob_low;            }            else if (john[john_low] < bob[bob_low]){                ++john_low;                --bob_high;            }            else{                if (john[john_high] > bob[bob_high]){                    ++sum;                    --john_high;                    --bob_high;                }                else if (john[john_high] < bob[bob_high]){                    ++john_low;                    --bob_high;                }                else{                    if (john[john_low] < bob[bob_high]){                        ++john_low;                        --bob_high;                    }                    else if (john[john_low] == bob[bob_high])                        break;                }            }        }        printf("%d\n", sum);    }    return 0;}


0 0
原创粉丝点击