CodeforcesRound #231 (Div.2)

Source: Internet
Author: User
Problems # NameACountingSticksstandardinputoutput1s, second, 256MBx856CDominoesstandardinputoutput2s, 256MBx803DPhysicalEducationandBunsstandardinputoutput

Problems # Name A Counting Sticks standard input/output 1 s, 256 MB x2326 B Very Beautiful Number standard input/output 1 s, 256 MB x856 C Dominoes standard input/output 2 s, 256 MB x803 D Physical Education and Buns standard input/output

Problems

# Name
A

Counting Sticks

Standard input/output

1 s, 256 MB

X2326
B

Very Beautiful Number

Standard input/output

1 s, 256 MB

X856
C

Dominoes

Standard input/output

2 s, 256 MB

X803
D

Physical Education and Buns

Standard input/output

2 s, 256 MB

X234
E

Lightbulb for Minister

Standard input/output

1 s, 256 MB

X49

Question A: First, process the string and save the numbers at the three positions. Then, you can move them to determine whether the number is equal or the difference is 2.

Question B: enumerate the last digit, simulate pushing the number forward, and push it to the first digit to determine if it is the same as the number first enumerated.

Question C: greedy. 10 and 01 are actually the same. Therefore, first save the total number of 11, 10, and 01, and the number of 00. First put 11 from left to right. Then, place 10, 01 on the right to the left, and each line is placed alternately. The rest is 00.

D: after sorting from small to large, enumerate the tolerances d first. The first changed sequence A1 is 0, then, we can find the maximum and minimum values (which may be negative) that need to be shifted upwards. Then, the changed sequence can be regarded as a straight line with the slope k being d and B being A1, then, no matter whether this line is moved up or down, the maximum and minimum values must be the original two positions, so as long as the maximum and minimum values to be moved to the minimum and maximum values are as small as possible, that is, it is certainly the best to go to the center, for (up + down + 1)/2 (to be rounded up so + 1), and finally maintain the minimum value of ans.

Question D: there is another solution, which is to divide the answer into two parts, then judge and judge the method by enumerating the tolerances first, and maintain each upper and lower intervals in the O (n) method.

Code:

Question:

#include 
 
  #include 
  
   char c;int main() {    int num[3], s = 0;     memset(num, 0, sizeof(num));    while ((c = getchar()) != EOF && c != '\n') {        if (c == '+' || c == '=') s++;        else num[s]++;    }    if (num[0] - 1 + num[1] == num[2] + 1) {        if (num[0] == 1) num[1]--;        else if (num[1] == 1) num[0]--;        else if (num[0] != 1 && num[1] != 1) num[0]--;        num[2]++;    }    else if (num[0] + num[1] == num[2]) {        }    else if (num[0] + 1 + num[1] == num[2] - 1) {        if (num[2] == 1) {            printf("Impossible\n");            return 0;        }        num[2]--;        num[0]++;    }    else {        printf("Impossible\n");        return 0;    }    int i;    for (i = 0; i < num[0]; i++)        printf("|");    printf("+");    for (i = 0;i < num[1]; i++)        printf("|");    printf("=");    for (i = 0; i < num[2]; i++)        printf("|");    printf("\n");    return 0;}
  
 

Question B:
#include 
 
  #include 
  
   int p, x, ans[1000005];int main() {    scanf("%d%d", &p, &x);    int yu = 0;    for (int i = 0; i <= 9; i++) {        int s = i; int j; yu = 0;        for (j = 0; j < p; j++) {            ans[j] = s;            int ji = s * x + yu;            s = ji % 10;            yu = ji / 10;        }        if (s == i && j == p && ans[p - 1] != 0 && yu == 0) {            for (int j = p - 1; j >= 0; j--)                printf("%d", ans[j]);            printf("\n");            return 0;        }    }    printf("Impossible\n");    return 0;}
  
 

Question C:

#include 
 
  #include 
  
   int n, m, i, j;int num10, num00, num11;char str[10], ans[1005][1005][4];int main() {    num10 = num00 = num11 = 0;    scanf("%d%d", &n, &m);    for (i = 0; i < n; i++)        for (j = 0; j < m; j++) {            scanf("%s", str);            if (strcmp(str, "00") == 0)                num00++;            if (strcmp(str, "01") == 0 || strcmp(str, "10") == 0)                num10++;            if (strcmp(str, "11") == 0)                num11++;        }        for (i = 0; i < n; i++)        for (j = 0; j < m; j++)            strcpy(ans[i][j], "00");    i = 0; j = 0;    while (num11) {        strcpy(ans[i][j], "11");        j++;        if (j == m) {            j = 0;            i++;        }        num11--;    }    int jj = m - 1;    while (num10) {        strcpy(ans[i][jj], "10");        num10--;        if (jj == j) break;        jj--;    }    i++;    jj = m - 1;    while (num10) {        strcpy(ans[i][jj], "01");        num10--;        if (jj == j) break;        jj--;    }    int flag = 0; j--;    if (j == -1) {        j = m - 1;        i++;    }    while (num10) {        if (flag == 0)            strcpy(ans[i][j], "01");        else            strcpy(ans[i][j], "10");        j--;        if (j == -1) {            j = m - 1;            i++;            flag = 1 - flag;        }        num10--;    }    for (i = 0; i < n; i++) {        for (j = 0; j < m - 1; j++) {            printf("%s ", ans[i][j]);        }        printf("%s\n", ans[i][j]);    }    return 0;}
  
 

D Question 1:
#include 
 
  #include 
  
   #include #define INF 0x3f3f3f3f#define max(a,b) ((a)>(b)?(a):(b))#define min(a,b) ((a)<(b)?(a):(b))using namespace std;const int N = 1005;int n, num[N];void solve() {    int ans = INF, start, dd;    sort(num, num + n);    for (int d = 0; d <= 20000; d++) {        int up = -INF, down = INF;        for (int i = 0; i < n; i++) {            up = max(up, i * d - num[i]);            down = min(down, i * d - num[i]);        }        int res = (up - down + 1) / 2;        if (ans > res) {            ans = res; start = -up + res; dd = d;        }    }    printf("%d\n%d %d\n", ans, start, dd);}int main() {    scanf("%d", &n);    for (int i = 0; i < n; i++)        scanf("%d", &num[i]);    solve();    return 0;}
  
 

D Question 2:
#include 
 
  #include 
  
   #include #define INF 0x3f3f3f3f#define max(a,b) ((a)>(b)?(a):(b))#define min(a,b) ((a)<(b)?(a):(b))using namespace std;const int N = 1005;const int M = 10005;int n, num[N], start, dd;bool judge(int Max) {for (int d = 0; d <= 20000; d++) {int up = num[n - 1] + Max, down = num[n - 1] - Max;for (int i = n - 2; i >= 0; i--) {up = min(num[i] + Max, up - d);down = max(num[i] - Max, down - d);}if (down <= up) {start = down;dd = d;return true;}}return false;}void solve() {int l = 0, r = M;sort(num, num + n);while (l < r) {int mid = (l + r) / 2;if (judge(mid)) r = mid;else l = mid + 1;}printf("%d\n%d %d\n", l, start, dd);}int main() {scanf("%d", &n);for (int i = 0; i < n; i++)scanf("%d", &num[i]);solve();    return 0;}
  
 

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.