Nyoj 364--Tian Bogey horse racing —————— "greedy"

Source: Internet
Author: User
Tags rounds

Tian Bogey race time limit:MS | Memory limit:65535 KB Difficulty:3
Describe
Here is a famous stories in Chinese.

"That's about 2300 years ago." General Tian Ji is a high official in the country Qi. He likes to play horse racing with the king and others. "

"Both of Tian and the king had three horses in different classes, namely, regular, plus, and super. The rule is to has three rounds in a match; Each of the horses must is used in one round. The winner of a single round takes and hundred silver dollars from the loser. "

"The Being the most powerful man in the country, the king had so nice horses, and each class he horse is better than Tian ' S. As a result, each time the king takes six hundred silver dollars from Tian. "

"Tian Ji is not happy on that, until he met Sun Bin, one of the most famous generals in Chinese history. Using a little trick due to Sun, Tian Ji brought home of hundred silver dollars and such a grace in the next match. "

"It is a rather simple trick. Using the He regular class horse race against the Super class from the king, they would certainly lose that round. But then he plus beat the King ' s regular, and his super beat the King's Plus. What's a simple trick. And how does your think of Tian Ji, the high ranked official in China? "

Were Tian Ji lives in nowadays, he'll certainly laugh at himself. Even more, were he sitting in the ACM contest right now, he could discover that the horse racing problem can be simply Viewe D as finding the maximum matching in a bipartite graph. Draw Tian ' s horses on one side, and the king's horses on the other. Whenever one of Tian ' s horses can beat one from the king, we draw a edge between them, meaning we wish to establish this Pair. Then, the problem of winning as many rounds as possible are just to find the maximum matching in this graph. If There is ties, the problem becomes more complicated, he needs to assign weights 0, 1, or-1 to all the possible edges, And find a maximum weighted perfect matching ...

However, the horse racing problem is a very special case of bipartite matching. The graph is decided by the speed of the horses---a vertex of higher speed always beat a vertex of lower speed. In this case, the weighted bipartite matching algorithm are a too advanced tool to deal with the problem.

In this problem, you is asked to the write a program to solve this special case of matching problem.
Input
The
input consists of many test cases. Each case starts with a positive an integer n (n <=) on the first line, which are the number of horses on each side. The next n integers on the second line is the speeds of Tian ' s horses. Then the next n integers in the third line is the speeds of the King ' s horses.
Output
For each input case, output a line containing a single number, which are the maximum money Tian Ji'll get, in Silver doll Ars.

Sample input
392 83 7195 87 74220 2020 20220 1922 18
Sample output
20000


The main topic: to Tian Bogey N horse, to the King N Horse, lose a round to reduce 200 yuan, win a round increase 200 yuan, draw not rewards and punishments. Ask how much money you can get at the end.


Problem-solving ideas: If the slow horse can win the King, win. If it is slower than the king, pull the King's fastest ponies, anyway lose, rather than lose more valuable, for behind the horse to reduce resistance. If the slow horse as fast as the first to see Tian Bogey's fastest horse is not faster than the king's fastest horse, if it is the fastest horse to win a game. If not, let Tian bogey's slowest horse compare with the King's fastest horse is not equal, if not, then lose a game; if so, let the slowest horse and the King draw the fastest horse.

#include <bits/stdc++.h>using namespace Std;int tj[1100],king[1100];int main () {int t,i,j,k,tmp,sum,cnt,n,m,    Tslow,tfast,kslow,kfast;        while (scanf ("%d", &n)!=eof) {memset (tj,0,sizeof (TJ));        memset (King,0,sizeof (king));        for (i=0;i<n;i++) {scanf ("%d", &tj[i]);        } for (i=0;i<n;i++) {scanf ("%d", &king[i]);        } sort (tj,tj+n);        Sort (king,king+n);        tslow=kslow=0;        tfast=kfast=n-1;        m=k=0;                while (m<n) {if (Tj[tslow]>king[kslow]) {//Tian Bogey slow ponies king slow horse fast tslow++;                kslow++;            k++;                }else if (Tj[tslow]<king[kslow]) {//Tian Bogey slow ponies king slow horse slow tslow++;                kfast--;            k--;                    }else{//Two people slow horse same speed if (Tj[tfast]>king[kfast]) {//Tian bogey pony faster than the King fast tfast--;                    kfast--;                k++; }else {//Tian bogey horse is slower than or equal to the King's Horse if (Tj[tsloW]<king[kfast] {//Tian Bogey slow ponies king horse slow kfast--;                        tslow++;                    k--;                        }else if (Tj[tslow]==king[kfast]) {//Tian bogey slow horse equals King's Pony tslow++;                    kfast--;        }}} m++;    } cout<<k*200<<endl; } return 0;} /*58 6 5 1 3 9 7 6 4 2 * *

  




Nyoj 364--Tian Bogey horse racing —————— "greedy"

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.