! Codeforces 399C Cards-Number Theory & amp; greedy-(violent search), codeforcescards-

Source: Internet
Author: User

! Codeforces 399C Cards-Number Theory & greedy-(violent search), codeforcescards-

There are two types of characters x and o. Calculate the square of the number of the same consecutive characters, and then subtract the sum of the squares of x from the sum of the squares of o to obtain the maximum result, and output the corresponding sequence.

Analysis:

Number theory knowledge: To make o1 ^ 2 + o2 ^ 2 + ..... to be as big as possible, we need to make o try to make it as long as possible; To make x1 ^ 2 + x2 ^ 2 + ..... as small as possible, we need to distribute them as much as possible. Therefore, we use the same way for each score.

Based on the above theory, the number of blocks of o and x is enumerated and the results are constantly updated. The length of each piece can be obtained by using the above theory and the number of parts.

Error cause:

1. TLE: the data type of the cyclic variable is incorrect. Here, a and B are long, so the cyclic variable must be set to long.

2. WA: It's still a long problem. It was changed but not carefully. The % d used for Input and Output

Code:

# Include <iostream> # include <cstdio> # include <algorithm> # include <cstring> // # define min (a, B) a <B? A: B // use define directly after testing. It is not the cause of the Error. # define INF 1000000007 using namespace std; long a, B; long mi; long min (long a, long B) {return a <B? A: B;} int main () {while (cin> a> B) {if (a = 0) {cout <"-" <B * B <endl; for (int I = 0; I <B; I ++) cout <"x "; cout <endl;} else if (B <= 1) {cout <a * a-B * B <endl; for (int I = 0; I <; I ++) cout <"o"; for (int I = 0; I <B; I ++) cout <"x"; cout <endl ;} else {long ans =-(a + B) * (a + B); long o, x; mi = min (a, B ); for (long I = 1; I <= mi; I ++) {long a1 = (I-1) + (a-(I-1 )) * (a-(I-1); for (long j = I-1; j <= I + 1; j ++) {if (j> 0 & j <= B) {long b1 = (B/j) * (j-B % j) + (B/j + 1) * (B/j + 1) * (B % j); long s = a1-b1; if (ans <s) {ans = s; o = I, x = j ;}}} cout <ans <endl; if (o> = x) {for (long I = 1; I <= o; I ++) {long len1, len2; if (I = 1) len1 = a-(O-1); else len1 = 1; if (I <= B % x) len2 = B/x + 1; else len2 = B/x; for (long j = 0; j <len1; j ++) cout <"o"; if (I <= x) for (long j = 0; j <len2; j ++) cout <"x ";} cout <endl ;}else {for (long I = 1; I <= x; I ++) {long len1, len2; if (I <= B % x) len2 = B/x + 1; else len2 = B/x; if (I = 1) len1 = a-(O-1 ); else len1 = 1; for (long j = 0; j <len2; j ++) cout <"x"; if (I <= o) for (long j = 0; j <len1; j ++) cout <"o" ;}cout <endl ;}}}}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.