Codeforces Round #264 (Div. 2) [ABCDE]

Source: Internet
Author: User

Codeforces Round #264 (Div. 2) [ABCDE]
Codeforces Round #264 (Div. 2) [ABCDE]

ACM

Address: Codeforces Round #264 (Div. 2)

There were only two things in this case, TAT. C. Due to the fact that cin gave fst, D thought that the correct solution was over but the game was over...
How powerful is rating? QvQ...

A-Caisa and Sugar [simulation]

Question:
Caisa took the s dollar to the supermarket to buy sugar, there are n kinds of sugar, each is the xi dollar yi cent, the supermarket will not find the cent, but instead use sweet, of course, if you can use the dollar, try to use the dollar. He wants to get as many sweet as possible. You can get up to a few sweet questions. If you can't afford any sort of sugar, you can output-1.

Analysis:
It means the question is a little egg pain. Isn't sugar and sweet all candy...
Note:

    You can't just judge the sweet you can find for cents, but also whether you can afford it. Don't ignore what you can afford.

    Code:

    /**  Author:      illuz 
      
       *  Blog:        http://blog.csdn.net/hcbbt*  File:        A.cpp*  Create Date: 2014-08-30 15:41:45*  Descripton:   */#include 
       
        using namespace std;#define repf(i,a,b) for(int i=(a);i<=(b);i++)typedef long long ll;const int N = 110;int x[N], y[N];int n, s;int main() {int mmax = -1;scanf("%d%d", &n, &s);repf (i, 1, n) {scanf("%d%d", &x[i], &y[i]);if (x[i] < s) {mmax = max(mmax, (100 - y[i]) % 100);}if (x[i] == s && y[i] == 0) {mmax = max(mmax, 0);}}printf("%d\n", mmax);return 0;}
       
      


    B-Caisa and Pylons [greedy]

    Question:
    In a game, you must skip all the towers. The rules are as follows:

      Initially you are in Tower 0, and the height of the tower 0 is 0. You can only jump from I to Tower I + 1 and change +(h[i]-h[i+1])Life cannot be less than 0 at any time. You can spend money to buy a tower to increase the level of a tower.

      Ask the minimum number of towers required for customs clearance.

      Analysis:

      Read questions and be greedy.

      Code:

      /**  Author:      illuz 
         
          *  Blog:        http://blog.csdn.net/hcbbt*  File:        B.cpp*  Create Date: 2014-08-30 15:57:02*  Descripton:   */#include 
          
           using namespace std;#define repf(i,a,b) for(int i=(a);i<=(b);i++)typedef long long ll;const int N = 1e5 + 10;ll n, h[N];ll energy, cast;int main() {while (cin >> n) {energy = 0, cast = 0;repf (i, 1, n) {cin >> h[i];}repf (i, 0, n - 1) {energy += h[i] - h[i + 1];if (energy < 0) {cast += -energy;energy = 0;}}cout << cast << endl;}return 0;}
          
         


      C-Gargari and Bishops [preprocessing, black and white Games]

      Question:
      Here is a chessboard with some values on the grid. Then you have to select two places to place the pawns:

        A piece can walk along the diagonal line and eat any value on the lattice. No two pieces can be taken at the same time, that is, the value cannot be repeatedly eaten.

        Ask about the maximum value and position of the two pawns.

        Analysis:

        For rule 2, just like the black and white games, as long as the color is different, that is, (x + y) % 2 is different.

        The next step is to calculate the sum of values.
        The size of the checker is 2000*2000. If each checker is violent, the value that can be eaten by the pawns will time out.
        Obviously, the sum of values of (x, y) is equal to the diagonal and + diagonal and-value (I, j) to which it belongs.
        We only need to pre-process the sum of each diagonal and diagonal.
        We found that (x + y) The same grid belongs to the same diagonal, (x-y) The same belongs to the same diagonal. We can open two arrays to record the data. Since x-y has negative numbers, we can just add 2000 to them.

        In this way, when calculating the sum of values of a grid, we can simply calculate the diagonal and (x + y) diagonal and (x-y) diagonal.

        Code:

        /**  Author:      illuz 
            
             *  Blog:        http://blog.csdn.net/hcbbt*  File:        C.cpp*  Create Date: 2014-08-30 16:26:14*  Descripton:   */#include 
             
              using namespace std;#define repf(i,a,b) for(int i=(a);i<=(b);i++)typedef long long ll;const int N = 2010;int n;ll v;ll x[N*2], y[N*2], mp[N][N];pair
              
                A, B;ll am, bm;int main() {scanf("%d", &n);A.first = 1;A.second = 2;B.first = 1;B.second = 1;repf (i, 1, n) repf (j, 1, n) {scanf("%lld", &v);x[i + j] += v;y[i - j + N] += v;mp[i][j] = v;}repf (i, 1, n) repf (j, 1, n) {v = x[i + j] + y[i - j + N] - mp[i][j];if ((i + j) % 2) {if (am < v) {am = v;A.first = i;A.second = j;}} else {if (bm < v) {bm = v;B.first = i;B.second = j;}}}cout << am + bm << endl;cout << A.first << ' ' << A.second << ' ';cout << B.first << ' ' << B.second << endl;return 0;}
              
             
            


        D-Gargari and Permutations [multi-sequence LCS, DAG]

        Question:
        Obtain the longest common subsequence of k sequences with n length. (2 <= k <= 5)

        Analysis:
        The LCS of the first two sequences cannot be obtained, and then the result is followed by the following result.
        Because the LCS of the first two sequences is not unique.

        We pre-process (I, j). If there is a pos [I] <pos [j] for each sequence, it indicates that as LCS, I can be followed by j, and then in I, j is connected to an edge.
        In this way, it will be converted into a DAG, and you can find the longest path.

        Code:

        /**  Author:      illuz 
            
             *  Blog:        http://blog.csdn.net/hcbbt*  File:        D.cpp*  Create Date: 2014-08-30 17:06:04*  Descripton:   */#include 
             
              using namespace std;#define repf(i,a,b) for(int i=(a);i<=(b);i++)typedef long long ll;const int N = 1010;int a[6][N], vis[N];int n, k, v;int dp[N], mmax;vector
              
                G[N];bool check(int x, int y) {repf (i, 0, k - 1) {if (a[i][x] >= a[i][y])return 0;}return 1;}int dfs(int x, int d) {int ret = 0;if (vis[x])return vis[x];int sz = G[x].size();repf (i, 0, sz - 1) {ret = max(ret, dfs(G[x][i], d + 1));}return vis[x] = ret + 1;}int main() {while (cin >> n >> k) {memset(vis, 0, sizeof(vis));repf (i, 0, n)G[i].clear();repf (i, 0, k - 1) {repf (j, 1, n) {cin >> v;a[i][v] = j;}}repf (i, 1, n) {repf (j, 1, n) {if (check(i, j)) {G[i].push_back(j);}}}mmax = 0;repf (i, 1, n) {if (!vis[i])mmax = max(dfs(i, 0), mmax);}cout << mmax << endl;}return 0;}
              
             
            


        E-Caisa and Tree [brute force, non-positive solution]

        Question:
        A tree with a node value is provided. There are two operations:

          Ask the label of the node with the deepest depth and greater than 1 in the path from the root node to a node. Modify the value of a node.

          Analysis:

          I can't think of brute force as easy, but it can only indicate that the data is too weak...
          Dfs build, record the parent node of each node.
          During the query, find the correct node from the query node in a loop and then output the node.

          The data is too weak. For example, if the fruit tree is a long chain, gcd = 1 at the bottom and other nodes, and the last node is queried every time, the time-out will occur.
          I tried it just now. It seems that none of Solution can avoid TLE. It's all about violence.

          Wait for the official solution.

          Below is the data generator of the TLE data written in python:

          #!/usr/bin/python # by hcbbt 2014-08-30 17:30:33#n = 100000print n, nfor i in range(n - 1):    print 2,print 3for i in range(n - 1):    print i + 1, i + 2for i in range(n):    print 1, n


          Code:

          /**  Author:      illuz 
               
                *  Blog:        http://blog.csdn.net/hcbbt*  File:        E.cpp*  Create Date: 2014-08-30 19:20:17*  Descripton:  brute force, gcd*/#include 
                
                 using namespace std;#define repf(i,a,b) for(int i=(a);i<=(b);i++)typedef long long ll;const int N = 1e5 + 10;vector
                 
                   mp[N];int n, q, v[N], fa[N], x, y;void dfs(int x, int f) {fa[x] = f;int sz = mp[x].size();repf (i, 0, sz - 1) {if (mp[x][i] != f) {dfs(mp[x][i], x);}}}int main() {scanf("%d%d", &n, &q);repf (i, 1, n) {scanf("%d", &v[i]);}repf (i, 1, n - 1) {scanf("%d%d", &x, &y);mp[x].push_back(y);mp[y].push_back(x);}dfs(1, 0);repf (i, 1, q) {scanf("%d", &x);if (x == 1) {scanf("%d", &y);int i;for (i = fa[y]; i; i = fa[i])if (__gcd(v[i], v[y]) > 1)break;if (!i)printf("-1\n");elseprintf("%d\n", i);} else {scanf("%d %d", &x, &y);v[x] = y;}}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.