Codeforces Round #323 (Div. 2) 解题报告

来源:互联网 发布:qq超市数据清零 编辑:程序博客网 时间:2024/05/15 12:39


CodeforcesRound #323 (Div. 2)解题报告

 

  1. Asphalting Roads

     

    题目分类:朴素模拟

     

    题目描述:一座城市被N(1 ≤ N ≤ 50)条水平路线和N条垂直路线分割出N*N个交叉口。有一个施工队将对城市路线进行施工N*N天。他们每天来到一个交叉口,如果这个交叉口对应的水平路线和垂直路线都没有被施工过,他们将对其施工,否则不施工。问施工队第几天施过工。

     

    解题思路:数据很小,只需要开两个一维数组记录所有水平和垂直路线是否被施工过就行。

     

    AC代码:

    #include<iostream>

    #include<cstdio>

    #include<cstring>

    #include<queue>

    #include<stack>

    #include<map>

    #include<algorithm>

    #include<cstdlib>

    #include<cmath>

    #definesqr(x)(x)*(x)

     

    usingnamespace std;

    typedeflonglong llg;

    constint inf = 0x3f3f3f3f;

    constdouble eps = 1e-11;

    constint N = 1100;

    constint M = 1e6+10;

    int a[N],b[N];

     

    intmain()

    {

       int n,m;

       while(~scanf("%d",&n))

       {

           for(int i=1;i<=n;i++)

               scanf("%d",&a[i]);

           int ans= 0;

           int tot= 0;

           memset(b,0,sizeof(b));

           for(int h=1;h<=n;h++)

           {

               for(int i=1;i<=n;i++)

               {

                   if(!b[i]&& a[i]<=tot)

                   {

                       tot++;

                       b[i]= 1;

                   }

               }

               if(tot>=n)break;

               ans ++;

               for(int i=n;i;i--)

               {

                   if(!b[i]&& a[i]<=tot)

                   {

                       tot++;

                       b[i]= 1;

                   }

               }

     

               if(tot>=n)break;

               ans++;

           }

           printf("%d\n",ans);

       }

       return0;

    }

    原题描述:

     

    A.Asphalting Roads

    timelimit per test

    1second

    memorylimit per test

    256megabytes

    input

    standardinput

    output

    standardoutput

    City X consists ofn vertical andn horizontal infinite roads, formingn × n intersections. Roads (both vertical andhorizontal) are numbered from 1 ton, and the intersections are indicated by the numbers of the roads thatform them.

    Sand roads have long been recognized out ofdate, so the decision was made to asphalt them. To do this, a team of workerswas hired and a schedule of work was made, according to which the intersectionsshould be asphalted.

    Road repairs are planned forn2 days. On the i-th day of the team arrives at thei-th intersection in the list and ifnoneof the two roads that form the intersection were already asphalted they asphaltboth roads. Otherwise, the team leaves the intersection, without doing anythingwith the roads.

    According to the schedule of road workstell in which days at least one road will be asphalted.

    Input

    The first line contains integern (1 ≤ n ≤ 50) — the number of vertical and horizontalroads in the city.

    Nextn2 lines contain the order of intersectionsin the schedule. The i-th of them contains two numbershi, vi (1 ≤ hi, vi ≤ n), separated by a space, and meaning thatthe intersection that goes i-th in the timetable is at the intersectionof the hi-th horizontal and vi-th vertical roads. It is guaranteed thatall the intersections in the timetable are distinct.

    Output

    In thesingle line print the numbers of the days when road works will be in progressin ascending order. The days are numbered starting from1.

    Sampletest(s)

    Input

    2
    1 1
    1 2
    2 1
    2 2

    Output

    1 4

    Input

    1
    1 1

    Output

    1

    Note

    In the sample the brigade acts like that:

1.   On thefirst day the brigade comes to the intersection of the 1-st horizontal and the1-st vertical road. As none of them has been asphalted, the workers asphalt the1-st vertical and the 1-st horizontal road;

