• Codeforces Round #369 (Div. 2) A B 暴力 模拟


    A. Bus to Udayland
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    ZS the Coder and Chris the Baboon are travelling to Udayland! To get there, they have to get on the special IOI bus. The IOI bus has nrows of seats. There are 4 seats in each row, and the seats are separated into pairs by a walkway. When ZS and Chris came, some places in the bus was already occupied.

    ZS and Chris are good friends. They insist to get a pair of neighbouring empty seats. Two seats are considered neighbouring if they are in the same row and in the same pair. Given the configuration of the bus, can you help ZS and Chris determine where they should sit?

    Input

    The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the number of rows of seats in the bus.

    Then, n lines follow. Each line contains exactly 5 characters, the first two of them denote the first pair of seats in the row, the third character denotes the walkway (it always equals '|') and the last two of them denote the second pair of seats in the row.

    Each character, except the walkway, equals to 'O' or to 'X'. 'O' denotes an empty seat, 'X' denotes an occupied seat. See the sample cases for more details.

    Output

    If it is possible for Chris and ZS to sit at neighbouring empty seats, print "YES" (without quotes) in the first line. In the next n lines print the bus configuration, where the characters in the pair of seats for Chris and ZS is changed with characters '+'. Thus the configuration should differ from the input one by exactly two charaters (they should be equal to 'O' in the input and to '+' in the output).

    If there is no pair of seats for Chris and ZS, print "NO" (without quotes) in a single line.

    If there are multiple solutions, you may print any of them.

    Examples
    input
    6
    OO|OX
    XO|XX
    OX|OO
    XX|OX
    OO|OO
    OO|XX
    output
    YES
    ++|OX
    XO|XX
    OX|OO
    XX|OX
    OO|OO
    OO|XX
    input
    4
    XO|OX
    XO|XX
    OX|OX
    XX|OX
    output
    NO
    input
    5
    XX|XX
    XX|XX
    XO|OX
    XO|OO
    OX|XO
    output
    YES
    XX|XX
    XX|XX
    XO|OX
    XO|++
    OX|XO
    Note

    Note that the following is an incorrect configuration for the first sample case because the seats must be in the same pair.

    O+|+X

    XO|XX

    OX|OO

    XX|OX

    OO|OO

    OO|XX

    题意:n排座位 每排两对座位  X代表已经被占了 现在两个人找座位 座位必须是某一对

    若能找到输出YES 并用+标注找到的座位 否则输出NO

    题解:模拟暴力

     1 /******************************
     2 code by drizzle
     3 blog: www.cnblogs.com/hsd-/
     4 ^ ^    ^ ^
     5  O      O
     6 ******************************/
     7 #include<bits/stdc++.h>
     8 #include<iostream>
     9 #include<cstring>
    10 #include<cstdio>
    11 #include<map>
    12 #include<algorithm>
    13 #include<queue>
    14 #define ll __int64
    15 using namespace std;
    16 int n;
    17 char a[1005][10];
    18 int main()
    19 {
    20     scanf("%d",&n);
    21     int flag=1;
    22     for(int i=1; i<=n; i++)
    23     {
    24         getchar();
    25         for(int j=1; j<=5; j++)
    26         {
    27             scanf("%c",&a[i][j]);
    28             if(flag)
    29             {
    30                 if(j==2)
    31                 {
    32                     if(a[i][1]=='O'&&a[i][2]=='O')
    33                     {
    34                         a[i][1]='+';
    35                         a[i][2]='+';
    36                         flag=0;
    37                     }
    38                 }
    39             }
    40             if(flag)
    41             {
    42                 if(j==5)
    43                 {
    44                     if(a[i][4]=='O'&&a[i][5]=='O')
    45                     {
    46                         a[i][4]='+';
    47                         a[i][5]='+';
    48                         flag=0;
    49                     }
    50                 }
    51             }
    52         }
    53     }
    54     if(flag==0)
    55     {
    56         printf("YES
    ");
    57         for(int i=1; i<=n; i++)
    58         {
    59             for(int j=1; j<=5; j++)
    60                 printf("%c",a[i][j]);
    61             printf("
    ");
    62         }
    63     }
    64     else
    65         printf("NO
    ");
    66     return 0;
    67 }
    B. Chris and Magic Square
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × n magic grid on the entrance which is filled with integers. Chris noticed that exactly one of the cells in the grid is empty, and to enter Udayland, they need to fill a positive integer into the empty cell.

    Chris tried filling in random numbers but it didn't work. ZS the Coder realizes that they need to fill in a positive integer such that the numbers in the grid form a magic square. This means that he has to fill in a positive integer so that the sum of the numbers in each row of the grid (), each column of the grid (), and the two long diagonals of the grid (the main diagonal —  and the secondary diagonal — ) are equal.

    Chris doesn't know what number to fill in. Can you help Chris find the correct positive integer to fill in or determine that it is impossible?

    Input

    The first line of the input contains a single integer n (1 ≤ n ≤ 500) — the number of rows and columns of the magic grid.

    n lines follow, each of them contains n integers. The j-th number in the i-th of them denotes ai, j (1 ≤ ai, j ≤ 109 or ai, j = 0), the number in the i-th row and j-th column of the magic grid. If the corresponding cell is empty, ai, j will be equal to 0. Otherwise, ai, j is positive.

    It is guaranteed that there is exactly one pair of integers i, j (1 ≤ i, j ≤ n) such that ai, j = 0.

    Output

    Output a single integer, the positive integer x (1 ≤ x ≤ 1018) that should be filled in the empty cell so that the whole grid becomes a magic square. If such positive integer x does not exist, output  - 1 instead.

    If there are multiple solutions, you may print any of them.

    Examples
    input
    3
    4 0 2
    3 5 7
    8 1 6
    output
    9
    input
    4
    1 1 1 1
    1 1 0 1
    1 1 1 1
    1 1 1 1
    output
    1
    input
    4
    1 1 1 1
    1 1 0 1
    1 1 2 1
    1 1 1 1
    output
    -1
    Note

    In the first sample case, we can fill in 9 into the empty cell to make the resulting grid a magic square. Indeed,

    The sum of numbers in each row is:

    4 + 9 + 2 = 3 + 5 + 7 = 8 + 1 + 6 = 15.

    The sum of numbers in each column is:

    4 + 3 + 8 = 9 + 5 + 1 = 2 + 7 + 6 = 15.

    The sum of numbers in the two diagonals is:

    4 + 5 + 6 = 2 + 5 + 8 = 15.

    In the third sample case, it is impossible to fill a number in the empty square such that the resulting grid is a magic square.

     题意:一个n*n的矩阵 在0的位置上(只有一个零)填写一个正整数使得这个矩阵中每一行每一列以及两个对角线的和都相等

    输出这个数 不存在则输出-1

     题解:暴力 对于这个矩阵求出每一行每一列以及两个对角线的和 判断这个和的值有多少种

    对于只有两种的 因为只能填写正整数 按要求比较大小输出值差值  特判 n=1

     1 /******************************
     2 code by drizzle
     3 blog: www.cnblogs.com/hsd-/
     4 ^ ^    ^ ^
     5  O      O
     6 ******************************/
     7 #include<bits/stdc++.h>
     8 #include<iostream>
     9 #include<cstring>
    10 #include<cstdio>
    11 #include<map>
    12 #include<algorithm>
    13 #include<queue>
    14 #define ll __int64
    15 using namespace std;
    16 int n;
    17 ll a[505][505];
    18 ll r[505],l[505];
    19 ll s,d;
    20 ll ans[1100];
    21 ll  xx,yy;
    22 map<ll,int> mp;
    23 int main()
    24 {
    25     scanf("%d",&n);
    26     s=0;
    27     d=0;
    28     mp.clear();
    29     memset(r,0,sizeof(r));
    30     memset(l,0,sizeof(l));
    31     for(int i=1;i<=n;i++)
    32     {
    33         for(int j=1;j<=n;j++)
    34         {
    35             scanf("%I64d",&a[i][j]);
    36             if(a[i][j]==0)
    37             {
    38                 xx=i;
    39                 yy=j;
    40             }
    41             r[i]+=a[i][j];
    42             l[j]+=a[i][j];
    43             if(i==j)
    44                 s+=a[i][j];
    45             if((i+j)==(n+1))
    46                 d+=a[i][j];
    47         }
    48     }if(n==1)
    49     {
    50         printf("1
    ");
    51         return 0;
    52     }
    53     int jishu=0;
    54     for(int i=1;i<=n;i++)
    55     {
    56         if(mp[r[i]]==0)
    57             ans[jishu++]=r[i];
    58         mp[r[i]]++;
    59     }
    60     for(int i=1;i<=n;i++)
    61     {
    62         if(mp[l[i]]==0)
    63             ans[jishu++]=l[i];
    64         mp[l[i]]++;
    65     }
    66     if(mp[s]==0)
    67             ans[jishu++]=s;
    68         mp[s]++;
    69     if(mp[d]==0)
    70             ans[jishu++]=d;
    71         mp[d]++;
    72     int s1=2;
    73     if(xx==yy)
    74         s1++;
    75     if((xx+yy)==(n+1))
    76         s1++;
    77     if(jishu!=2)
    78         printf("-1
    ");
    79     else{
    80         if(mp[ans[0]]==s1&&ans[0]<ans[1])
    81          {
    82              printf("%I64d
    ",ans[1]-ans[0]);
    83              return 0;
    84          }
    85          if(mp[ans[1]]==s1&&ans[1]<ans[0])
    86          {
    87              printf("%I64d
    ",ans[0]-ans[1]);
    88              return 0;
    89          }
    90          printf("-1
    ");
    91     }
    92     return 0;
    93 }
  • 相关阅读:
    Python 多线程学习(转)
    自己使用python webob,paste.deploy,wsgi总结
    Python中*args 和**kwargs的用法
    python 数字和字符串转换问题
    python socket编程
    深入解读Quartz的原理
    解决get方法传递URL参数中文乱码和解决tomcat下中文乱码问题
    Tomcat的Manager显示403 Access Denied
    mysql5.6 linux下安装笔记
    Quartz应用与集群原理分析
  • 原文地址:https://www.cnblogs.com/hsd-/p/5843765.html
Copyright © 2020-2023  润新知