BestCoder Round #58(Div.2) (4/4) (Div.1) (3/4)

maksyuki 发表于 比赛 分类,标签:
0

A Card Game

Problem Description

Soda and Beta are good friends. They are going to play a card game today. Soda has n cards with number a1,a2,...,an while Beta has n cards with number b1,b2,...,bn.

First, they choose a number m no larger than n. Then they both randomly select m cards from their own n cards. The one with larger sum of the selected cards will win. Soda wants to know if he can always win no mater what cards will be randomly selected from him and Beta.

Input

There are multiple test cases. The first line of input contains an integer T(1≤T≤100), indicating the number of test cases. For each test case:

The first line contains two integer n and m (1≤m≤n≤500). The second line contains n integers a1,a2,...,an (1≤ai≤1000) denoting Soda's cards. The third line contains n integers b1,b2,...,bn (1≤bi≤1000) denoting Beta's cards.

Output

For each test case, output "YES" (without the quotes) if Soda can always win, otherwise output "NO" (without the quotes) in a single line.

Sample Input

2
3 1
4 5 6
1 2 3
5 2
3 4 7 8 9
3 4 5 2 3

Sample Output

YES

NO

Source

BestCoder Round #58 (div.2)

 

题目类型:贪心

算法分析:只要判断Soda最小的m个数大于Beta最大的m个数即可

 

 

B LCS

Problem Description

You are given two sequence {a1,a2,...,an} and {b1,b2,...,bn}. Both sequences are permutation of {1,2,...,n}. You are going to find another permutation {p1,p2,...,pn} such that the length of LCS (longest common subsequence) of {ap1,ap2,...,apn} and {bp1,bp2,...,bpn} is maximum.

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains an integer n(1≤n≤105) - the length of the permutation. The second line contains n integers a1,a2,...,an. The third line contains nintegers b1,b2,...,bn.

The sum of n in the test cases will not exceed 2×106.

Output

For each test case, output the maximum length of LCS.

Sample Input

2
3
1 2 3
3 2 1
6
1 5 3 2 6 4
3 6 2 4 5 1

Sample Output

2

4

Source

BestCoder Round #58 (div.2)

 

题目类型:置换群

算法分析:由于两个序列都是1~n的一个排列。且操作是对对应下标的两个序列的元素同时移动来构成一个最大的LCS。一个贪心策略是将所有处在一个置换中的元素连续排列,这样才能保证最后的LCS是最大的

 

C Beauty of Sequence

Problem Description

Sequence is beautiful and the beauty of an integer sequence is defined as follows: removes all but the first element from every consecutive group of equivalent elements of the sequence (i.e. unique function in C++ STL) and the summation of rest integers is the beauty of the sequence.

Now you are given a sequence A of n integers {a1,a2,...,an}. You need find the summation of the beauty of all the sub-sequence of A. As the answer may be very large, print it modulo 109+7.

Note: In mathematics, a sub-sequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. For example {1,3,2} is a sub-sequence of {1,4,3,5,2,1}.

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains an integer n (1≤n≤105), indicating the size of the sequence. The following line contains n integers a1,a2,...,an, denoting the sequence (1≤ai≤109).

The sum of values n for all the test cases does not exceed 2000000.

Output

For each test case, print the answer modulo 109+7 in a single line.

Sample Input

3
5
1 2 3 4 5
4
1 2 1 3
5
3 3 2 1 2

Sample Output

240

54

144

Source

BestCoder Round #58 (div.2)

 

题目类型:组合计数

算法分析:找每个元素对序列和的贡献,此时先将每个元素看作是n个不同元素,则此时所有子序列的元素和就是2 ^ (n - 1) * Sigma(ai),然后再减去由于重复元素出现导致多算的部分和。对于当前元素a[i]来说,重复元素的和包含在a[i]之后的所有元素的组合*a[i]之前元素的组合,若出现多个相同元素的和,则可以维护一个前缀和num[i],表示出现在元素i之前所有的i在对应元素i之前的元素的组合总数。这里通过使用离散化,用num[a[i]]简化操作,这个地方也可以使用map

 

D Inversion

Problem Description

You have a sequence {a1,a2,...,an} and you can delete a contiguous subsequence of length m. So what is the minimum number of inversions after the deletion.

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains two integers n,m(1≤n≤105,1≤m<n) - the length of the seuqence. The second line contains n integers a1,a2,...,an(1≤ai≤n).

The sum of n in the test cases will not exceed 2×106.

Output

For each test case, output the minimum number of inversions.

Sample Input

2
3 1
1 2 3
4 2
4 1 3 2

Sample Output

0

1

Source

BestCoder Round #58 (div.2)

 

题目类型:求区间逆序对数

算法分析:先计算出m+1~n区间内的逆序对数,然后从后面插入一个元素(范围val[m+1]~val[n])并删除区间的第一个元素,类似于滑动窗口。每个位置都要减去区间第一个元素被删去时对减少逆序对数的贡献,加上新插入的元素对增加逆序对的贡献,并更新结果即可,注意这里使用线段树会MLE!!!结果要使用long long输出!!!