D题(贪心)

D - D

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

Here is a famous story in Chinese history.

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

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

"Being the most powerful man in the country, the king has so nice horses that in each class his horse is better than Tian‘s. As a result, each time the king takes six hundred silver dollars from Tian."

"Tian Ji was not happy about 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 two hundred silver dollars and such a grace in the next match."

"It was a rather simple trick. Using his regular class horse race against the super class from the king, they will certainly lose that round. But then his plus beat the king‘s regular, and his super beat the king‘s plus. What a simple trick. And how do you think of Tian Ji, the high ranked official in China?"

Were Tian Ji lives in nowadays, he will certainly laugh at himself. Even more, were he sitting in the ACM contest right now, he may discover that the horse racing problem can be simply viewed 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 an edge between them, meaning we wish to establish this pair. Then, the problem of winning as many rounds as possible is just to find the maximum matching in this graph. If there are 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 is a too advanced tool to deal with the problem.

In this problem, you are asked to write a program to solve this special case of matching problem.

Input

The input consists of up to 50 test cases. Each case starts with a positive integer n (n <= 1000) on the first line, which is the number of horses on each side. The next n integers on the second line are the speeds of Tian’s horses. Then the next n integers on the third line are the speeds of the king’s horses. The input ends with a line that has a single 0 after the last test case.

Output

For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.

Sample Input

3

92 83 71

95 87 74

2

20 20

20 20

2

20 19

22 18

0

Sample Output

200

0

0

题解:田忌赛马

三种情况

1.当田忌最快的马比齐王最快的马快时,用田忌最快的  马赢齐王最快的马

2.当田忌最快的马比齐王最快的马慢时,用田忌最慢的  马输给齐王最快的马

3.当田忌最快的马跟齐王最快的马一样快时,分情况讨  论

a.当田忌最慢的马比齐王慢快的马快时,用田忌最慢的马赢齐王最慢的马比

b.当田忌最慢的马比齐王最慢的马慢时,用田忌最慢的马和齐王最快的马比

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int t;
int total,co;
int a[1005],b[1005];
bool cmp(int a1,int a2)
{
    return a1>a2;
}
int main()
{
    while(cin>>t&&t)
    {
        for(int i=0; i<t; i++)
            cin>>a[i];
        for(int i=0; i<t; i++)
            cin>>b[i];
        sort(a,a+t,cmp);
        sort(b,b+t,cmp);
        total=0;
        co=0;
        int p=0,q=0;
        int x=t-1,y=t-1;
        for(int i=0; i<t; i++)
        {
            co=-1;
            if(a[p]>b[q])
            {
                co=1;
                p++;
                q++;
            }
            else
            {
                if(a[x]>b[y])
                {
                    co=1;
                    x--;
                    y--;
                }
                else
                {
                    if(a[x]==b[q]) co=0;
                    else
                        co=-1;
                        x--;
                        q++;
                }
            }
            total+=co;
        }
        cout<<total*200<<endl;
    }
    return 0;
}

时间: 2024-10-04 20:50:15

D题(贪心)的相关文章

CodeForces 719B Anatoly and Cockroaches (水题贪心)

题意:给定一个序列,让你用最少的操作把它变成交替的,操作有两种,任意交换两种,再就是把一种变成另一种. 析:贪心,策略是分别从br开始和rb开始然后取最优,先交换,交换是最优的,不行再变色. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <c

Codeforces Round #320 (Div. 2) &quot;Or&quot; Game(好题,贪心/位运算/前缀后缀或)

1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 using namespace std; 6 typedef long long ll; 7 /* 8 n个数,你最多有k次操作,每次操作可以选择一个数乘以x,问所有数或(|)的最大值 9 贪心思路:选一个数进行k此乘以x操作; 因为x>=2 10 111 ---> 1111 11

Random Maze (hdu 4067 最小费用流 好题 贪心思想建图)

Random Maze Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1373    Accepted Submission(s): 514 Problem Description In the game "A Chinese Ghost Story", there are many random mazes which

51nod 1344 走格子(水题+贪心)

1344 走格子 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 取消关注 有编号1-n的n个格子,机器人从1号格子顺序向后走,一直走到n号格子,并需要从n号格子走出去.机器人有一个初始能量,每个格子对应一个整数A[i],表示这个格子的能量值.如果A[i] > 0,机器人走到这个格子能够获取A[i]个能量,如果A[i] < 0,走到这个格子需要消耗相应的能量,如果机器人的能量 < 0,就无法继续前进了.问机器人最少需要有多少初始能量,才能完成

[思路题+贪心] fzu oj 2197 最小花费

题意: 给一个01串,相邻的01交换代价为X,否则为Y. 问把全部1变到0前面的最小费用. 思路: 对于 01011->11100 如果只靠相邻位移动是需要5步的. 然而不管怎么移动,步数是固定的. 那我们就把最前面的0和最后面的1交换 假设0在i,1在j.我们交换的代价就是min(y,x*(j-i)) 然后累加求和就好了! 很棒的脑洞题! 代码: #include"cstdlib" #include"cstdio" #include"cstring

集训第四周(高效算法设计)G题 (贪心)

G - 贪心 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description Simon and Garfunkel Corporation (SG Corp.) is a large steel-making company with thousand of customers. Keeping the customer satisfied is one of th

ZZNU 我只看看不写题 贪心?dp?不懂

G : 我只看看不写题 Progress Bar 时间限制:1 Sec 内存限制:128 MiB 提交:221 答案正确:59 题目描述 伴随着科技的发展,我们的生活也越来越多姿多彩,随着手机的普及,各种交友软件也在快速的发展. 小a是个老实人,当然只是自己理解而已,其实小a是个不折不扣的渣男.因为他在有女朋友的同时,还在疯狂的撒网,利用各种交友软件寻求更适合自己的伴侣. 小a女朋友当然不是省油的灯,自然了解小a的本性,所以在每次见面时就会翻看小a的软件记录,来了解小a近期的状况,机智的小a当然

【构造题 贪心】cf1041E. Tree Reconstruction

比赛时候还是太慢了……要是能做快点就能上分了 Monocarp has drawn a tree (an undirected connected acyclic graph) and then has given each vertex an index. All indices are distinct numbers from 11 to nn. For every edge ee of this tree, Monocarp has written two numbers: the ma

CF 1008B Turn the Rectangles(水题+贪心)

There are n rectangles in a row. You can either turn each rectangle by 90 degrees or leave it as it is. If you turn a rectangle, its width will be height, and its height will be width. Notice that you can turn any number of rectangles, you also can t

HDU 2037 今年暑假不AC(贪心,区间更新,板子题)

今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 55813    Accepted Submission(s): 30009 Problem Description “今年暑假不AC?” “是的.” “那你干什么呢?” “看世界杯呀,笨蛋!” “@#$%^&*%...”确实如此,世界杯来了,球迷的节日也来了,估计很多AC