"Greedy topic" HDU 1052 Tian Ji--The Horse Racing (Tian Bogey horse racing)

Source: Internet
Author: User

Link: click here~~

Test instructions

Tian Bogey and King each have n horse, judge how to match, so that Tian bogey net wins the most.

Have no intention to see strong elder brother write the solution (very early did ~ ~ embarrassed) feeling is very interesting, but at that time forgot to go to a, now in retrospect this problem is classic, meaning still not feel, direct copy solution, thought write very clear,

The basic thing is to look at the idea of knocking

"Problem-solving ideas" regardless of how the game, all to let Tian bogey horse to play the maximum value. Of course, the horse's first priority is to win the game, and to win the most benefit, that is, to win each other behind their own horse.
When he can not complete the task of the time to lose, and pull each other's fastest horse into the water, to the back of his teammates to create a greater chance of victory.
1. If the slowest horse can defeat King's slowest horse, then let it prevail over the slow horse, the victory session plus 1. (Tian bogey slowest horse > King slowest horse)

2. If the slowest horse can not overcome the king the slowest horse, then it can not beat the other horses, then let it lose, and lose to King the fastest, failed to add 1. (Tian bogey slowest horse < King)

3. If the slowest horse is at the same speed as the slowest horse in the king. At this point, it is not easy to think that playing with it is the best situation, on the contrary, playing flat is the next worst, why?

Because of their teammates are likely to overcome this time the other side of the slow horse, so even if they lose a one, teammates can help win back a game, and win a, lose a profit and play flat a profit is the same, and lose when you can pull each other's fastest horse to launch, Create a greater chance of victory for one's fastest horse (because it loses a strong opponent), that is to say, the fastest horse of its own is likely to win again because of their own sacrifice, from this point of view, or deliberately lose the better.
However, there is a point to note, when their own water before, if the fastest horse of their own faster than the other side of the fastest horse, and then also lost to each other the fastest horse, then the fastest horse of their own talent wasted, why?

Very simple, it could have won, need you to water it? - -! In other words, in this case, your own sacrifice is of little value.

Therefore, in the water, it is necessary to ensure that their fastest horse is not the most fast horse. After satisfying this condition, let the most slow horse and the other side to race (possibly draw), so that the field of the horse has been fully utilized.

To summarize, say:
/*
1 Tian bogey slowest horse > King slowest horse
2 Tian bogey slowest horse < King the fastest horses
3 Tian bogey slowest horse = = King slowest horse, with King the fastest pony ratio
4 Make sure your horse is the quickest and the slowest.
*/

Code:

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm>using namespace    std;const int maxn=1005;int tianji[maxn],king[maxn];int n,win,lose;void Solve () {win=lose=0;    int t_slow=0,t_fast=n-1;    int k_slow=0,k_fast=n-1;            while (T_slow<=t_fast) {if (Tianji[t_slow]>king[k_slow]) {win++;            t_slow++;        k_slow++;            } else if (Tianji[t_slow]<king[k_slow]) {lose++;            t_slow++;        k_fast--;                } else {if (Tianji[t_fast]>king[k_fast]) {win++;                t_fast--;            k_fast--;                    } else {if (tianji[t_slow]<king[k_fast]) lose++;                    t_slow++;            k_fast--; }}}}int Main () {while (scanf ("%d", &n), N) {for (int i=0; i<n; i++) scanf ("%d",&Tianji[i]);        for (int i=0; i<n; i++) scanf ("%d", &king[i]);        Sort (tianji,tianji+n);        Sort (king,king+n);        Solve ();    printf ("%d\n", (win-lose) *200); } return 0;}


"Greedy topic" HDU 1052 Tian Ji--The Horse Racing (Tian Bogey horse racing)

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.