Codeforces Unlucky Ticket 题解
来源:互联网 发布:mac桌面东西没了 编辑:程序博客网 时间:2024/05/18 21:06
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 outstrictly 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 and1 < 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.
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.
In the first line print "YES" if the ticket meets the unluckiness criterion. Otherwise, print "NO" (without the quotes).
22421
YES
本题思路:
1 利用string保存数据,把string分成两半
2 由小到大排序
3 有小到大逐个比较,有一个不符合条件就可以结束循环了
#include <string>#include <iostream>using namespace std;void UnluckyTicket(){int n = 0;cin>>n;string left, right;cin>>left;right = left.substr(n);left.resize(n);sort(left.begin(), left.end());sort(right.begin(), right.end());bool unluck = true;if (left[0] < right[0]){for (unsigned i = 0; i < n; i++){if (left[i] >= right[i]){unluck = false;break;}}}else{for (unsigned i = 0; i < n; i++){if (left[i] <= right[i]){unluck = false;break;}}}if (unluck) cout<<"YES";else cout<<"NO";}
- Codeforces Unlucky Ticket 题解
- CodeForces 160B - Unlucky Ticket
- CodeForces 160B Unlucky Ticket
- CodeForces 160B Unlucky Ticket
- Codeforces-160B-Unlucky Ticket
- Codeforces Round #111 (Div. 2) B Unlucky Ticket (贪心)
- B. Unlucky Ticket
- cf160B Unlucky Ticket
- CF160 B Unlucky Ticket (sort)
- Luba And The Ticket CodeForces
- codeforces 845B Luba And The Ticket
- Codeforces 845 B Luba And The Ticket
- codeforces 845B.Luba And The Ticket
- unlucky number
- Codeforces Round #104 (Div. 2)---A. Lucky Ticket
- Codeforces 845B Luba And The Ticket (贪心)
- cf Educational Codeforces Round 27 B. Luba And The Ticket
- codeforces Round #180题解
- nefu 650 max num
- JAVA设计模式-装饰设计模式-继承体系的由来和装饰类的优化
- 取石子游戏 hdu 1907、2509、 水1846
- C++之this指针和const对象/成员函数
- ffmpeg搭建hls服务器
- Codeforces Unlucky Ticket 题解
- IC总线应用下的EEPROM
- 黑马程序员---.NET\C#J基础扫盲日记
- Spiral Matrix II
- Asp.Net Mvc 4 使用Ajax 遇到的问题
- CXF学习02---处理JavaBean类型与List的集合类型
- js控制图片自适应屏幕
- NGUI学习之路(2)Scroll View 简洁版【NGUI 2.6.3】
- Java卡环境介绍