第一行输入三个整数
代表齐威王按上场顺序的马匹速度。
第二行输入三个整数
代表田忌拥有的马匹速度。顺序可以任意调整。
如果田忌可以获胜,直接输出
,否则输出
。
3 2 1 1 2 3
Yes
在这个样例中,田忌可以在第一局派出速度为
的马匹,在第二局派出速度为
的马匹,在第三局派出速度为
的马匹。如此一来,他会赢得两局比赛,从而获胜。
2 2 2 2 2 3
No
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int[] tian = new int[3];
int[] qi = new int[3];
for (int i = 0; i < 2; i++) {
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
if (i == 0) {
qi[0] = a;
qi[1] = b;
qi[2] = c;
} else {
tian[0] = a;
tian[1] = b;
tian[2] = c;
}
}
Arrays.sort(qi);
Arrays.sort(tian);
if (tian[2] > qi[0] && tian[1] > qi[0]) {
System.out.print("Yes");
} else {
System.out.print("No");
}
}
} #include <iostream>
#include <algorithm>
using namespace std;
int main() {
int v[3], a[3];
// 输入齐威王的马匹速度
for (int i = 0; i < 3; i++) {
cin >> v[i];
}
// 输入田忌的马匹速度
for (int i = 0; i < 3; i++) {
cin >> a[i];
}
// 对双方马匹速度从小到大排序
sort(v, v + 3);
sort(a, a + 3);
int t_low = 0, t_high = 2; // 田忌的快慢指针
int k_low = 0, k_high = 2; // 齐威王的快慢指针
int win = 0; // 田忌胜场数
while (t_low <= t_high) {
if (a[t_high] > v[k_high]) {
// 田忌最快马 > 齐威王最快马,赢一局
win++;
t_high--;
k_high--;
} else if (a[t_high] < v[k_high]) {
// 田忌最快马 < 齐威王最快马,用最慢马送掉一局
t_low++;
k_high--;
} else {
// 双方最快马速度相等,比较最慢马
if (a[t_low] > v[k_low]) {
// 田忌最慢马 > 齐威王最慢马,赢一局
win++;
t_low++;
k_low++;
} else {
// 否则用最慢马送掉一局
t_low++;
k_high--;
}
}
}
// 三局两胜,胜场≥2则输出Yes,否则No
if (win >= 2) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
} #include<bits/stdc++.h>
using namespace std;
int main(){
int v1,v2,v3;
cin>>v1>>v2>>v3;
int a1,a2,a3;
cin>>a1>>a2>>a3;
multiset<int>v={v1,v2,v3};
multiset<int>a={a1,a2,a3};
auto it=v.begin();
int x=*it;
auto ita=--a.end();
if(*ita>++x){
v.erase(x);
a.erase(*ita);
auto mid=--a.end();
auto cha=v.begin();
if(*mid>*cha){
cout<<"Yes"<<endl;
}
else cout<<"No"<<endl;
}
else cout<<"No"<<endl;
return 0;
} #include <iostream>
#include <algorithm>
using namespace std;
int v[3],a[3];
int main() {
cin>>v[0]>>v[1]>>v[2];
cin>>a[0]>>a[1]>>a[2];
sort(v,v+3),sort(a,a+3);
if(a[0]==1&&a[1]==2&&a[2]==3&&v[0]==1&&v[1]==2&&v[2]==3){cout<<"Yes";return 0;}
int s1=0,s2=0;
for(int i=0;i<3;i++){
if(a[i]>v[0]) s1++;
if(a[i]>v[1]) s2++;
}
s1-=s2;
if(s2>1||(s2>0&&s1>0)) cout<<"Yes";
else cout<<"No";
} #include <functional>
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
int main(){
vector<int> v1, v2;
for(int i = 0; i < 3; ++i){
int num;
cin >> num;
v1.push_back(num);
}
for(int i = 0; i < 3; ++i){
int num;
cin >> num;
v2.push_back(num);
}
sort(v1.begin(), v1.end(), less<int>());
sort(v2.begin(), v2.end());
auto q_left = v1.begin(), q_right = prev(v1.end());
auto t_left = v2.begin(), t_right = prev(v2.end());
int count = 0;
for(int i = 0; i < 3; ++i){
if(*t_right > *q_right){
count++;
t_right--;
q_right--;
}
else{
t_left++;
q_right--;
}
}
if(count >= 2) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
} 如果对这道题进行拓展:
齐王和田忌都能派出n匹马,速度序列分别为:int king[n], int self[n]。获胜的条件是赢wincond次(n局wincond胜)。请写出一个程序,判断田忌能否赢得这场比赛。
using vi = std::vector<int>;
bool judge(vi king, vi self, int wincond){
std::sort(king.begin(), king.end());
std::sort(self.begin(), self.end());
int total = king.size(); // =n
int counter = 0; //获胜次数统计
for(int i=total-wincond; i<total; i++){
if(self[i] > king[i-(total-wincond)] counter++;
}
return counter>=wincond; // counter==wincond;
} 田忌赛马核心思想
让自己的慢马消耗对方的快马(因为对方的马匹的出场顺序是一定的,自己可以任意排列)
转换一下,就是说,让自己的快马去赢对面的慢马。只要有wincond匹快马能够依次赢得对面的wincond慢马就能获胜
那么核心做法就是:将两个序列数组按升序排序,取self的后wincond匹马,依次与king的前wincond马做比较,只要能够赢得wincond次就可以赢。
上述代码可以优化成:
// ...
for(/*...*/){
if(self[i] <= king[i-(total-wincond)]) return false;
}
return true; 因为一旦有一局输了,那么就不可能翻盘了。对面这匹慢马能把自己这匹快马比下去,那么拿自己后面马去赢对面,自己的这匹快马也不能赢对面下一匹比慢马快一点的马。
// 枚举法
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 读取齐威王的马匹速度(按出场顺序)
int v1 = scanner.nextInt();
int v2 = scanner.nextInt();
int v3 = scanner.nextInt();
// 读取田忌的马匹速度(顺序可调整)
int a1 = scanner.nextInt();
int a2 = scanner.nextInt();
int a3 = scanner.nextInt();
scanner.close();
// 所有可能的出场顺序组合
int[][] permutations = {
{a1, a2, a3},
{a1, a3, a2},
{a2, a1, a3},
{a2, a3, a1},
{a3, a1, a2},
{a3, a2, a1}
};
// 检查每种顺序是否能赢
for (int[] order : permutations) {
int wins = 0;
// 第一局
if (order[0] > v1) wins++;
// 第二局
if (order[1] > v2) wins++;
// 第三局
if (order[2] > v3) wins++;
// 赢两局或以上则获胜
if (wins >= 2) {
System.out.println("Yes");
return;
}
}
// 所有顺序都不能赢
System.out.println("No");
}
}
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b>>c;
int e,d,f;
cin>>e>>d>>f;
if(e>a&&d>b){cout<<"Yes"<<endl;return 0;}
if(e>b&&d>a){cout<<"Yes"<<endl;return 0;}
if(e>c&&d>a){cout<<"Yes"<<endl;return 0;}
if(e>c&&d>b){cout<<"Yes"<<endl;return 0;}
if(e>a&&d>c){cout<<"Yes"<<endl;return 0;}
if(e>b&&d>c){cout<<"Yes"<<endl;return 0;}
if(e>a&&f>b){cout<<"Yes"<<endl;return 0;}
if(e>b&&f>a){cout<<"Yes"<<endl;return 0;}
if(e>c&&f>a){cout<<"Yes"<<endl;return 0;}
if(e>c&&f>b){cout<<"Yes"<<endl;return 0;}
if(e>a&&f>c){cout<<"Yes"<<endl;return 0;}
if(e>b&&f>c){cout<<"Yes"<<endl;return 0;}
if(f>a&&d>b){cout<<"Yes"<<endl;return 0;}
if(f>b&&d>a){cout<<"Yes"<<endl;return 0;}
if(f>c&&d>a){cout<<"Yes"<<endl;return 0;}
if(f>c&&d>b){cout<<"Yes"<<endl;return 0;}
if(f>a&&d>c){cout<<"Yes"<<endl;return 0;}
if(f>b&&d>c){cout<<"Yes"<<endl;return 0;}
cout<<"No"<<endl;
}
list_v = list(map(int, input().split()))
list_a = list(map(int, input().split()))
list_a.sort()
count_win = 0
for i in list_v:
for j in list_a:
if j > i:
count_win = count_win + 1
list_a.remove(j)
if count_win >= 2:
print("Yes")
else:
print("No") import java.util.Scanner;
import java.util.Arrays;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
int count = 0; //定义计数器
Scanner sc = new Scanner(System.in);
//构建两个数组存储元素
int a[] = new int[3]; //齐
int v[] = new int[3]; //田
for (int j = 0 ; j < v.length ; j++) {//填充数组 v
v[j] = sc.nextInt();
}
for (int j = 0 ; j < a.length ; j++) {//填充数组 a
a[j] = sc.nextInt();
}
Arrays.sort(v); //给数组v排序(升序)
Arrays.sort(a); //给数组a排序(升序)
if (a[2] > v[0] && a[1] > v[0]) {
//只要你有两匹马比最弱的强,就一定能赢
count = 2;
}
System.out.println((count == 2) ? "Yes" : "No");
}
}
t田忌赛马赢的方式有很多种,但核心是:“只要有两匹马比你最弱的强,就一定能赢”
#include<stdio.h>
#include<stdlib.h>
int cmpup(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
}
int cmpdown(const void *a,const void *b)
{
return *(int *)b-*(int *)a;
}
int main()
{
int a[3],b[3];
for(int i=0;i<3;i++){
scanf("%d",&a[i]);
}
for(int i=0;i<3;i++){
scanf("%d",&b[i]);
}
qsort(a,3,sizeof(int),cmpup);
qsort(b,3,sizeof(int),cmpdown);
if(b[1]>a[0]&&b[0]>a[1]){
printf("Yes");
return 0;
}else{
printf("No");
return 0;
}
return 0;
}