田忌赛马

Problem C

田忌赛马(tian ji racing)

时限:1000ms 内存限制:10000K 总时限:3000ms

描述:

 

田忌与齐王赛马,双方各有n匹马参赛(n<=100),每场比赛赌注为1两黄金,现已知齐王与田忌的每匹马的速度,并且齐王肯定是按马的速度从快到慢出场,现要你写一个程序帮助田忌计算他最好的结果是赢多少两黄金(输用负数表示)。
Tian Ji and the king play horse racing, both sides have n horse (n is no more the 100), every game a bet of 1 gold, now known king and Tian Ji each horse's speed, and the king is definitely on the horse speed from fast to slow, we want you to write a program to help Tian Ji his best result is win the number gold (lost express with the negative number).

输入:

 

多个测例。
每个测例三行:第一行一个整数n,表示双方各有n匹马;第二行n个整数分别表示田忌的n匹马的速度;第三行n个整数分别表示齐王的n匹马的速度。
n=0表示输入结束。
A plurality of test cases.
Each test case of three lines: the first line contains an integer n, said the two sides each have n horse; second lines of N integers n Tian Ji horse speed; third lines of N integers King n horse speed.
N = 0 indicates the end of input.

输出:

 

每行一个整数,田忌最多能赢多少两黄金。
how many gold the tian ji win

输入样例:

 

 

3
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18
3
20 20 10
20 20 10
0

输出样例:

 

 

1
0
0
0

借鉴:https://www.cnblogs.com/anderson0/archive/2011/05/07/2039971.html

#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int a,int b)
{
     return a>b;
}
//平行四边形的处理方法:
/*
    kquick=tquick=0;
    kslow=tslow=n-1;
    类似队列的处理结构
*/
int main()
{
    int i,j,h,n,king[1005],tianji[1005],kquick,kslow,tquick,tslow,total;
    while(cin>>n,n)
    {
       total=0;
      for(i=0;i<n;i++)
        cin>>tianji[i];
      for(i=0;i<n;i++)
        cin>>king[i];
      sort(king,king+n,cmp);
      sort(tianji,tianji+n,cmp);
      kquick=tquick=0;
      kslow=tslow=n-1;
      for(i=0;i<n;i++)
      {
         //当田忌最快的马比国王最快的马快时 用田忌最快的马跟国王最快的马比
         if(tianji[tquick]>king[kquick])
         {
             total+=1;
             kquick++;
             tquick++;
         }
         //当田忌最快的马比国王最快的马慢时 用田忌最慢的马跟国王最快的马比
         else if(tianji[tquick]<king[kquick])
         {
              total-=1;
              tslow--;
              kquick++;
         }
         else//当田忌最快的马比国王最快的马一样快时
         {
             for(j=tslow,h=kslow;j>=tquick;j--,h--)
             {
                 if(tianji[j]>king[h])//当田忌最慢的马比国王最慢的马快时 用田忌最慢的马跟国王最慢的马比
                 {
                     total+=1;
                     tslow--;
                     kslow--;
                 }
                 else     //用田忌最慢的马跟国王最快的马比
                 {
                     if(tianji[j]<king[i])
                     {
                         total-=1;
                         tslow--;
                         kquick++;
                     }
                     tslow=--j;
                     kslow=h;
                     break;
                 }
             }
         }
         if(tquick>tslow)  //因为for里比较的次数没法和n连系上,所以在加个外加条件
         break;
      }
      cout<<total<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值