zoukankan      html  css  js  c++  java
  • acm集训1


     
    C - C

    Crawling in process...Crawling failedTime Limit:500MS    Memory Limit:4096KB     64bit IO Format:%I64d & %I64u

    Description

    There is a checkered field of sizeN x N cells (1 ЈN Ј 3). Each cell designates the territory of a state (i.e.N2 states). Each state has an army. Let A [i, j] be the number of soldiers in the state which is located on i-th line and on j-th column of the checkered field (1£i£N, 1£j£N, 0 £  A[i, j] £  9). For each state the number of neighbors, B [i, j], that have a larger army, is known. The states are neighbors if they have a common border (i.e.£  B[i, j]  £  4). Shtirlits knows matrix B. He has to determine the number of armies for all states (i.e. to find matrix A) using this information for placing forces before the war. If there are more than one solution you may output any of them.

    Input

    The first line contains a natural number N. FollowingN lines contain the description of matrix B - N numbers in each line delimited by spaces.

    Output

    If a solution exists, the output file should contain N lines, which describe matrix A. Each line will contain N numbers delimited by spaces. If there is no solution, the file should contain NO SOLUTION.

    Sample Input

    3
    1 2 1
    1 2 1
    1 1 0
    

    Sample Output

    1 2 3
    1 4 5
    1 6 7
    

    c题暂时不会写,看了队长的代码,实在是很黄很暴力,这是再网上搜到的非暴力解决方案,待有时间再做分析:

    /**
     * @file
     * @brief my c solution for problem 125 on SGU OJ
     * @author ck<chengkechengke@gmail.com>
     * @date 2013-06-19
     * @version 
     * 
     * @section DESCRIPTION
     *
     * There must exist zero in B, if not, the problem has no answer.
     * The A value in the zero position must be bigger than others.
     * Put the biggest value to A at the zero position, update B around
     * the zero positon. Continue this untill A is all filled.
     * Here is the algrithm:
     *  1, init
     *      biggest_value = 9;
     *      A = 0;    // zero in A means that it is not filled, we can always
     *                   fill it with a negtive number.
     *      zero[] = 0; // mark zero positons.
     *                  // zero position means a positon where A and B 
                        // are both zero
     *                  // 0: not a zero positon,
     *                  // 1: a zero positon, its neighbours ar not checked
     *                  // 2: a zero positon, its neighbours are checked
     *  2, find a positon where A and B are both zero
     *      if exist
     *           mark the position with 1, goto 3
     *      else if A is all filled 
     *           success! output A 
     *      else 
     *           NO_ANS
     * 3, extend zero positon in the same row or column, extend in four 
     *    directions -- up, down, right, left
     *        1) find a zero position mark with 1.
     *        2) check if its four neighbours are zero positon
     *           if it is, and its zero mark is zero,
     *                mark the positon with 1;
     *        3) mark this positon with 2
     *        4) goto 1), untill you cannot find zero positon mark with 1.
     * 4, put biggest_value to A at zero positons
     * 5, biggest_value = biggest_value - 1;
     * 6, update B with zero positons
     *      foreach zero positon
     *         if its neighbour value in A is zero
     *              sub B value with 1
     *              if B value < 0
     *                 NO_ANS
     * 7, goto 2
     */
    
    #include <stdio.h>
    #include <string.h>
    
    #define NO_ANS 	printf("NO SOLUTION
    ");return 0;
    #define SZ 3        /**< max size of input */
    
    int n;   
    int A[SZ][SZ] = {{0}};
    int B[SZ][SZ] = {{0}};
    
    int i, j, nn, row, col;
    int element = SZ*SZ;  /**< the number need to put to A */
    int zero[SZ*SZ];    /**< zeros positon row*n+col */
    
    int main() 
    {
      // read input
      scanf("%d", &n);
      nn = n * n;
      for (i=0; i<n; ++i) {
        for (j=0; j<n; ++j) {
          scanf("%d", &B[i][j]);
        }
      }
    
      while (element > 0) {
        // find zeros
        row = 0;   //indicate if there is zero
        memset(zero, 0, sizeof(zero));
        for (i=0; i<n; ++i) {
          for (j=0; j<n; ++j) {
    	if (B[i][j] == 0 && A[i][j] == 0) {
    	  row = 1;
    	  zero[i*n+j] = 1;
    	  break;
    	}
          }
          if (row > 0) {
    	break;
          }
        }
        // cannot find zero
        if (row <= 0) {
          for (i=0; i<n; ++i) {
    	for (j=0; j<n; ++j) {
    	  if (A[i][j] <= 0) {
    	    NO_ANS;
    	  }
    	}
          }
          break;
        }
        // can find zero, extend
        j = 1;  // indicate if zero can be extened
        while (j) {
          j = 0;
          for (i=0; i<nn; ++i) {
    	if (zero[i] == 1) {
    	  row = i / n;
    	  col = i % n;
    	  j = row - 1;
    	  if (j>=0 && A[j][col]==0 && B[j][col]==0 && zero[j*n+col]==0) {
    	    zero[j*n+col] = 1;
    	  }
    	  j = row + 1;
    	  if (j<n && A[j][col]==0 && B[j][col]==0 && zero[j*n+col]==0) {
    	    zero[j*n+col] = 1;
    	  }
    	  j = col - 1;
    	  if (j>=0 && A[row][j]==0 && B[row][j]==0 && zero[row*n+j]==0) {
    	    zero[row*n+j] = 1;
    	  }
    	  j = col + 1;
    	  if (j<n && A[row][j]==0 && B[row][j]==0 && zero[row*n+j]==0) {
    	    zero[row*n+j] = 1;
    	  }
    	  zero[i] = 2;
    	  j = 1;
    	}
          }
        }
        // put element to A
        for (i=0; i<nn; ++i) {
          if (zero[i]) {
    	row = i / n;
    	col = i % n;
    	A[row][col] = element;
          }
        }
        --element;
        // update B
        for (i=0; i<nn; ++i) {
          if (zero[i]) {
    	row = i / n;
    	col = i % n;      
    	j = row - 1;
    	if (j>=0 && A[j][col]<=0) {
    	  --B[j][col];
    	  if (B[j][col] < 0) {
    	    NO_ANS;
    	  }
    	}
    	j = row + 1;
    	if (j<n && A[j][col]<=0) {
    	  --B[j][col];
    	  if (B[j][col] < 0) {
    	    NO_ANS;
    	  }
    	}      
    	j = col - 1;
    	if (j>=0 && A[row][j]<=0) {
    	  --B[row][j];
    	  if (B[row][j] < 0) {
    	    NO_ANS;
    	  }
    	}
    	j = col + 1;
    	if (j<n && A[row][j]<=0) {
    	  --B[row][j];
    	  if (B[row][j] < 0) {
    	    NO_ANS;
    	  }
    	}
          }
        }
      }
    
      // output ans
      for (i=0; i<n; ++i) {
        for (j=0; j<n; ++j) {
          printf("%d ", A[i][j]);
        }
        putchar('
    ');
      }
    
      return 0;
    }
    


     

    D - D

    Crawling in process...Crawling failedTime Limit:500MS    Memory Limit:4096KB     64bit IO Format:%I64d & %I64u

    Description

    There are two boxes. There are A balls in the first box, and B balls in the second box (0 < A + B < 2147483648). It is possible to move balls from one box to another. From one box into another one should move as many balls as the other box already contains. You have to determine, whether it is possible to move all balls into one box. 

    Input

    The first line contains two integers A and B, delimited by space.

    Output

    First line should contain the number N - the number of moves which are required to move all balls into one box, or -1 if it is impossible.

    Sample Input

    Sample Output

    2 6
    

    Sample Output

    2
    

    循环两次才行,wa两个小时后终于突破了

    #include<iostream>
    #include<stdio.h>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    int main()
    {
    	long long a,b;
    	while(cin>>a>>b)
    	{
    		if(a==0||b==0)
    		{
    			cout<<"0"<<endl;
    			continue;
    		}
    		bool flag=false;
    		bool flag2=false;
    		bool check=true;
    		long long tmin=a>b?b:a;
    		long long tmax=a>=b?a:b;
    	    long long mind=-1;
    		int count=0;
    		long long t1,t2,t3,t4;
    		long distance;
    		long long db;
    		int ww=2;
    		while((ww--)&&!flag)
    		{
    			for(int i=1;;i++)
    		  {
    			long long db=(long long)pow((double)2,(double)i);
    		//	cout<<"max: "<<tmax<<" min: "<<tmin<<endl;
    			//cout<<"i: "<<i<<endl;
    			if(2*tmin*db-tmin>tmax)
    			{
    				t1=tmin*db;
    				t2=a+b-t1;
    				long long d1=t1-t2;
    				//cout<<"d1: "<<d1<<endl;
    				
    				t3=tmin*db/2;
    				t4=a+b-t3;
    				long long d2=t4-t3;
    				//cout<<"d2: "<<d2<<endl;
    				if(d1!=0&&d2!=0)
    				{
    					tmax=t1;
    				    tmin=t2;
    					count+=i;
    					break;
    				}
    				else 
    				{
    					 if(d2==0)
    					 {
    					 	count+=i;
    						flag=true;
    						break;
    					 }
    					 if(d1==0)
    					 {
    					  	count+=(i+1);
    						flag=true;
    						break;
    					 }
    
    				}
    		    }
    		  }
    		
    		}
    		if(flag)
    		  cout<<count<<endl;
    		else cout<<"-1"<<endl;
    	}
    }


     

    E - E

    Crawling in process...Crawling failedTime Limit:500MS    Memory Limit:4096KB     64bit IO Format:%I64d & %I64u

    Description

    CIA has decided to create a special telephone directory for its agents. The first 2 pages of the directory contain the name of the directory and instructions for agents, telephone number records begin on the third page. Each record takes exactly one line and consists of 2 parts: the phone number and the location of the phone. The phone number is 4 digits long. Phone numbers cannot start with digits 0 and 8. Each page of the telephone directory can contain not more then K lines. Phone numbers should be sorted in increasing order. For the first phone number with a new first digit, the corresponding record should be on a new page of the phone directory. You are to write a program, that calculates the minimal number P pages in the directory. For this purpose, CIA gives you the list of numbers containing N records, but since the information is confidential, without the phones locations. 

    Input

    The first line contains a natural number K (0 < K < 255) - the maximum number of lines that one page can contain. The second line contains a natural  N (0 < N < 8000) -  number of phone numbers supplied.  Each of following N lines contains a number consisting of  4 digits - phone numbers in any order, and it is known, that numbers in this list cannot repeat.

    Output

    First line should contain a natural number P - the number of pages in the telephone directory.

    Sample Input

    5
    10
    1234
    5678
    1345
    1456
    1678
    1111
    5555
    6789
    6666
    5000
    

    Sample Output

    5
    

    暂时不会写

    F - F

    Crawling in process...Crawling failedTime Limit:500MS    Memory Limit:4096KB     64bit IO Format:%I64d & %I64u

    Description

    There are N points given by their coordinates on a plane. All coordinates (xi,yi)  are integers in a range from -10000 up to 10000 inclusive  . It is necessary to construct a broken line satisfying the following conditions:
    1. The broken line should be closed.
    2. End points of each segment (verteces) of the broken line can only be the given points, and all given points should be used.
    3. Each two consecutive segments of the broken line should form a corner of 90 degrees in each vertex point.
    4. The sides of the broken line should be parallel to coordinate axes.
    5. The broken line should have no self-crossing and self-contact.
    6. The broken line should have the minimal length.
    You have to either find the length L of the constructed broken line, or determine that it is impossible to construct such a broken line.

    Input

    First line contains the number N (4 <= N <= 10000) - amount of points. Each of the following N lines contains coordinates of points separated by space xi and yi (1 <= i <= N). Points are given in random order.

    Output

    First line should contain the length of the broken line L or 0 if there is no solution.

    Sample Input

    Sample Output

    4
    0 0
    0 3
    3 3
    3 0
    

    Sample Output

    12
    
    G - G

    Crawling in process...Crawling failedTime Limit:500MS    Memory Limit:4096KB     64bit IO Format:%I64d & %I64u

    Description

    The old King decided to divide the Kingdom into parts among his three sons. Each part is a polygonal area. Taking into account the bad temper of the middle son the King gave him a part of Kingdom such that moving straight from any place of this part to any other place of this part he will not cross the boundary.
    There are several mineral deposits in the Kingdom. Each mineral deposit looks like a straight line segment. The middle son wants to know what part of mineral deposits is located inside his territory (not including the boundaries).

    Input

    The first line contains an integer N (3<=N<=400) - the number of vertexes of the polygon boundaring the territory of King's middle son. Each i-th line of the next N lines contains pair of integers xi, yi (0<=xi,yi<=30000) - a position of the i-th vertex (3<=i<=400). The vertexes are given in random order. There are no any three vertexes laying on a straight line. The next line includes the only integer M (2<=M<=1000) - the number of mineral deposits in the Kingdom. Each j-th line of the next M lines contains two pairs of integers aj1, bj1 - point of the beginning and aj2, bj2 - point of the end of the j-th mineral deposit (0<=aj1,bj1,aj2,bj2<=30000, for 1<=j<=M). The numbers in each line are divided by spaces.

    Output

    Output file should contain M lines. Each j-th line should contain a real number Lj calculated with precision 0.01 - the lehgth of the middle son's part of j-th mineral deposit.

    Sample Input

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

    Sample Output

    0
    2
    1
    0
    
    H - H

    Crawling in process...Crawling failedTime Limit:500MS    Memory Limit:4096KB     64bit IO Format:%I64d & %I64u

    Description

    On a circle border there are 2k different points A1, A2, ..., A2k, located contiguously. These points connect k chords so that each of points A1, A2, ..., A2k is the end point of one chord. Chords divide the circle into parts. You have to find N - the number of different ways to connect the points so that the circle is broken into minimal possible amount of parts P.

    Input

    The first line contains the integer k (1 <= k <= 30).

    Output

    The first line should contain two numbers N and P delimited by space.

    Sample Input

    2
    

    Sample Output

    2 3
    

    卡特兰数

    #include<iostream>
    #include<stdio.h>
    using namespace std;
    int main()
    {
    	int k;
    	while(cin>>k)
    	{
    		long long sum;
    		for(int i=1;i<=k;i++)
    		{
    			if(i==1)
    			{
    				sum=1;
    			}
    			else 
    			{
    				sum=(4*i-2)*sum/(i+1);
    			}
    		}
    		cout<<sum<<' '<<k+1<<endl;
    	}
    }                                                                                                                                                                 


     

    I - I

    Crawling in process...Crawling failedTime Limit:500MS    Memory Limit:4096KB     64bit IO Format:%I64d & %I64u

    Description

    The banquet hall of Computer Scientists' Palace has a rectangular form of the size M x N (1<=M<=9, 1<=N<=9). It is necessary to lay hardwood floors in the hall. There are wood pieces of two forms:
    1) rectangles (2x1)
    2) corners (squares 2x2 without one 1x1 square)
    You have to determine X - the number of ways to cover the banquet hall.
    Remarks. The number of pieces is large enough. It is not allowed to leave empty places, or to cover any part of a surface twice, or to saw pieces.

    Input

    The first line contains natural number M. The second line contains a natural number N.

    Output

    First line should contain the number X, or 0 if there are no solutions.

    Sample Input

    2 3
    

    Sample Output

    5
    
    还没看题
  • 相关阅读:
    卡特兰数
    混合运算改进(代码)
    典型用户和场景
    混合运算
    四则运算
    计算
    感想
    git
    附加导航 affix,side--toolbar(可结合博客园使用~)
    对python-rrdtool模块的浅研究。
  • 原文地址:https://www.cnblogs.com/jackwuyongxing/p/3366490.html
Copyright © 2011-2022 走看看