首页 > 代码库 > 2017-5-1-Train:Codeforces Round #317 [AimFund Thanks-Round] (Div. 2)

2017-5-1-Train:Codeforces Round #317 [AimFund Thanks-Round] (Div. 2)

A. Arrays(思维)

You are given two arrays A and B consisting of integers, sorted in non-decreasing order. Check whether it is possible to choose knumbers in array A and choose m numbers in array B so that any number chosen in the first array is strictly less than any number chosen in the second array.

Input

The first line contains two integers n**A, n**B (1 ≤ n**A, n**B ≤ 105), separated by a space — the sizes of arrays A and B, correspondingly.

The second line contains two integers k and m (1 ≤ k ≤ n**A, 1 ≤ m ≤ n**B), separated by a space.

The third line contains n**A numbers a1, a2, ... anA ( - 109 ≤ a1 ≤ a2 ≤ ... ≤ anA ≤ 109), separated by spaces — elements of array A.

The fourth line contains n**B integers b1, b2, ... bnB ( - 109 ≤ b1 ≤ b2 ≤ ... ≤ bnB ≤ 109), separated by spaces — elements of array B.

Output

Print "YES" (without the quotes), if you can choose k numbers in array A and m numbers in array B so that any number chosen in arrayA was strictly less than any number chosen in array B. Otherwise, print "NO" (without the quotes).

Examples

input

3 32 11 2 33 4 5

output

YES

input

3 33 31 2 33 4 5

output

NO

input

5 23 11 1 1 1 12 2

output

YES

Note

In the first sample test you can, for example, choose numbers 1 and 2 from array A and number 3 from array B (1 < 3 and 2 < 3).

In the second sample test the only way to choose k elements in the first array and m elements in the second one is to choose all numbers in both arrays, but then not all the numbers chosen in A will be less than all the numbers chosen in B: 技术分享.

技术分享

Code:

技术分享
 1 #pragma comment(linker, "/STACK:36777216") 2  3 #include <bits/stdc++.h> 4 using namespace std; 5 #define LSON            id << 1 , l , mid 6 #define RSON            id << 1 | 1 , mid + 1 , r 7 #define ROOT            1 , 1 , n 8 #define CLR(x , y)      memset(x , y , sizeof(x)) 9 #define LOWBIT(x)       x & (-x)10 #define FORN(i , a , n)  for(int i = (a) ; i <= (n) ; ++i)11 #define FORP(i , n , a)  for(int i = (n) ; i >= (a) ; --i)12 #define CASE(x)        printf("Case %d: ", x)13 #define SFD(x)      scanf("%lf" , &x)14 #define SFC(x)      scanf(" %c" , &x)15 #define SFS(x)      scanf(" %s" , x)16 #define SFI(x)      scanf("%d" , &x)17 #define SFI64(x)    scanf("%I64d" , &x)18 #define PFF(x)         printf("%f" , x)19 #define PFD(x)         printf("%lf" , x)20 #define PFI(x)         printf("%d" , x)21 #define PFC(x)         printf("%c" , x)22 #define PFS(x)         printf("%s" , x)23 #define PFI64(x)       printf("%I64d" , x)24 #define SPACE          printf(" ")25 #define PUT            puts("")26 #define LPUP(i , j , k) for(int i = j ; i <= k ; ++i)27 #define LPDW(i , j , k) for(int i = j ; i >= k ; --i)28 #define PB(x)          push_back(x)29 #define ALL(A)         A.begin(), A.end()30 #define SZ(A)          int((A).size())31 #define LBD(A, x)      (lower_bound(ALL(A), x) - A.begin())32 #define UBD(A, x)      (upper_bound(ALL(A), x) - A.begin())33 #define LOCAL34 static const double PI = acos(-1.0);35 static const double EPS = 1e-8;36 static const int INF = 0X3fffffff;37 typedef double DB;38 template<class T> inline39 void read(T &x)40 {41     x = 0;42     int f = 1 ; char ch = getchar();43     while (ch < 0 || ch > 9) {if (ch == -) f = -1; ch = getchar();}44     while (ch >= 0 && ch <= 9) {x = x * 10 + ch - 0; ch = getchar();}45     x *= f;46     return ;47 }48 49 /************************Little Pea****************************/50 51 static const int MAXN = 1e5 + 10;52 int k , m;53 int na , nb;54 int da[MAXN] , db[MAXN];55 int main()56 {57 #ifndef ONLINE_JUDGE58     freopen("D:\\系统优化\\Desktop\\littlepea\\in.data" , "r" , stdin);59 #endif60     read(na);read(nb);61     read(k);read(m);62     LPUP(i , 1 , na)63     {64         read(da[i]);65     }66     LPUP(i , 1 , nb)67     {68         read(db[i]);69     }70     int mxa , mib;71     mxa = da[k];72     mib = db[nb - m + 1];73     if(mib > mxa)74         puts("YES");75     else76         puts("NO");77 78 #ifndef ONLINE_JUDGE79     fclose(stdin), fclose(stdout);80 #endif81 }
View Code

B. Order Book(计数排序)

In this task you need to process a set of stock exchange orders and use them to create order book.

An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price p**i, direction d**i — buy or sell, and integer q**i. This means that the participant is ready to buy or sell q**i stocks at price p**i for one stock. A value q**i is also known as a volume of an order.

