Codeforces #422 Div.2 2017.7.3部分题解
2017-07-03 02:50
435 查看
由于没有人组队,基本自己思考,再加上之前调试网络,导致写题的时间只有一个小时多一点点了,比较疲倦所以就只写了三道题
memory limit per test256 megabytes
inputstandard input
outputstandard output
Holidays have finished. Thanks to the help of the hacker Leha, Noora managed to enter the university of her dreams which is located in a town Pavlopolis. It’s well known that universities provide students with dormitory for the period of university studies. Consequently Noora had to leave Vičkopolis and move to Pavlopolis. Thus Leha was left completely alone in a quiet town Vičkopolis. He almost even fell into a depression from boredom!
Leha came up with a task for himself to relax a little. He chooses two integers A and B and then calculates the greatest common divisor of integers “A factorial” and “B factorial”. Formally the hacker wants to find out GCD(A!, B!). It’s well known that the factorial of an integer x is a product of all positive integers less than or equal to x. Thus x! = 1·2·3·…·(x - 1)·x. For example 4! = 1·2·3·4 = 24. Recall that GCD(x, y) is the largest positive integer q that divides (without a remainder) both x and y.
Leha has learned how to solve this task very effective. You are able to cope with it not worse, aren’t you?
4 3
output
6
4! = 1·2·3·4 = 24. 3! = 1·2·3 = 6. The greatest common divisor of integers 24 and 6 is exactly 6.
不是很想讲这钟普及组难度不到的题…三十秒写完…
memory limit per test256 megabytes
inputstandard input
outputstandard output
Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he decided to solve some crosswords. It’s well known that it is a very interesting occupation though it can be very difficult from time to time. In the course of solving one of the crosswords, Leha had to solve a simple task. You are able to do it too, aren’t you?
Leha has two strings s and t. The hacker wants to change the string s at such way, that it can be found in t as a substring. All the changes should be the following: Leha chooses one position in the string s and replaces the symbol in this position with the question mark “?”. The hacker is sure that the question mark in comparison can play the role of an arbitrary symbol. For example, if he gets string s=”ab?b” as a result, it will appear in t=”aabrbb” as a substring.
Guaranteed that the length of the string s doesn’t exceed the length of the string t. Help the hacker to replace in s as few symbols as possible so that the result of the replacements can be found in t as a substring. The symbol “?” should be considered equal to any other symbol.
The second line contains n lowercase English letters — string s.
The third line contains m lowercase English letters — string t.
In the second line print k distinct integers denoting the positions of symbols in the string s which need to be replaced. Print the positions in any order. If there are several solutions print any of them. The numbering of the positions begins from one.
3 5
abc
xaybz
output
2
2 3
input
4 10
abcd
ebceabazcd
output
1
2
这道题我的解决思路是一个复杂度大概是n*m的方法,所以我们只需要for第二个串中第一个串的匹配位置,然后记录从这个位置开始的最多不匹配个数,然后再在所有的记录的值中找到一个最小的值,然后进行该起点的计数和输出
memory limit per test256 megabytes
inputstandard input
outputstandard output
It’s well known that the best way to distract from something is to do one’s favourite thing. Job is such a thing for Leha.
So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers li, ri, costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.
At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don’t intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don’t intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.
Help Leha to choose the necessary vouchers!
Each of the next n lines contains three integers li, ri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.
4 5
1 3 4
1 2 5
5 6 1
1 2 4
output
5
input
3 2
4 6 3
2 4 1
3 5 4
output
-1
In the second sample the duration of each voucher is 3 therefore it’s impossible to choose two vouchers with the total duration equal to 2.
这道题其实也是只需要进行所有区间的整理,我是用一个vector来存储了所有的出现长度的边的编号,这样一来我们在枚举i长度的时候,就可以枚举x-i长度的边,直接查询vector[x-i]的size大小,如果等于0的话,就continue,可以省下不少时间
A. I’m bored with life
time limit per test1 secondmemory limit per test256 megabytes
inputstandard input
outputstandard output
Holidays have finished. Thanks to the help of the hacker Leha, Noora managed to enter the university of her dreams which is located in a town Pavlopolis. It’s well known that universities provide students with dormitory for the period of university studies. Consequently Noora had to leave Vičkopolis and move to Pavlopolis. Thus Leha was left completely alone in a quiet town Vičkopolis. He almost even fell into a depression from boredom!
Leha came up with a task for himself to relax a little. He chooses two integers A and B and then calculates the greatest common divisor of integers “A factorial” and “B factorial”. Formally the hacker wants to find out GCD(A!, B!). It’s well known that the factorial of an integer x is a product of all positive integers less than or equal to x. Thus x! = 1·2·3·…·(x - 1)·x. For example 4! = 1·2·3·4 = 24. Recall that GCD(x, y) is the largest positive integer q that divides (without a remainder) both x and y.
Leha has learned how to solve this task very effective. You are able to cope with it not worse, aren’t you?
Input
The first and single line contains two integers A and B (1 ≤ A, B ≤ 109, min(A, B) ≤ 12).Output
Print a single integer denoting the greatest common divisor of integers A! and B!.Example
input4 3
output
6
Note
Consider the sample.4! = 1·2·3·4 = 24. 3! = 1·2·3 = 6. The greatest common divisor of integers 24 and 6 is exactly 6.
不是很想讲这钟普及组难度不到的题…三十秒写完…
#include<cstdio> #include<iostream> using namespace std; int main(){ int a,b; scanf("%d%d",&a,&b); a=min(a,b); int temp=1; for(register int i=1;i<=a;i++) temp*=i; printf("%d\n",temp); return 0; }
B. Crossword solving
time limit per test1 secondmemory limit per test256 megabytes
inputstandard input
outputstandard output
Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he decided to solve some crosswords. It’s well known that it is a very interesting occupation though it can be very difficult from time to time. In the course of solving one of the crosswords, Leha had to solve a simple task. You are able to do it too, aren’t you?
Leha has two strings s and t. The hacker wants to change the string s at such way, that it can be found in t as a substring. All the changes should be the following: Leha chooses one position in the string s and replaces the symbol in this position with the question mark “?”. The hacker is sure that the question mark in comparison can play the role of an arbitrary symbol. For example, if he gets string s=”ab?b” as a result, it will appear in t=”aabrbb” as a substring.
Guaranteed that the length of the string s doesn’t exceed the length of the string t. Help the hacker to replace in s as few symbols as possible so that the result of the replacements can be found in t as a substring. The symbol “?” should be considered equal to any other symbol.
Input
The first line contains two integers n and m (1 ≤ n ≤ m ≤ 1000) — the length of the string s and the length of the string t correspondingly.The second line contains n lowercase English letters — string s.
The third line contains m lowercase English letters — string t.
Output
In the first line print single integer k — the minimal number of symbols that need to be replaced.In the second line print k distinct integers denoting the positions of symbols in the string s which need to be replaced. Print the positions in any order. If there are several solutions print any of them. The numbering of the positions begins from one.
Examples
input3 5
abc
xaybz
output
2
2 3
input
4 10
abcd
ebceabazcd
output
1
2
这道题我的解决思路是一个复杂度大概是n*m的方法,所以我们只需要for第二个串中第一个串的匹配位置,然后记录从这个位置开始的最多不匹配个数,然后再在所有的记录的值中找到一个最小的值,然后进行该起点的计数和输出
#include<cstdio> #include<iostream> using namespace std; char A[1005],B[1005]; int len[1005],temp[1005]; int main(){ int n,m; scanf("%d%d",&n,&m); scanf("%s%s",&A,&B); for(register int i=0;i<=m-n;i++){ for(register int j=i;j<=i+n-1;j++){ if(A[j-i]!=B[j]) len[i]++; } } int minn=0x7fffffff; int mark=0; for(register int i=0;i<=m-n;i++){ if(minn>len[i]){ minn=len[i]; mark=i; } } int cnt=0; for(register int i=mark;i<=mark+n-1;i++){ if(A[i-mark]!=B[i]){ cnt++; temp[cnt]=i-mark+1; } } printf("%d\n",cnt); for(register int i=1;i<=cnt;i++) printf("%d ",temp[i]); return 0; } /* 3 5 abc xaybz */
C题T了
C. Hacker, pack your bags!
time limit per test2 secondsmemory limit per test256 megabytes
inputstandard input
outputstandard output
It’s well known that the best way to distract from something is to do one’s favourite thing. Job is such a thing for Leha.
So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers li, ri, costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.
At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don’t intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don’t intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.
Help Leha to choose the necessary vouchers!
Input
The first line contains two integers n and x (2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha’s vacation correspondingly.Each of the next n lines contains three integers li, ri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.
Output
Print a single integer — a minimal amount of money that Leha will spend, or print - 1 if it’s impossible to choose two disjoint vouchers with the total duration exactly x.Examples
input4 5
1 3 4
1 2 5
5 6 1
1 2 4
output
5
input
3 2
4 6 3
2 4 1
3 5 4
output
-1
Note
In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5.In the second sample the duration of each voucher is 3 therefore it’s impossible to choose two vouchers with the total duration equal to 2.
这道题其实也是只需要进行所有区间的整理,我是用一个vector来存储了所有的出现长度的边的编号,这样一来我们在枚举i长度的时候,就可以枚举x-i长度的边,直接查询vector[x-i]的size大小,如果等于0的话,就continue,可以省下不少时间
#include<cstdio> #include<iostream> #include<vector> const int MAXN = 1000000; using namespace std; struct Line{ int l,r,cost,len; }line[MAXN]; vector<int>v[1000000]; int tail,n,x; void add(int L,int R,int Cost,int len){ tail++; v[len].push_back(tail); line[tail].l=L; line[tail].r=R; line[tail].cost=Cost; line[tail].len=len; } int main(){ int tempL,tempR,tempcost; scanf("%d%d",&n,&x); for(register int i=1;i<=n;i++){ scanf("%d%d%d",&tempL,&tempR,&tempcost); add(tempL,tempR,tempcost,tempR-tempL+1); } bool judge=false; int minn=0x7fffffff; for(register int i=1;i<=x-1;i++){ if(v[i].size()==0) continue; int temp=x-i; if(v[temp].size()==0) continue; for(register int j=0;j<=v[i].size()-1;j++){ int loc=v[i][j]; for(register int k=0;k<=v[temp].size()-1;k++){ int ation=v[temp][k]; if(line[loc].l>line[ation].r||line[ation].l>line[loc].r){ minn=min(minn,line[loc].cost+line[ation].cost); judge=true; } } } } if(judge) printf("%d\n",minn); else printf("-1\n"); return 0; }
相关文章推荐
- 寒假练习赛20170109 (部分题解)
- 【FZU 2200、2202~2204】FOJ有奖月赛 2015.10 个人的部分题解(C、E、F、G)
- Zhejiang Provincial Programming Contest 2007 部分题解
- CodeForces #292 div.2 题解
- 喵哈哈村的魔法考试 Round #5 (Div.2) 题解
- 福州大学第十届程序设计竞赛 -- 部分题解
- 【2013杭州区域赛】部分题解 hdu4770—4780
- BestCoder Round #20 部分题解(A,B,C)(hdu5123,5124,5125)
- 【BestCoder Round #81 (div.2)】【HDU5670&5671&5672】题意&题解&代码(C++)
- 喵哈哈村的魔法考试 Round #1 (Div.2) 题解
- SWUST ACM 训练题部分题解 hdu1384 && hdu3666 && hdu 4786 &&uva 1395 && uva 1151
- CTU 2011/CTU 2012 部分题解...
- BestCoder Round #54 (div.2) 题解
- 喵哈哈村的魔法考试 Round #2 (Div.2) 题解
- 2016 计蒜之道 复赛 题解 部分
- POJ数据结构专辑(含部分题解)
- 【2013南京区域赛】部分题解 hdu4802—4812
- 团体程序设计天梯赛-习题集部分题解(大牛勿喷)
- 喵哈哈村的魔法考试 Round #11 (Div.2) 题解
- 百度之星第一场初赛部分题解