“科林明伦杯”哈尔滨理工大学第十届程序设计竞赛(同步赛) 赛马 贪心

链接:https://ac.nowcoder.com/acm/contest/5758/E
来源:牛客网

一天小明与他同学准备赛马,他们每人有n匹马,每匹马有一个固定的战力值,战力值高的马会战胜战力值低的马并赢得比赛。每匹马只能出场比赛一次。小明偷看到了他对手每匹马的出场顺序,小明在更改自己马出场顺序后最多能赢多少场比赛。

输入描述:

输入t,代表有t组数据。每组数据输入正整数n,每人的马匹数量。下一行输入n个值a[i],代表小明每匹马的战力值。接下来一行输入n个值b[i],代表对手按顺序出场的每匹马的战力值。(t<=10, n<1000,1<=i<=n,a[i]<1e6,b[i]<1e6)

输出描述:

小明在更改马匹出场顺序后,最多能赢的场数。

示例1
输入
复制

1
3
5 8 8
4 7 10

输出
复制

2


考虑贪心,每次对决都必须要尽可能赢,并且要用所有能赢的马里能力最小的去赢
排个序

  • 能赢吐掉两个队头
  • 不能赢吐掉自己,换下一只上
#define debug
#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>

#define MAXN ((int)1e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)

using namespace std;

#define show(x...) \ do { \ cout << "\033[31;1m " << #x << " -> "; \ err(x); \ } while (0)

void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

namespace FastIO{

	char print_f[105];
	void read() {}
	void print() { putchar('\n'); }

	template <typename T, typename... T2>
		inline void read(T &x, T2 &... oth) {
			x = 0;
			char ch = getchar();
			ll f = 1;
			while (!isdigit(ch)) {
				if (ch == '-') f *= -1; 
				ch = getchar();
			}
			while (isdigit(ch)) {
				x = x * 10 + ch - 48;
				ch = getchar();
			}
			x *= f;
			read(oth...);
		}
	template <typename T, typename... T2>
		inline void print(T x, T2... oth) {
			ll p3=-1;
			if(x<0) putchar('-'), x=-x;
			do{
				print_f[++p3] = x%10 + 48;
			} while(x/=10);
			while(p3>=0) putchar(print_f[p3--]);
			putchar(' ');
			print(oth...);
		}
} // namespace FastIO
using FastIO::print;
using FastIO::read;

int n, m, Q, K, A[MAXN], B[MAXN];


int main() {
#ifdef debug
	freopen("test", "r", stdin);
	clock_t stime = clock();
#endif
	read(Q);
	while(Q--) {
		read(n);
		for(int i=1; i<=n; i++) read(B[i]);
		for(int i=1; i<=n; i++) read(A[i]);
		sort(A+1, A+1+n);
		sort(B+1, B+1+n);
		int pa = 1, pb = 1, ans = 0;

		//从能赢的马中选最弱的去赢
		//排个序,
		// + 能赢就把两个的队头吐掉 (赢ans++)
		// + 不能赢就吐掉自己的队头换下一只上
		while(pa <= n && pb <= n) {
			if(B[pb] > A[pa]) {
				ans ++;
				pa ++, pb ++;
			} else {
				pb ++;
			}
		}
		printf("%d\n", ans);
	}





#ifdef debug
	clock_t etime = clock();
	printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
	return 0;
}




全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务