B. Unlucky Ticket
Each of you probably has your personal experience of riding public transportation and buying tickets. After a person buys a ticket (which traditionally has an even number of digits), he usually checks whether the ticket is lucky. Let us remind you that a ticket is lucky if the sum of digits in its first half matches the sum of digits in its second half.
But of course, not every ticket can be lucky. Far from it! Moreover, sometimes one look at a ticket can be enough to say right away that the ticket is not lucky. So, let's consider the following unluckiness criterion that can definitely determine an unlucky ticket. We'll say that a ticket is definitely unlucky if each digit from the first half corresponds to some digit from the second half so that each digit from the first half is strictly less than the corresponding digit from the second one or each digit from the first half is strictly more than the corresponding digit from the second one. Each digit should be used exactly once in the comparisons. In other words, there is suchbijective correspondence between the digits of the first and the second half of the ticket, that either each digit of the first half turns outstrictly less than the corresponding digit of the second half or each digit of the first half turns out strictly more than the corresponding digit from the second half.
For example, ticket 2421 meets the following unluckiness criterion and will not be considered lucky (the sought correspondence is 2 > 1and 4 > 2), ticket 0135 also meets the criterion (the sought correspondence is 0 < 3 and 1 < 5), and ticket 3754 does not meet the criterion.
You have a ticket in your hands, it contains 2n digits. Your task is to check whether it meets the unluckiness criterion.
<p< p=""><p< p="">
Input
<p< p="">
The first line contains an integer n (1 ≤ n ≤ 100). The second line contains a string that consists of 2n digits and defines your ticket.
Output
In the first line print "YES" if the ticket meets the unluckiness criterion. Otherwise, print "NO" (without the quotes).
Sample test(s)
input
2
2421
output
YES
input
2
0135
output
YES
input
2
3754
output
NO
解题报告:这道题题意就是判断数是否满足要去,判断一个数2*n(位数), 若前n个数全部大于后n个数(顺序可以变),或前n个数全部小于后n个数,这输出YES,否则输出NO
但是这道题测试数据有点变态必须输入的个数是2*n;例如:2, 111是不合法的;
代码如下:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 110;
char str[2 * N];
int a[N], b[N];
int cmp ( const void *a , const void *b )
{
return *(int *)a - *(int *)b;
}
int main()
{
int n, i, flag = 0, p, q;
scanf("%d", &n);
scanf("%s", str);
for (i = 0; i < n; ++i)
{
a[i] = str[i] - '0';
}
for (i = 0; i < n; ++i)
{
b[i] = str[n + i] - '0';
}
p = 0;
q = 0;
qsort(a, n, sizeof(a[0]), cmp);
qsort(b, n, sizeof(b[0]), cmp);
for (i = 0; i < n; ++i)
{
if(a[i] < b[i])
{
p ++;
}
else if (a[i] > b[i])
{
q ++;
}
}
if ((p == 0|| q ==0) && p + q == n)//必须判断p + q == n
{
flag = 1;
}
if (flag)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
return 0;
}