Codeforces Round #313 (Div. 2)

题目链接

这是后来补的题,感觉这次比较简单吧,以前都是做到第三道做不动了,这次做第五道超时做不懂了,貌似得用一些数学定理,而不是简单的dp。

A. Currency System in Geraldion

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?

Input

The first line contains number n (1 ≤ n ≤ 1000) — the number of values of the banknotes that used in Geraldion.

The second line contains n distinct space-separated numbers a1, a2, …, an (1 ≤ ai ≤ 106) — the values of the banknotes.

Output

Print a single line — the minimum unfortunate sum. If there are no unfortunate sums, print  - 1.

题意:给定一些数,每个数都有无数个,问这些数字想家组不成的最小的数。

分析:给定的数种有1则可组成任意的数,输出-1;没有则不能组成1,输出1。

Java代码:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        boolean ok = false;
        for (int i = 0; i < n; i++) {
            int x = cin.nextInt();
            if(x == 1) ok = true;
        }
        if(ok) System.out.println("-1");
        else System.out.println("1");
    }
}

B. Gerald is into Art

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?

Input

The first line contains two space-separated numbers a1 and b1 — the sides of the board. Next two lines contain numbers a2, b2, a3 and b3— the sides of the paintings. All numbers ai, bi in the input are integers and fit into the range from 1 to 1000.

Output

If the paintings can be placed on the wall, print “YES” (without the quotes), and if they cannot, print “NO” (without the quotes).

题意:给定一个大矩形和两个小矩形,问两个小举行能不能不重叠的放到大矩形里面。

分析:构造小矩形的摆放组合方式(4种),看大矩形能否容纳。

CPP代码:

 #include <bits/stdc++.h>
using namespace std;

int x, y, x1, y1, x2, y2;

bool in(int a, int b) {
    return (a <= x && b <= y) || (a <= y && b <= x);
}

bool judge() {
    int a, b;
    a = x1 + x2, b = max(y1, y2);
    if(in(a, b)) return true;

    swap(x1, y1);
    a = x1 + x2, b = max(y1, y2);
    if(in(a, b)) return true;

    swap(x2, y2);
    a = x1 + x2, b = max(y1, y2);
    if(in(a, b)) return true;

    swap(x1, y1);
    a = x1 + x2, b = max(y1, y2);
    if(in(a, b)) return true;

    return false;
}

int main() {
    cin >> x >> y >> x1 >> y1 >> x2 >> y2;
    if(judge()) puts("YES");
    else puts("NO");
    return 0;
}

C. Gerald’s Hexagon

Gerald got a very curious hexagon for his birthday. The boy found out that all the angles of the hexagon are equal to 120*. 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.

Input

The first and the single line of the input contains 6 space-separated integers a1, a2, a3, a4, a5 and a6 (1 ≤ ai ≤ 1000) — the lengths of the sides of the hexagons in centimeters in the clockwise order. It is guaranteed that the hexagon with the indicated properties and the exactly such sides exists.

Output

Print a single integer — the number of triangles with the sides of one 1 centimeter, into which the hexagon is split.

题意:给定一个每个角都是120度的六边形的六个边长,求能把这个六边形划分成几个边长为1的三角形。

分析:因为六条边的对边都是平行的,所以可以将任意给定形状的六边形划分成上中下三步分分别来求出,求个数的时候可以用:面积/边长为1的正三角形的面积,来求出。

CPP代码:

 

#include &lt;bits/stdc++.h&gt;
using namespace std;

int a, b, c, d, e, f;

int main() {
    cin &gt;&gt; a &gt;&gt; b &gt;&gt; c &gt;&gt; d &gt;&gt; e &gt;&gt; f;
    int res = 0;
    int x1 = min(b, f), y1 = a;
    res += x1*x1 + 2*x1*y1;
    // cout &lt;&lt; res &lt;&lt; endl;
    int x2 = min(e, c), y2 = d;
    res += x2*x2 + 2*x2*y2;
    // cout &lt;&lt; res &lt;&lt; endl;
    int x3 = max(b, f)-x1, y3 = y1+x1;
    res += 2 * x3 * y3;
    cout &lt;&lt; res &lt;&lt; endl;
    return 0;
}

D. Equivalent Strings

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!

Input

The first two lines of the input contain two strings given by the teacher. Each of them has the length from 1 to 200 000 and consists of lowercase English letters. The strings have the same length.

Output

Print “YES” (without the quotes), if these two strings are equivalent, and “NO” (without the quotes) otherwise.

题意:给定两个字符串,判断是否相等。相等的条件是要么每个字符串相等,要么各平分成两份后,对应相等或者交叉相等。条件是递归的。

分析:递归暴力判断即可,要注意当不能平分的时候不满足题意。

CPP代码:

 

#include &lt;bits/stdc++.h&gt;
using namespace std;

char s[200005];
char t[200005];

bool isEuqal(int a, int b, int len) {
    if(strncmp(s+a, t+b, len) == 0) return true;
    if(len % 2 == 1) return false;
    int l = len &gt;&gt; 1;
    if(isEuqal(a, b, l) &amp;&amp; isEuqal(a+l, b+l, l)) return true;
    if(isEuqal(a+l, b, l) &amp;&amp; isEuqal(a, b+l, l)) return true;
    return false;
}

int main() {
    scanf("%s %s", s, t);
    int len1 = strlen(s), len2 = strlen(t);
    if(len1 == len2 &amp;&amp; isEuqal(0, 0, len1)) puts("YES");
    else puts("NO");
    return 0;
}

1 条评论

欢迎留言