Codeforces Round #104 (Div. 2) ABCD
2015-09-22 20:05
281 查看
A - Lucky Ticket
B - Lucky Mask
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits
4 and 7. For example, numbers
47, 744,
4 are lucky and
5, 17,
467 are not.
Petya calls a mask of a positive integer
n the number that is obtained after successive writing of all lucky digits of number
n from the left to the right. For example, the mask of number
72174994 is number 7744, the mask of
7 is 7, the mask of
9999047 is 47. Obviously, mask of any number is always a lucky number.
Petya has two numbers — an arbitrary integer a and a lucky number
b. Help him find the minimum number
c (c > a) such that the mask of number
c equals b.
Input
The only line contains two integers a and
b (1 ≤ a, b ≤ 105). It is guaranteed that number
b is lucky.
Output
In the only line print a single number — the number c that is sought by Petya.
Sample test(s)
Input
Output
Input
Output
C - Lucky Conversion
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits
4 and 7. For example, numbers
47, 744,
4 are lucky and
5, 17,
467 are not.
Petya has two strings a and
b of the same length n. The strings consist only of lucky digits. Petya can perform
operations of two types:
replace any one digit from string a by its opposite (i.e., replace
4 by 7 and
7 by 4);
swap any pair of digits in string a.
Petya is interested in the minimum number of operations that are needed to make string
a equal to string b. Help him with the task.
Input
The first and the second line contains strings a and
b, correspondingly. Strings
a and b have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed
105.
Output
Print on the single line the single number — the minimum number of operations needed to convert string
a into string b.
Sample test(s)
Input
Output
Input
Output
Input
Output
其实就是找同一位置ab串不同时的数量
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits
4 and 7. For example, numbers
47, 744,
4 are lucky and
5, 17,
467 are not.
Petya loves long lucky numbers very much. He is interested in the
minimum lucky number d that meets some condition. Let
cnt(x) be the number of occurrences of number
x in number d as a substring. For example, if
d = 747747, then cnt(4) = 2,
cnt(7) = 4, cnt(47) = 2,
cnt(74) = 2. Petya wants the following condition to fulfil simultaneously:
cnt(4) = a1,
cnt(7) = a2,
cnt(47) = a3,
cnt(74) = a4. Petya is not interested in the occurrences of other numbers. Help him cope with this task.
Input
The single line contains four integers a1,
a2,
a3 and a4 (1 ≤ a1, a2, a3, a4 ≤ 106).
Output
On the single line print without leading zeroes the answer to the problem — the minimum lucky number
d such, that cnt(4) = a1,
cnt(7) = a2,
cnt(47) = a3,
cnt(74) = a4. If such number does not exist, print the single number "-1" (without the quotes).
Sample test(s)
Input
Output
Input
Output
贪心构造
Let we have some string result s. Let then delete all repititions, i. e. while we have some pair adjacent equal digits, we delete one of them. Let call formed string a root. In root there will be no adjacent equal digits,
so |cnt(47) - cnt(74)| ≤ 1. So, if
|a3 - a4| > 1, then answer is "-1". Now, if we would know the root, that will be used in our result, we can create result.
You can see, that if a3 = a4, then root must be
47474747...474 or 747474...747. If
a3 < a4, then root is
74747474....74. If a3 > a4, then root is
474747...47. Length of the root must be such that it fulfill
a3 and
a4.
Now, when you have a root, you can build result. You just need to find first occurrence of
4 in root and insert the rest of
4 from a1 right next to that digit. To add the rest of
7, you need to find last occurrence of
7 in root.
The answer does not exits if, after constructing the root, you have used more
4 than a1 or more
7 than a2.
或见代码
char a[55]; int n; int main() { while( ~scanf("%d", &n) ) { scanf("%s", a+1); n = strlen( a+1 ); int s1 = 0, s2 = 0; bool OK = 1; for( int i = 1; i <= n; ++i ) { if( i <= n/2 ) s1 += a[i] - '0'; else s2 += a[i] - '0'; if( a[i] != '4' && a[i] != '7' ) OK = 0; } if( s1 != s2 ) OK = 0; if( OK ) puts("YES"); else puts("NO"); } return 0; }
B - Lucky Mask
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits
4 and 7. For example, numbers
47, 744,
4 are lucky and
5, 17,
467 are not.
Petya calls a mask of a positive integer
n the number that is obtained after successive writing of all lucky digits of number
n from the left to the right. For example, the mask of number
72174994 is number 7744, the mask of
7 is 7, the mask of
9999047 is 47. Obviously, mask of any number is always a lucky number.
Petya has two numbers — an arbitrary integer a and a lucky number
b. Help him find the minimum number
c (c > a) such that the mask of number
c equals b.
Input
The only line contains two integers a and
b (1 ≤ a, b ≤ 105). It is guaranteed that number
b is lucky.
Output
In the only line print a single number — the number c that is sought by Petya.
Sample test(s)
Input
1 7
Output
7
Input
100 47
Output
147
const int N = 200020; int a, b, x; int main() { while( ~scanf("%d%d", &a, &b) ) { int x = a+1; while( 1 ) { int p[10], l = 0; int tmp = 0, z = x; while( z ) { if( (z % 10 == 4) || (z % 10 == 7) ) p[++l] = z % 10; z /= 10; } for( int i = l; i >= 1; --i ) tmp = tmp * 10 + p[i]; if( tmp == b ) break; x++; } printf("%d\n", x); } return 0; }
C - Lucky Conversion
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits
4 and 7. For example, numbers
47, 744,
4 are lucky and
5, 17,
467 are not.
Petya has two strings a and
b of the same length n. The strings consist only of lucky digits. Petya can perform
operations of two types:
replace any one digit from string a by its opposite (i.e., replace
4 by 7 and
7 by 4);
swap any pair of digits in string a.
Petya is interested in the minimum number of operations that are needed to make string
a equal to string b. Help him with the task.
Input
The first and the second line contains strings a and
b, correspondingly. Strings
a and b have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed
105.
Output
Print on the single line the single number — the minimum number of operations needed to convert string
a into string b.
Sample test(s)
Input
4774
Output
1
Input
774 744
Output
1
Input
777444
Output
3
其实就是找同一位置ab串不同时的数量
const int N = 100005; char a , b ; int main() { scanf("%s%s", a, b); int l = strlen(a); int cnt47 = 0, cnt74 = 0; for (int i = 0; i < l; ++i) { if (a[i] == '4' && b[i] == '7') { ++cnt47; } if (a[i] == '7' && b[i] == '4') { ++cnt74; } } printf("%d\n", max(cnt47, cnt74)); return 0; }
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits
4 and 7. For example, numbers
47, 744,
4 are lucky and
5, 17,
467 are not.
Petya loves long lucky numbers very much. He is interested in the
minimum lucky number d that meets some condition. Let
cnt(x) be the number of occurrences of number
x in number d as a substring. For example, if
d = 747747, then cnt(4) = 2,
cnt(7) = 4, cnt(47) = 2,
cnt(74) = 2. Petya wants the following condition to fulfil simultaneously:
cnt(4) = a1,
cnt(7) = a2,
cnt(47) = a3,
cnt(74) = a4. Petya is not interested in the occurrences of other numbers. Help him cope with this task.
Input
The single line contains four integers a1,
a2,
a3 and a4 (1 ≤ a1, a2, a3, a4 ≤ 106).
Output
On the single line print without leading zeroes the answer to the problem — the minimum lucky number
d such, that cnt(4) = a1,
cnt(7) = a2,
cnt(47) = a3,
cnt(74) = a4. If such number does not exist, print the single number "-1" (without the quotes).
Sample test(s)
Input
2 2 1 1
Output
4774
Input
4 7 3 1
Output
-1
贪心构造
Let we have some string result s. Let then delete all repititions, i. e. while we have some pair adjacent equal digits, we delete one of them. Let call formed string a root. In root there will be no adjacent equal digits,
so |cnt(47) - cnt(74)| ≤ 1. So, if
|a3 - a4| > 1, then answer is "-1". Now, if we would know the root, that will be used in our result, we can create result.
You can see, that if a3 = a4, then root must be
47474747...474 or 747474...747. If
a3 < a4, then root is
74747474....74. If a3 > a4, then root is
474747...47. Length of the root must be such that it fulfill
a3 and
a4.
Now, when you have a root, you can build result. You just need to find first occurrence of
4 in root and insert the rest of
4 from a1 right next to that digit. To add the rest of
7, you need to find last occurrence of
7 in root.
The answer does not exits if, after constructing the root, you have used more
4 than a1 or more
7 than a2.
或见代码
#include <bits/stdc++.h>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <time.h>
#include <vector>
#include <cstdio>
#include <string>
#include <iomanip>
///cout << fixed << setprecision(13) << (double) x << endl;
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define lson l, mid, rt << 1#define rson mid + 1, r, rt << 1 | 1#define ls rt << 1#define rs rt << 1 | 1#define pi acos(-1.0)
#define eps 1e-8
#define Mp(a, b) make_pair(a, b)
#define asd puts("asdasdasdasdasdf");
typedef long long ll;
typedef pair <int, int> pl;
//typedef __int64 LL;
const int inf = 0x3f3f3f3f;
string ans;
int a1, a2, a3, a4;
int main()
{
while( ~scanf("%d%d%d%d", &a1, &a2, &a3, &a4 ) ) {
ans = "";
if( abs( a3-a4 ) > 1 || a3 > a1 || a3 > a2 || a4 > a1 || a4 > a2 || a3+a4 >= a1+a2 ) {
puts("-1");
continue;
}
// 4444444444474747 if( a3 == a4 + 1 ) {
for( int i = 1; i <= a1 - a3; ++i ) {
ans += '4';
}
for( int i = 1; i <= a3; ++i ) {
ans += '4';
ans += '7';
}
for( int i = 1; i <= a2 - a3; ++i )
ans += '7';
}
// 74444444447474777777777774
else if( a3 == a4 - 1 ) {
ans += '7';
ans += '4';
for( int i = 1; i <= a1-a4; ++i ) {
ans += '4';
}
for( int i = 1; i < a4-1; ++i ) {
ans += '7';
ans += '4';
}
for( int i = 1; i <= a2 - (a4-1); ++i )
ans += '7';
ans += '4';
}
// 7444444474747777774
else {
if (a1 - a4 - 1 >= 0 && a2 - a4 >= 0) {
for(int i = 1; i <= a1 - a4 - 1; ++ i)
ans += '4';
for(int i = 1; i <= a4; ++ i) {
ans += '4';
ans += '7';
}
for(int i = 1; i <= a2 - a4; ++ i)
ans += '7';
ans += '4';
}
else if (a1 - a4 >= 0 && a2 - a4 - 1 >= 0) {
ans += '7';
for(int i = 1; i <= a1 - a4; ++ i)
ans += '4';
for(int i = 1; i <= a4; ++ i) {
ans += '4';
ans += '7';
}
for(int i = 1; i <= a2 - a4 - 1; ++ i)
ans += '7';
}
}
cout << ans << endl;
}
return 0;
}
相关文章推荐
- JAVA上机——
- 编译android内核和文件系统,已经安装jdk,提示build/core/config.mk:268: *** Error: could not find jdk tools.jar
- iOS开发 ---- 其他控件,弹窗,滑块,菊花,步进,分段等
- Codeforces 461B Appleman and Tree(木dp)
- JavaScript学习总结(一)基础部分
- C++多线程编程
- iOS开发 ----- UISearchBar_UISearchController
- 社团划分——Fast Unfolding算法
- Android开发之闹钟
- JAVA Current date and time
- NFS服务的简单使用
- hdu 5464 Clarke and problem(dp)
- SQL 语句的子查询例子
- JAVA上机作业 3.22
- Qt Mac 下软件Release 发布dmg
- 无限分类左右值实现算法
- HttpRouter项目中的问题
- iOS开发 ----- UICollectionView
- MyBatis学习总结(一)——MyBatis快速入门
- OCP-V13-521