All orders with the same price p and direction d are merged into one aggregated order with price p and direction d. The volume of such order is a sum of volumes of the initial orders.

An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order.

An order book of depth s contains s best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than s aggregated orders for some direction then all of them will be in the final order book.

You are given n stock exhange orders. Your task is to print order book of depth s for these orders.

Input

The input starts with two positive integers n and s (1 ≤ n ≤ 1000, 1 ≤ s ≤ 50), the number of orders and the book depth.

Next n lines contains a letter d**i (either ‘B‘ or ‘S‘), an integer p**i (0 ≤ p**i ≤ 105) and an integer q**i (1 ≤ q**i ≤ 104) — direction, price and volume respectively. The letter ‘B‘ means buy, ‘S‘ means sell. The price of any sell order is higher than the price of any buy order.

Output

Print no more than 2s lines with aggregated orders from order book of depth s. The output format for orders should be the same as in input.

Examples

input

6 2B 10 3S 50 2S 40 1S 50 6B 20 4B 25 10

output

S 50 8S 40 1B 25 10B 20 4

Note

Denote (x, y) an order with price x and volume y. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample.

You need to print no more than two best orders for each direction, so you shouldn‘t print the order (10 3) having the worst price among buy orders.

技术分享

Code:

技术分享
  1 #pragma comment(linker, "/STACK:36777216")  2   3 #include <bits/stdc++.h>  4 using namespace std;  5 #define LSON            id << 1 , l , mid  6 #define RSON            id << 1 | 1 , mid + 1 , r  7 #define ROOT            1 , 1 , n  8 #define CLR(x , y)      memset(x , y , sizeof(x))  9 #define LOWBIT(x)       x & (-x) 10 #define FORN(i , a , n)  for(int i = (a) ; i <= (n) ; ++i) 11 #define FORP(i , n , a)  for(int i = (n) ; i >= (a) ; --i) 12 #define CASE(x)        printf("Case %d: ", x) 13 #define SFD(x)      scanf("%lf" , &x) 14 #define SFC(x)      scanf(" %c" , &x) 15 #define SFS(x)      scanf(" %s" , x) 16 #define SFI(x)      scanf("%d" , &x) 17 #define SFI64(x)    scanf("%I64d" , &x) 18 #define PFF(x)         printf("%f" , x) 19 #define PFD(x)         printf("%lf" , x) 20 #define PFI(x)         printf("%d" , x) 21 #define PFC(x)         printf("%c" , x) 22 #define PFS(x)         printf("%s" , x) 23 #define PFI64(x)       printf("%I64d" , x) 24 #define SPACE          printf(" ") 25 #define PUT            puts("") 26 #define LPUP(i , j , k) for(int i = j ; i <= k ; ++i) 27 #define LPDW(i , j , k) for(int i = j ; i >= k ; --i) 28 #define PB(x)          push_back(x) 29 #define ALL(A)         A.begin(), A.end() 30 #define SZ(A)          int((A).size()) 31 #define LBD(A, x)      (lower_bound(ALL(A), x) - A.begin()) 32 #define UBD(A, x)      (upper_bound(ALL(A), x) - A.begin()) 33 #define LOCAL 34 static const double PI = acos(-1.0); 35 static const double EPS = 1e-8; 36 static const int INF = 0X3fffffff; 37 typedef double DB; 38 template<class T> inline 39 T read(T &x) 40 { 41     x = 0; 42     int f = 1 ; char ch = getchar(); 43     while (ch < 0 || ch > 9) {if (ch == -) f = -1; ch = getchar();} 44     while (ch >= 0 && ch <= 9) {x = x * 10 + ch - 0; ch = getchar();} 45     x *= f; 46 } 47  48 /************************Little Pea****************************/ 49  50 static const int OO = 0x3fffffff; 51 static const int MAXN = 1e5 + 10; 52 int S[MAXN]; 53 int B[MAXN]; 54 int n , s; 55 int main() 56 { 57 #ifndef ONLINE_JUDGE 58     freopen("D:\\系统优化\\Desktop\\littlepea\\in.data" , "r" , stdin); 59 #endif 60     read(n);read(s); 61     int hs = 0 , hb = 0; 62     LPUP(i , 1 , n) 63     { 64         char c ; 65         int p , q; 66         SFC(c);read(p);read(q); 67         if(c == S) 68         { 69             S[p] += q; 70         } 71         else 72         { 73             B[p] += q; 74         } 75     } 76     int i = 0; 77     for(; i <= 100000 && hs < s ; ++i) 78     { 79         if(S[i]) 80             ++hs; 81     } 82     LPDW(j , i - 1 , 0) 83     { 84         if(S[j]) 85             printf("S %d %d\n" , j , S[j]); 86     } 87     i = 100000; 88     for(; i >= 0 && hb < s ; --i) 89     { 90         if(B[i]) 91             ++hb; 92     } 93     LPDW(j , 100000 , i + 1) 94     { 95         if(B[j]) 96             printf("B %d %d\n" , j , B[j]); 97     } 98  99 #ifndef ONLINE_JUDGE100     fclose(stdin), fclose(stdout);101 #endif102 }
View Code

 

2017-5-1-Train:Codeforces Round #317 [AimFund Thanks-Round] (Div. 2)