2.   On thesecond day the brigade of the workers comes to the intersection of the 1-sthorizontal and the 2-nd vertical road. The 2-nd vertical road hasn't beenasphalted, but as the 1-st horizontal road has been asphalted on the first day,the workers leave and do not asphalt anything;

3.   On thethird day the brigade of the workers come to the intersection of the 2-ndhorizontal and the 1-st vertical road. The 2-nd horizontal road hasn't beenasphalted but as the 1-st vertical road has been asphalted on the first day,the workers leave and do not asphalt anything;

4.   On thefourth day the brigade come to the intersection formed by the intersection ofthe 2-nd horizontal and 2-nd vertical road. As none of them has been asphalted,the workers asphalt the 2-nd vertical and the 2-nd horizontal road.

 

 

  1. Robot's Task

     

    题目分类:贪心策略

     

    题目描述:在一条直线上有n (1 ≤ n ≤ 1000)台计算机,依次标号为1,2,3,……,每台计算机分别有a1, a2, ..., an (0 ≤ ai < n)的信息收集难度。一个机器人负责收集每台计算机的信息。他从1号计算机开始,初试收集0个信息,每收集一台计算机的信息机器人就可以升一级,机器人只能收集到信息收集难度小于等于自己等级的计算机信息,一台计算机的信息只能被收集一次。机器人在放置计算机的直线上来回行走以便收集信息,但是机器人转身十分困难,问机器人最少转身多少次可以收集完所有计算机的信息。输出保证有解。

     

    解题思路:询问最少的转身次数。贪心策略是每次走到末尾转身,保证每次转身前都可以尽可能多的收集信息。最多转身n次必能收集完所有信息,所以O(N*N)的循环可以解决本题。

     

     

    AC代码:

    #include<iostream>

    #include<cstdio>

    #include<cstring>

    #include<queue>

    #include<stack>

    #include<map>

    #include<algorithm>

    #include<cstdlib>

    #include<cmath>

    #definesqr(x)(x)*(x)

     

    usingnamespace std;

    typedeflonglong llg;

    constint inf = 0x3f3f3f3f;

    constdouble eps = 1e-11;

    constint N = 1100;

    constint M = 1e6+10;

    int a[N],b[N];

     

    intmain()

    {

       int n,m;

       while(~scanf("%d",&n))

       {

           for(int i=1;i<=n;i++)

               scanf("%d",&a[i]);

           int ans= 0;

           int tot= 0;

           memset(b,0,sizeof(b));

           for(int h=1;h<=n;h++)

           {

               for(int i=1;i<=n;i++)

               {

                   if(!b[i]&& a[i]<=tot)

                   {

                       tot++;

                       b[i]= 1;

                   }

               }

               if(tot>=n)break;

               ans ++;

               for(int i=n;i;i--)

               {

                   if(!b[i]&& a[i]<=tot)

                   {

                       tot++;

                       b[i]= 1;

                   }

               }

     

               if(tot>=n)break;

               ans++;

           }

           printf("%d\n",ans);

       }

       return0;

    }

     

    原题描述:

    B.Robot's Task

    timelimit per test

    1second

    memorylimit per test

    256megabytes

    input

    standardinput

    output

    standardoutput

    Robot Doc is located in the hall, withn computers stand in a line, numbered fromleft to right from1 ton. Each computer containsexactly onepiece of information, each of which Doc wants to get eventually. The computersare equipped with a security system, so to crack thei-th of them, the robot needs to collect atleastai any pieces of information from the othercomputers. Doc can hack the computer only if he is right next to it.

    The robot is assembled using moderntechnologies and can move along the line of computers in either of the twopossible directions, but the change of direction requires a large amount ofresources from Doc. Tell the minimum number of changes of direction, which therobot will have to make to collect alln parts of information if initially it isnext to computer with number1.

    It is guaranteed that there exists at least one sequence ofthe robot's actions, which leads to the collection of all information.Initially Doc doesn't have any pieces of information.

    Input

    The first line contains numbern (1 ≤ n ≤ 1000). The second line contains n non-negative integersa1, a2, ..., an (0 ≤ ai < n), separated by a space. It is guaranteedthat there exists a way for robot to collect all pieces of the information.

    Output

    Printa single number — the minimum number of changes in direction that the robotwill have to make in order to collect alln parts of information.

    Sampletest(s)

    Input

    3
    0 2 0

    Output

    1

    Input

    5
    4 2 3 0 1

    Output

    3

    Input

    7
    0 3 1 0 5 2 6

    Output

    2

    Note

    In the first sample you can assemble allthe pieces of information in the optimal manner by assembling first the pieceof information in the first computer, then in the third one, then changedirection and move to the second one, and then, having 2 pieces of information,collect the last piece.

    In the second sample to collect all thepieces of information in the optimal manner, Doc can go to the fourth computerand get the piece of information, then go to the fifth computer with one pieceand get another one, then go to the second computer in the same manner, then tothe third one and finally, to the first one. Changes of direction will takeplace before moving from the fifth to the second computer, then from the secondto the third computer, then from the third to the first computer.

    In thethird sample the optimal order of collecting parts from computers can look likethat: 1->3->4->6->2->5->7.

     

     

     

  2. GCD Table

     

    题目分类:贪心策略

     

    题目描述:定义n*n(0<n<=500)的数阵满足,打乱顺序给你这n*n个数字,让你找出在原数阵主对角线上的n个数字gii(0<i<=n).输出一种满足要求的解就行。

     

    解题思路:因为两个数字的最大公因数一定小于这两个数,所以n*n中最大的两个数字一定在主对角线上,然后排除掉他们的最大公因数后找剩余数字最大的数字,继续排除已知数字的最大公因数,直到找出n个数字为止。这种构造方法的有效性可以证明贪心策略的正确性。

     

     

    AC代码:

    #include <iostream>

    #include <cstdio>

    #include <cstring>

    #include <cstdlib>

    #include <algorithm>

    #include <map>

    using namespace std;

    map<int,int > ma;

    int gcd(int a,int b)

    {

        if(b==0) return a;

        else return gcd(b,a%b);

    }

    const int N = 510;

    int ans[N];

    int a[N*N];

    int main()

    {

        int n;

        while(~scanf("%d",&n))

        {

            ma.clear();

            for(int i=0;i<n*n;i++)

            {

               scanf("%d",&a[i]);

               ma[a[i] ]++;

            }

            sort(a,a+n*n);

            int top=0;

     

            for(int i=n*n-1;i>=0;i--)

            {

               if(top==n) break;

               if(ma[a[i]])

               {

                   for(int j=0;j<top;j++)

                   {

                       ma[gcd(ans[j],a[i]) ] -= 2;

                   }

                   ans[top++] = a[i];

                   ma[a[i] ]--;

               }

            }

            for(int i=0;i<top;i++)

               printf("%d ",ans[i]);

        }

        return 0;

    }

     

    原题描述:

    C. GCD Table

    time limit per test

    2 seconds

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    The GCD table G of size n × n for an array of positive integers a of length n is defined by formula

    Let us remind you that the greatest commondivisor (GCD) of two positive integers x and y is the greatest integer that isdivisor of both xand y, it is denoted as . For example,for array a = {4, 3, 6, 2} of length 4 the GCD table will lookas follows:

    Given all the numbers of the GCD table G, restore array a.

    Input

    The first line contains number n (1 ≤ n ≤ 500) — the length of array a. The second line contains n2 space-separated numbers — theelements of the GCD table of G for array a.

    All the numbers in the table are positiveintegers, not exceeding 109. Note that the elements are given in anarbitrary order. It is guaranteed that the set of the input data corresponds tosome array a.

    Output

    In the single line print n positive integers — the elements ofarray a. If there are multiple possible solutions, you are allowed to print anyof them.

    Sample test(s)

    input

    4
    2 1 2 3 4 3 2 6 1 1 2 2 1 2 3 2

    output

    4 3 6 2

    input

    1
    42

    output

    42

    input

    2
    1 1 1 1

    output

    1 1

     

     

  3. Once Again...

     

    题目分类:动态规划加贪心

     

    题目描述:经典动规题目的变形。长度为n的数字序列重复T次以后的最长不下降子序列有多长(1 ≤ n ≤ 100,1 ≤ T ≤ 107).

     

    解题思路:直接动规即便是ON*T*logN*T))的算法也显然超时。观察发现n并不大,求n*n的最长不下降子序列很容易,如果T<=N裸做即可。如果T>N的话只做n*n的最长不下降子序列,此时在原序列中重复最多的数字必然出现在n*n的最长不下降子序列中,剩余的(T-n)次序列重复自然是将重复出现次数最多的数字全放进最长不下降子序列中最优。将其总长度加到n*n的最长不下降子序列的长度中去即可。于是利用贪心策略将原题转化成为经典的动态规划问题。

     

    AC代码:

    #include<iostream>

    #include<cstdio>

    #include<cstring>

    #include<queue>

    #include<stack>

    #include<map>

    #include<algorithm>

    #include<cstdlib>

    #include<cmath>

    #definesqr(x)(x)*(x)

     

    usingnamespace std;

    typedeflonglong llg;

    constint inf = 0x3f3f3f3f;

    constdouble eps = 1e-11;

    constint N = 1e5+10;

    constint M = 1e6+10;

    int a[N],b[N];

    int c[N];

    intmain()

    {

       int n,m;

       while(~scanf("%d",&n))

       {

           scanf("%d",&m);

           memset(c,0,sizeof(c));

           int ma= 0;

           for(int i=1;i<=n;i++)

           {

               scanf("%d",&a[i]);

               c[a[i]]++;

               ma = max(ma,c[a[i]]);

           }

           int top=0,ans= 0;

           if(m>n)

           {

               ans =(m-n)*ma;

               m = n;

           }

           for(int i=n+1;i<=n*m;i++) a[i] = a[i-n];

           b[0]= 0;

           for(int i=1;i<=n*m;i++)

           {

               if(a[i]>=b[top])

               {

                   b[++top]= a[i];

               }else

               {

                   b[top+1]= 333;

                   int y=upper_bound(b,b+top+1,a[i])- b;

                   b[y]= a[i];

               }

           }

           printf("%d\n",top+ans);

       }

       return0;

    }

     

    原题描述:

    D.Once Again...

    timelimit per test

    1second

    memorylimit per test

    256megabytes

    input

    standardinput

    output

    standardoutput

    You are given an array of positive integersa1, a2, ..., an × T of length n × T. We know that for any i > n it is true that ai = ai - n. Find the length of the longestnon-decreasing sequence of the given array.

    Input

    The first line contains two space-separatedintegers:n,T (1 ≤ n ≤ 100,1 ≤ T ≤ 107). The second line contains n space-separated integersa1, a2, ..., an (1 ≤ ai ≤ 300).

    Output

    Printa single number — the length of a sought sequence.

    Sampletest(s)

    Input

    4 3
    3 1 4 2

    Output

    5

    Note

    Thearray given in the sample looks like that: 3,1, 4, 2, 3,1, 4, 2, 3, 1,4, 2. The elements in bold form the largestnon-decreasing subsequence.

     

     

     

    备注:

    Codefoces支持原创题目,题目新颖启发性强,题目难度阶梯明显,适合从低到高各种水平的选手挑战。题目描述全英,还有助于提升英文阅读水平。

0 0