【Codeforces Round #313 (Div. 2)】A B C D--solutions

来源:互联网 发布:淘宝商学院网址 编辑:程序博客网 时间:2024/05/15 00:43
A. Currency System in Geraldion
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the system is not perfect and sometimes it happens that Geraldionians cannot express a certain sum of money with any set of banknotes. Of course, they can use any number of banknotes of each value. Such sum is called unfortunate. Gerald wondered: what is the minimumunfortunate sum?

水题,如果输入有1就是-1,否则就是1。

#include <iostream>#include <cstdio>#include <algorithm>using namespace std;int n,a[1005];int main(){    scanf("%d",&n);    for (int i=0;i<n;i++) scanf("%d",&a[i]);    sort(a,a+n);    if (a[0]!=1) cout <<1<<endl;    else cout <<-1<<endl;    return 0;}


B. Gerald is into Art
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Gerald bought two very rare paintings at the Sotheby's auction and he now wants to hang them on the wall. For that he bought a special board to attach it to the wall and place the paintings on the board. The board has shape of an a1 × b1 rectangle, the paintings have shape of a a2 × b2 and a3 × b3 rectangles.

Since the paintings are painted in the style of abstract art, it does not matter exactly how they will be rotated, but still, one side of both the board, and each of the paintings must be parallel to the floor. The paintings can touch each other and the edges of the board, but can not overlap or go beyond the edge of the board. Gerald asks whether it is possible to place the paintings on the board, or is the board he bought not large enough?

B题就是枚举每一种情况,存在合法的就是YES,否则就是NO。

#include <iostream>#include <cstdio>#include <cstdlib>using namespace std;int a[5],b[5];int main(){    for (int i=1;i<=3;i++)    scanf("%d%d",&a[i],&b[i]);    if (a[2]+b[3]<=a[1] && max(b[2],a[3])<=b[1]) cout <<"YES"<<endl;    else if (b[2]+a[3]<=b[1] && max(a[2],b[3])<=a[1]) cout <<"YES"<<endl;    else if (max(a[2],a[3])<=a[1] && b[2]+b[3]<=b[1]) cout <<"YES"<<endl;    else if (a[2]+a[3]<=a[1] && max(b[2],b[3])<=b[1]) cout <<"YES"<<endl;    else if (a[2]+b[3]<=b[1] && max(a[3],b[2])<=a[1]) cout <<"YES"<<endl;    else if (b[2]+a[3]<=a[1] && max(a[2],b[3])<=b[1]) cout <<"YES"<<endl;    else if (a[2]+a[3]<=b[1] && max(b[2],b[3])<=a[1]) cout <<"YES"<<endl;    else if (b[2]+b[3]<=a[1] && max(a[2],a[3])<=b[1]) cout <<"YES"<<endl;    else cout <<"NO"<<endl;    return 0;}


C. Gerald's Hexagon
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Gerald got a very curious hexagon for his birthday. The boy found out that all the angles of the hexagon are equal to . Then he measured the length of its sides, and found that each of them is equal to an integer number of centimeters. There the properties of the hexagon ended and Gerald decided to draw on it.

He painted a few lines, parallel to the sides of the hexagon. The lines split the hexagon into regular triangles with sides of 1 centimeter. Now Gerald wonders how many triangles he has got. But there were so many of them that Gerald lost the track of his counting. Help the boy count the triangles.

C题其实是一道数学题,可以把题目中的这个六边形分割成两个等腰梯形和中间一个平行四边形,这样就可以分别算出每个图形中所含的三角形数,最后相加就行了。

#include <iostream>#include <cstdio>#include <algorithm>using namespace std;int a[10];int main(){    for (int i=0;i<6;i++) scanf("%d",&a[i]);    if (a[0]==a[4]) cout <<a[0]*(a[0]+2*a[5])+a[1]*(a[1]+2*a[2])<<endl;    else if (a[4]>a[0]) cout <<a[0]*(a[0]+2*a[5])+a[3]*(a[3]+2*a[2])+(a[0]+a[5])*(a[4]-a[0])*2<<endl;    else if (a[4]<a[0]) cout <<a[4]*(a[4]+2*a[5])+a[1]*(a[1]+2*a[2])+(a[3]-a[1])*(a[1]+a[2])*2<<endl;    return 0;}

D. Equivalent Strings
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are calledequivalent in one of the two cases:

  1. They are equal.
  2. If we split string a into two halves of the same size a1 and a2, and string b into two halves of the same size b1 and b2, then one of the following is correct:
    1. a1 is equivalent to b1, and a2 is equivalent to b2
    2. a1 is equivalent to b2, and a2 is equivalent to b1

As a home task, the teacher gave two strings to his students and asked to determine if they are equivalent.

Gerald has already completed this home task. Now it's your turn!


这一题要注意当两个序列长度不等时,两序列一定不相等。而且题目加了一条说当两个序列长度为奇数时,必须两个序列完全相等才说两个序列相等。(一开始没看到...)注意到这两点在递归求解就能过了。

递归竟然不会超时0.0

#include <iostream>#include <cstdio>#include <cstring>using namespace std;char s1[200005],s2[200005];int com_p(int l1,int r1,int l2,int r2){    if (r1-l1!=r2-l2) return 0;    if ((r1-l1+1)%2!=0)    {        for (int i=l1,j=l2;i<=r1;i++,j++) if (s1[i-1]!=s2[j-1]) return 0;;        return 1;    }    int t1=(l1+r1)/2,t2=(l2+r2)/2;    if (l1==r1 && s1[l1-1]==s2[l2-1]) return 1;    else if (l1==r1) return 0;    else return ((com_p(l1,t1,l2,t2) && com_p(t1+1,r1,t2+1,r2))                 || (com_p(l1,t1,t2+1,r2) && com_p(t1+1,r1,l2,t2)) );}int main(){    scanf("%s",&s1);    scanf("%s",&s2);    int l=strlen(s1);    if (com_p(1,l,1,l)) cout <<"YES"<<endl;    else cout <<"NO"<<endl;    return 0;}



0 0
原创粉丝点击