CodeForces-160B Unlucky Ticket

B. Unlucky Ticket
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 such bijective correspondence between the digits of the first and the second half of the ticket, that either each digit of the first half turns out strictly 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 > 1 and 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.

Input
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).

Examples
inputCopy
2
2421
outputCopy
YES
inputCopy
2
0135
outputCopy
YES
inputCopy
2
3754
outputCopy
NO

原题链接

http://codeforces.com/problemset/problem/160/B

解析:

这个题目的大意就是让我们找到非幸运数,非幸运数是指找到一个判断关系,如果符合这个关系,就是非幸运数

例如:2421    2 > 1、4  > 2

即符合幸运数要求;

0135

0 < 3、1 < 5,

即符合非幸运数要求;

3754

3 < 4、7 > 5

即不符合幸运数要求....

这里用的是基础贪心算法➕简单的与操作。

import java.util.Scanner;
import java.util.Arrays;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		char []aa = new char[50 + 2];
		char []bb = new char[50 + 2];
		int []a = new int[100];
		int []b = new int[100];
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		String m = scanner.next();
		for(int i  = 0;i < n;++i)
		{
			a[i] = m.charAt(i) - '0';
		}
		for(int i = 0;i < n;++i)
		{
			b[i] = m.charAt(i+n) - '0';
		}
		/*for(int i = 0;i < n;++i)
		{
			System.out.println(b[i]);
		}
		for(int i = 0;i < n;++i)
		{
			System.out.println(a[i]);
		}*/
		Arrays.sort(a,0,n);//分治数组排序
		Arrays.sort(b,0,n);//分治数组排序
		/*for(int i = 0;i < n;++i)
		{
			System.out.println(a[i]);
		}
		for(int i = 0;i < n;++i)
		{
			System.out.println(b[i]);
		}*/
		boolean num = true;//设置  <= 判断Boolean变量
		boolean sum = true;//设置  >= 判断Boolean变量
		for(int i = 0;i < n;++i)
		{
			if(a[i] >= b[i])
				sum = false;
		}
		for(int i = 0;i < n;++i)
		{
			if(a[i] <= b[i])
				num = false;
		}
		//System.out.println(num);
		//System.out.println(sum);
		if(num||sum)//如果符合恒大于或恒小于元素,即为非幸运数
		{
			System.out.println("YES");
		}
		else {
			System.out.println("NO");
		}
	}

}

 

你可能感兴趣的:(贪心)