BestCoder Round #58 (div.2)

Card Game

 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)

問題描述
Soda和Beta是好朋友. 今天他們準備要玩一個遊戲. Soda有nn張牌, 牌面上數字分別爲a_1, a_2, ..., a_na1,a2,...,an. Beta也有nn張牌, 牌面上數字分別爲b_1, b_2, ..., b_nb1,b2,...,bn.

一開始, 他們選擇了一個小於等於nn的數字mm. 然後他們分別從自己的nn張牌中隨機選擇了mm張卡. mm張卡的和大的那個人贏. Soda想要知道他是否能夠必贏, 無論選出來的mm張牌是什麼.
輸入描述
輸入有多組數據. 第一行有一個整數TT, 表示測試數據組數. 然後對於每組數據:

第一行有兩個整數 nnmm (1 \le m \le n \le 500)(1mn500). 第2行有nn個整數a_1, a_2, ..., a_na1,a2,...,an (1 \le a_i \le 1000)(1ai1000)表示Soda的牌. 第3行有nn個整數b_1, b_2, ..., b_nb1,b2,...,bn (1 \le b_i \le 1000)(1bi1000)表示Beta的牌.
輸出描述
對於每組數據, 如果Soda必贏輸出"YES", 否則輸出"NO".
輸入樣例
2
3 1
4 5 6
1 2 3
5 2
3 4 7 8 9
3 4 5 2 3
輸出樣例
YES
NO

水題:一個按從大到小,一個按從小到大排序,然後各自累加前m個數即可,注意平局情況,平局不等於贏

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN = 505;
int Soda[MAXN], Beta[MAXN];
bool cmp1(int a, int b)
{
    return a < b;
}
bool cmp2(int a, int b)
{
    return a > b;
}
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, m,sumS=0,sumB=0;
        cin >> n >> m;
        for (int i = 0; i < n; i++)
        {
            cin >> Soda[i];
        }
        for (int i = 0; i < n; i++)
        {
            cin >> Beta[i];
        }
        sort(Soda, Soda + n, cmp1);
        sort(Beta, Beta + n,cmp2);
        for (int i = 0; i < m; i++)
        {
            sumS += Soda[i];
            sumB += Beta[i];
        }
        if (sumS > sumB)
        {
            cout << "YES" << endl;
        }
        else
        {
            cout << "NO" << endl;
        }
    }
    return 0;
}

LCS

 Time Limit: 6000/3000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)

問題描述
你有兩個序列\{a_1,a_2,...,a_n\}{a1,a2,...,an}\{b_1,b_2,...,b_n\}{b1,b2,...,bn}. 他們都是11nn的一個排列. 你需要找到另一個排列\{p_1,p_2,...,p_n\}{p1,p2,...,pn}, 使得序列\{a_{p_1},a_{p_2},...,a_{p_n}\}{ap1,ap2,...,apn}\{b_{p_1},b_{p_2},...,b_{p_n}\}{bp1,bp2,...,bpn}的最長公共子序列的長度最大.
輸入描述
輸入有多組數據, 第一行有一個整數TT表示測試數據的組數. 對於每組數據:

第一行包含一個整數n (1 \le n \le 10^5)n(1n105), 表示排列的長度. 第2行包含nn個整數a_1,a_2,...,a_na1,a2,...,an. 第3行包含nn個整數 b_1,b_2,...,b_nb1,b2,...,bn.

數據中所有nn的和不超過2 \times 10^62×106.
輸出描述
對於每組數據, 輸出LCS的長度.
輸入樣例
2
3
1 2 3
3 2 1
6
1 5 3 2 6 4
3 6 2 4 5 1
輸出樣例
2
4
一開始以爲真的是LCS,一直暴力找序列,發現肯定會TLE,只要固定一個找這個數字在另一個序列中出現的位置,然後再將數字固定爲該的位置的本序列的數,重複操作即可。相當於找環。
主要注意2個位置相同的情況,即子環,用cin會超3000MS,但是用scanf只需用1100+MS.

#include<iostream>
#include<cstring>
#include<stdio.h>
using namespace std;
const int MAXN = 100005;
int a[MAXN],vis[MAXN],map[MAXN];
int main()
{
	int t, n;
	scanf("%d", &t);
	while (t--)
	{
		memset(vis, 0, sizeof(vis));
		scanf("%d", &n);
		for (int i = 0; i < n; i++)
		{
			scanf("%d", &a[i]);
		}
		for (int i = 0; i < n; i++)
		{
			scanf("%d", &map[a[i]]);
		}
		int ans = 0, p , q ;
		for (int i = 1; i <= n; i++)
		{
			if (!vis[i])
			{
				int len = 0;
				p = i;
				q = map[i];
				vis[p] = 1;
				while (!vis[q])
				{
					p = q;
					q = map[p];
					vis[p] = 1;
					len++;
				}
				if (!len)
				{
					ans++;
				}
				else
				{
					ans += len;
				}
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}

</pre><pre name="code" class="cpp">



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章