2016SDAU課程練習一1002
Problem 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
題意:就是田忌賽馬的萌萌歷史小故事
思路:這個思路貌似從小看故事裡就有吧,就是看馬的好壞,要是田忌最差的馬比王最差的馬差那就用田忌最差的和王最好的比,不然就最差和最差比,田忌贏了加200,輸了減200,求最多金子數,思路比較好想
感想:一開始完全想暴力一點挨個比,後來發現不用,借鑑了下別人的,其實思路差不多,但是我不太會表示
這個題應該分這幾種去討論: 1. 田忌慢馬 > 齊王慢馬 win ++; 2. 田忌慢馬 < 齊王慢馬 lose ++ ,齊王快馬 out; 3. 田忌慢馬 = 齊王慢馬 { if(田忌快馬 > 齊王快馬) win ++ ; else lose ++ 齊王快馬 out;#include <cstdio>
#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<numeric>
#include<math.h>
#include<string.h>
#include<map>
#include<set>
#include<vector>
using namespace std;
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int n,i,j,w,l;
int a[1000];
int b[1000];
int t[1000];
while(cin>>n)
{
if(n==0) break;
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
cin>>b[i];
}
sort(a,a+n,cmp);
sort(b,b+n,cmp);
w=l=0;
int tm,tn,qm,qn;
tm=qm=0;tn=qn=n-1;
while(tm<=tn)
{
if(a[tn]>b[qn])
{
w++;
tn--;
qn--;
}
else if(a[tn]<b[qn])
{
l++;
tn--;
qm++;
}
else
{
if(a[tm]>b[qm])
{
w++;
tm++;
qm++;
}
else
{
if(a[tn]<b[qm])
l++;
tn--;
qm++;
}
}
}
cout<<200*(w-l)<<endl;
}
}
相關推薦
2016SDAU課程練習一1002
Problem C Problem Description Here is a famous story in Chinese history. "That was about 2300 years ago. General Tian Ji was a high offi
2016SDAU課程練習一1005 Problem F
Problem F Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 100 Accepted Submissio
2016SDAU程式設計練習二1002
Strange fuction Problem Description Now, here is a fuction:<br> F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <
2016SDAU課程練習三1001
題目題意: 最長上升子序列問題。 Sample Input 2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5 Sample Output Case 1: 14 1 4 Case 2: 7 1 6 解題思路: 以每個數作為最後一個數考慮,
C課程練習-單詞長度-多一種思考
#include <stdio.h> int main() { char s[100]=""; //The way you get the string decides if you can choose traversal operators. // while you hav
詞法分析器——哈工大編譯原理課程(一)
mina == 原理 技術分享 after 文件 編碼 exe warn 詞法分析器——哈工大編譯原理課程(一) 程序輸入:從code.txt文件中讀取內容 程序輸出:識別出的單詞序列,格式為:(種別碼,屬性值) ①對於關鍵字
python 練習一
python##題目鏈接:https://github.com/Yixiaohan/show-me-the-code#1.備份文件[[email protected]/* */ ~]# cat backup_file.py import time import os source=[‘/root
JavaSE(八)之集合練習一
can lec set 打印 定義 hello blog 要求 sys 前面把Collection家族給學習完畢了,接下來我們通過幾個練習來鞏固前面的知識。 一、產生10個1-20之間的隨機數要求隨機數不能重復 import java.util.HashSe
java課程作業一,傳入字符串求和
++ cli length logs public dos 技術 course eclips 設計思想:在命令行下以字符串形式輸入幾個數字,使用Double類的靜態方法valueof傳入字符串得到相應的數值型變量,一個for即可將所有的字符串轉化為數值型相加。 程序流程圖:
python練習(一)
val lte imp sts else 練習 filter [0 pen 1. 實現1-100的所有的和 def qiuhe(x, y): return x + y he = 0for i in xrange(1, 101): he =qiuhe(he,i)p
Python 正則練習(一) 爬取國內代理ip
取代 替代 use -a int 5.0 tdi col 則表達式 簡單的正則表達式練習,爬取代理 ip。 僅爬取前三頁,用正則匹配過濾出 ip 地址和 端口,分別作為key、value 存入 validip 字典。 如果要確定代理 ip 是否真的可用,還需要再對代理
MonogoDB 練習一
測試 處理 sse pod you rdf gho first 獲取 1.解析文件,僅處理 FIELDS 字典中作為鍵的字段,並返回清理後的值字典列表 需求: 1.根據 FIELDS 字典中的映射更改字典的鍵 2.刪掉“rdf-schema#label”中的小括號裏
Django學習筆記第五篇--實戰練習一--查詢數據庫並操作cookie
settings sin -h update out backend uitable -s ror 一、啟動項目: 1 django-admin start mysite1 2 cd mysite1 3 python manage.py startapp loginapp
【機器學習】谷歌的速成課程(一)
label spa dev 分類 ram 做出 org ron 表示 問題構建 (Framing) 什麽是(監督式)機器學習?簡單來說,它的定義如下: 機器學習系統通過學習如何組合輸入信息來對從未見過的數據做出有用的預測。 標簽 在簡單線性回歸中,標簽是我們要預測
函數式編程(練習一)
ack com cit 模糊查詢 alex opera 信息 影響 手機 作業 現要求你寫一個簡單的員工信息增刪改查程序,需求如下: 當然此表你在文件存儲時可以這樣表示 1,Alex Li,22,13651054608,IT,2013-04-01 2,Jack Wang,
Python小程序練習一之登陸接口
AD lse aps 錯誤 用戶名 添加 admin 賬戶 AS 登陸接口並實現猜數字遊戲 輸入用戶名密碼 認證成功後顯示歡迎信息 進行猜數字遊戲,可猜三次 輸錯三次後鎖定 1、Adduser.py 1 # The author is tou
MySQL數據庫語法-多表查詢練習一
轉載 AR author img 工資 class mage HA eid MySQL數據庫語法-多表查詢練習一 作者:尹正傑 版權聲明:原創作品,謝絕轉載!否則將追
python基礎練習一
you and led python != TE pass ... while循環 1、使用while循環輸入 1 2 3 4 5 6 8 9 10 i=1 while(i<11): if(i!=7): print(i,end=‘ ‘)
shell腳本練習一
壓縮 one shell腳本練習 nor .sh strong 分支語句 amp 整數和 if多分支語句練習#!/bin/bashread -p "請輸入100米賽跑秒數:" iif [ $i -lt 10 ] && [ $i -gt 0
C++編程基礎一 28-編程練習一
dex turn 運算 控制臺應用程序 number 數字 代碼 amp 報告 1 // 28-編程練習一.cpp: 定義控制臺應用程序的入口點。 2 // 3 4 #include "stdafx.h" 5 #include <iostrea