usc 队内练习赛8-19 思维训练

A.       Cup-holder

A  row  in a  theater has  N  seats. There  is  a single  cup-holder  between adjacent  seats,  and also  two additional cup-holdersat both ends of the row. Exception to this are pairs of love seats - there isno cup-holder between them.

Given sequence of letters describing seats in some row, and assuming thatall seats are taken, find the maximum number of people that can put their cupsin a cup-holder right next to their seat.

Letter ‘S’ in the sequence denotes ordinary seat, and ‘L’ denotes loveseat. Love seats will always come in pairs of adjacent seats.

Diagram below corresponds to sequence ‘SLLLLSSLL’, with asterixesdenoting cup-holders.

* S * L  L * L  L * S * S * L L *

For this example, at least two persons won’t be able to put their cupsinto cup-holders.

INPUT

The first line contains an integer T(1 <= T <= 20), indicating thenumber of test cases. Each test case contains two lines, the first line ofinput contains the integer N (1 ≤ N ≤ 50), number of seats in a row.Thefollowing line contains a sequence of N letters ‘L’ or ‘S’, describing the rowas stated above.

OUTPUT

For each case,output only line contained the maximum number of people that can put their cupsin cup-holder right next to them.

SAMPLE TESTS

IN

OUT

3

3

SSS

4                         

SLLS

9

SLLLLSSLL

3

4

7                  


算法:

统计s的个数个l的个数:然后最后结果为s+l/2+1,得出最后结果。

要注意的是 如果杯子比人多,输出人,如果人比杯子多,输出杯子;


#include<stdio.h>
#include<string.h>

char a[100];

int main()
{
  int x,n,sum,i;
  scanf("%d",&x);
  while(x--)    
  {
      sum=0;
      scanf("%d",&n);
      scanf("%s",a);
      for(i=0;i<n;i++)
      {
        if(a[i]=='S')sum++;
        if(a[i]=='L'){sum++;i++;}        
      }       
      sum++;
      if(n>sum)printf("%d\n",sum);
      else printf("%d\n",n);
                
  }  
    
}




B.       Point

The Croatian version of this contest has the following rules: “Each  round of  the  contest consists  of  8 tasks with different point values. Every contestant may solve any of thetasks they choose. However, the contestant’s final score will be the sum of points earned on any 5 taskssuch that this sum is maximized.” 

Since the organizers were busy coming up with interesting problems forthe contest (and translating  them),  they’ve simply  forgotten  to solve  the  problem of  determining  the points  scored  by each  contestant. Now they arekindly asking you to do it for them. 

Write a program that, given the number of points earned by a contestanton each task, determines the  totalamount of points scored by that contestant, as well as the sorted list of the 5problems counting  towards  that score.  No  contestant will  ever  score the  same  amount of  points  on two  different  problems. 

INPUT 

The first line contains an integer T(1 <= T <= 20), indicating thenumber of test cases. For each test case , the first and only line of inputcontains 8 positive integer X (0 ≤ X ≤ 150), where thei-th number X denotes the number of points earned by the contestant on problemi. All 8 numbers X will be distinct.

OUTPUT 

For each case, the first line of output must contain the total amount ofpoints scored by the contestant.  Thesecond line of output must contain indices of the 5 problems counting towardsthe total score,  sorted in ascendingorder, separated by single spaces. Problem indices are positive integers from 1to 8, inclusive. 

SAMPLE TESTS 

IN

OUT

3

20  30  50  48  33  66  0  64

20  0  50  80  77   110  56  48

20  30  50  80  110  11  0  85

261

3 4 5 6 8

373

3 4 5 6 7

355

2 3 4 5 8

算法:

最快方法:

定义8个结构体 包含 score 和rank

开始对score排序,求出前5个的值:

然后对rank排序,求出前5个的排名 


#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct stu{
           int point;
           int rank;
          }a[10];
int cmp(stu  a,stu b)
{
  return a.point>b.point;    
}
int cmp1(stu a,stu b)
{
  return a.rank<b.rank;    
}



int main()
{
    int x,i,sum;
    scanf("%d",&x);
    while(x--)
    {
        sum=0;
        for(i=1;i<=8;i++)
         {
          scanf("%d",&a[i].point);
          a[i].rank=i;
         }        
        sort(a+1,a+8+1,cmp);
        for(i=1;i<=5;i++)
         sum+=a[i].point;
         printf("%d\n",sum);
        sort(a+1,a+5+1,cmp1);
        for(i=1;i<=5;i++)
        printf("%d ",a[i].rank);
        printf("\n");        
         
              
    }
    
    
}


C.       Wood

Mirko needs to chop down M metres of wood. It is an easy job for himsince he has a nifty new woodcutting machine that can take down forests likewildfire. However, Mirko is only allowed to cut a single row of trees.

Mirko's machine works as follows: Mirko sets a height parameter H (inmetres), and the machine raises a giant sawblade to that height and cuts offall tree parts higher than H (of course, trees not higher than H meters remainintact). Mirko then takes the parts that were cut off.   For example, if the tree row contains treeswith heights of 20, 15, 10, and 17 metres, and Mirko raises his sawblade to 15metres, the remaining tree heights after cutting will be 15, 15, 10, and 15metres, respectively, while Mirko will take 5 metres off the first tree and 2metres off the fourth tree (7 metres of wood in total).

Mirko is ecologically minded, so he doesn't want to cut off more woodthan necessary. That's why he wants to set his sawblade as high as possible.Help Mirko find the maximum integer height of the sawblade that still allowshim to cut off at least M metres of wood.

INPUT

The first line contains an integer T(1 <= T <= 20), indicating thenumber of test cases. For each test case ,The first line of input contains twospace-separated positive integers, N (the number of trees, 1 ≤ N ≤  1 000 000) and M (Mirko'srequired wood amount, 1 ≤ M ≤ 2 000 000 000).

The second line of input contains N space-separated positive integersless than 1, 000, 000,000,   the heightsof each tree (in metres). The sum of all heights will exceed M, thus Mirko willalways be able to obtain the required amount of wood.

OUTPUT

For each test case , output only line contained the required heightsetting.

SAMPLE TESTS

IN

OUT

2

4 7                     

20 15 10 17

5 20

4 42 40 26 46

15

36            


伐树问题,应当添加一个0的树高,这样能能保证,伐的树木达到所需要的数量,然后用贪心的方法进行求解,要注意的是这一条语句

要注意的是下面这种情况 

样例:

3 10

7 7 7

那么切的树的高度应为 3这样就能保证达到要砍伐的值了。


#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int a[1100000];
int cmp(int a,int b)
{
  return a>b;    
}

int main()
{
  int x,n,require,i,flag;
  __int64 sum;
  scanf("%d",&x);
  while(x--)    
   {
    sum=0;
    scanf("%d %d",&n,&require);
    scanf("%d",&a[1]);
    for(i=2;i<=n;i++)
      {
      scanf("%d",&a[i]);             
      }
      if(n==1){printf("%d\n",a[1]-require);continue;}
   
    sort(a+1,a+1+n,cmp);    //将树高从小到大排序
   
    for(i=2;i<=n;i++)
    {
       sum+=(a[i-1]-a[i])*(i-1);  //这是通过O(n)的算法来找切的最近的require
       if(sum>=require)break;   
    }
     if(sum==require){printf("%d\n",a[i]);continue;}   //如果正好切到的高度 和其中的一个树高一样的话就直接输出这个树高
     
     if(sum<require) {            //在两个树高之间的话 采用下面这种方法求解
                      if( (require-sum)%n==0 ){printf("%d\n",a[n]-(require-sum)/n );continue;}
                      else {printf("%d\n",a[n]-(require-sum)/n );continue;}
                     }
     
    sum-=(a[i-1]-a[i])*(i-1);    //所切的值比任何一个树高都小的话采用下面这个方法求解。
    if((require-sum)%(i-1) ==0)printf("%d\n",a[i-1]-(require-sum)/(i-1) );
    else printf("%d\n",a[i-1]-(require-sum)/(i-1)-1 );             
   } 
    
    
    
}


D.        Music

Mr. B is a famous music composer. One of hismost famous work was his set of preludes. These 24 pieces span the 24 musicalkeys  (there are musically distinct 12scale notes, and each may use major or minor tonality). The 12 distinct scalenotes are:

Five of the notes have two alternate names,as is indicated above with equals sign. Thus, there are 17 possible names ofscale notes, but only 12 musically distinct notes. When using one of these asthe keynote for a musical key, we can further distinguish between major andminor tonalities. This gives 34 possible keys, of which 24 are musicallydistinct.

In naming his preludes Mr. B used all thekeys except the following 10, which were named instead by their alternatenames:

Write a program that, given the name of akey, give an alternate name if it has one, or report the key name is unique.

Input

Each test case is described by one linehaving the format "note tonality", where "note" is one ofthe 17 names for the scale notes given above, and "tonality" iseither "major" or "minor" (quotes for clarify).

Output

For each case output the required answer,following the format of the sample.

Sample Input

Output for Sample Input

Ab minor

D# major

G minor

Case 1: G# minor

Case 2: Eb major

Case 3: UNIQUE

算法: 

最简单的匹配 :

遇到表格一中的等号的值就直接转化就可以了,遇到单个字符直接输出unique。

 
 
#include<stdio.h>
#include<string.h>
char b[11][5];
char aa[5],bb[10];
int main()
{

  int sum=0;
  strcpy(b[1],"A#");     
  strcpy(b[2],"Bb");    
  strcpy(b[3],"C#");    
  strcpy(b[4],"Db");
  strcpy(b[5],"D#");  
  strcpy(b[6],"Eb");
  strcpy(b[7],"F#");     
  strcpy(b[8],"Gb");    
  strcpy(b[9],"G#");    
  strcpy(b[10],"Ab");

  int len,ji=0,i;
  while(scanf("%s %s",aa,bb)!=EOF)
  {

      
       ji++;
       len=strlen(aa);
       if(len==1){if(sum==0){printf("Case %d: UNIQUE\n",ji);sum++;continue;}printf("Case %d: UNIQUE\n",ji);continue;}           
       else{     
            for(i=1;i<=10;i++)
            {
                
            if(strcmp(b[i],aa)==0){
                if(i%2==0)strcpy(aa,b[i-1]);else strcpy(aa,b[i+1]);break;}
            }
            if(sum==0){printf("Case %d: %s %s\n",ji,aa,bb);sum++;continue;}
            printf("Case %d: %s %s\n",ji,aa,bb);
       }
       
  }
  return 0;
     
}




E.        Music

Mr. B,  Mr.  G and  Mr.  M are  now  in Warsaw,  Poland,  for the  2012’s  ACM-ICPC  World   Finals  Contest. They’ve decided to take a 5 hours training every day before the contest.Also, they plan to start training at 10:00 each day since the World FinalContest will do so. The scenery in Warsaw is so attractive that Mr. B wouldalways like to take a walk outside for a while after breakfast. However, Mr. Bhave to go back before training starts, otherwise his teammates will beannoyed. Here is a problem: Mr. B does not have a watch. In order to know theexact time, he has bought a new watch in Warsaw, but all the numbers on thatwatch are represented in Roman Numerals. Mr. B cannot understand such kind ofnumbers. Can you translate for him?

Input

Each test case contains  a single  line  indicating  a Roman Numerals that   to be  translated.  All the   numbers  can  befound on clocks. That is, each number in the input represents an integerbetween 1 and 12. Roman  Numerals areexpressed by strings consisting of uppercase ‘I’, ‘V’ and ‘X’. See the sampleinput for further information.

Output

For each test case, display a single linecontaining a decimal number corresponding to the given Roman Numerals.

Sample Input

Output for Sample Input

I

II

III

IV

V

VI

VII

VIII

IX

X

XI

XII

Case 1: 1

Case 2: 2

Case 3: 3

Case 4: 4

Case 5: 5

Case 6: 6

Case 7: 7

Case 8: 8

Case 9: 9

Case 10: 10

Case 11: 11

Case 12: 12

 将所有的情况列出来~~然后打表得出最后的结果。

#include<stdio.h>
#include<string.h>

char a[10];
char b[13][10];
int main()
{
   strcpy(b[1],"I"); 
   strcpy(b[2],"II");     
   strcpy(b[3],"III");     
   strcpy(b[4],"IV");  
   strcpy(b[5],"V"); 
   strcpy(b[6],"VI");     
   strcpy(b[7],"VII");     
   strcpy(b[8],"VIII"); 
   strcpy(b[9],"IX"); 
   strcpy(b[10],"X");     
   strcpy(b[11],"XI");     
   strcpy(b[12],"XII"); 
   int i,ji=0;
   while(scanf("%s",a)!=EOF)
    {
      ji++;
      for(i=1;i<=12;i++)
        if(strcmp(a,b[i])==0)break;
      printf("Case %d: %d\n",ji,i);
    }




    
}


 

F.         Letter Grid

Consider the following letter grid:

E     

R    

A    

T      

A

T

S

R

A

U

T

U

There are 7 ways to read the word TARTU fromthe grid:

E   

R   

A   

T   

A

T

S

R

A

U

T

U

E     

R     

A    

T   

A

T

S

R

A

U

T

U

E     

R     

A    

T     

A

T

S

R

A

U

T

U

E    

R    

A    

T      

A

T

S

R

A

U

T

U

E     

R   

A  

T  

A

T

S

R

A

U

T

U

E    

R     

A   

T    

A

T

S

R

A

U

T

U

E   

R    

A   

T    

A

T

S

R

A

U

T

U

 

Given a letter grid and a word, your task isto determine the number of ways the word can be read from the grid. The firstletter of the word can be in any cell of the grid, and after each letter, thenext letter has to be in one of the neighbour cells (horizontally, verticallyor diagonally). A cell can be used multiple times when reading the word.

Input Data

Each test case contains three integers: H(1 ≤ H ≤ 200), the height of the grid,W(1 ≤ W ≤ 200), the width of the grid, andL(1 ≤ L ≤ 100), the length of the word. The followingH lines each containingW letters describe the grid. The lastline containingL letters describes the word. All letters in the grid andin the word are uppercase English letters (A...Z).

Output Data

For each case, output only line contained oneinteger: the number of ways the word can be read from the grid. You may assumethat the answer is always at most 1018.

Examples

in

out

3 4 5                         
ERAT
ATSR
AUTU
TARTU

2 2 10
AA
AA
AAAAAAAAAA

7                

78732

只有zzy队长一个人a出了这道题,膜拜队长中~~典型的dp问题 来求解

算法: 

拿第一个举一个例子 

ERAT 搜第一个T  0001    搜第二个A时 对A所在位置周边8个数相加得到  0020         例如 第一个A在(0,2)位置上,周边8个位上有(0,3)(1,1)上有值为1,相加得到2.
ATSR                  0100                                                                       1000
AUTU                  0010                                                                       1000

搜R时    0300   搜T时 0002    搜U时 0000      在搜u结束后总共有5+2 7个结果

             0002            0300              0000

             0000            0020              0502


 
 
#include<stdio.h>
char a[210][210];
__int64 b[210][210];
__int64 c[210][210];
__int64 sum=0;
char judge[200];
int x[9]={0,-1,-1,-1,0,0,1,1,1};
int y[9]={0,-1,0,1,-1,1,-1,0,1};
int main()
{
  int wid,hit,len,i,j,ji,k,xa,ya;
  while(scanf("%d %d %d",&wid,&hit,&len)!=EOF)
  {
     ji=0;
     for(i=0;i<wid;i++)    
       scanf("%s",a[i]);              
     scanf("%s",judge);     
     for(i=0;i<wid;i++)
      for(j=0;j<hit;j++)    //寻找 judge[0]的值,、 
        {
        if(a[i][j]==judge[ji])b[i][j]=1;
        else b[i][j]=0;   
        }
      if(len==1){                  //会出现长度为1的这种特殊情况
                 sum=0;
                 for(i=0;i<wid;i++)
                   for(j=0;j<hit;j++)
                     sum+=b[i][j];
                 printf("%I64d\n",sum);
                 continue;
                 }  

  while(ji!=len-1)            //用长度来表示循环的次数,
   { 
        ji++;
        
     for(i=0;i<wid;i++)                     //两个数组分别记录 dp的数据
      for(j=0;j<hit;j++)
      {
        if(a[i][j]==judge[ji]){
         sum=0;
         for(k=1;k<9;k++)
           {   
           int xa=i+x[k];     //注意这里应当重新定义xa,ya来进行带入,如不重新定义后面的值会影响xa,ya的结果 
           int ya=j+y[k];
           if(xa>=0&&xa<wid&&ya>=0&&ya<hit)
            {if(b[xa][ya]!=0)
              {
                sum+=b[xa][ya];
              }
            }
           }

              c[i][j]=sum;               
                             }
         else c[i][j]=0;              
      }

     for(i=0;i<wid;i++)                  //将数据进行拷贝
       {
       for(j=0;j<hit;j++)
         {
         b[i][j]=c[i][j];
         }
       }   
   }
           sum=0;                   //输出结果
        for(i=0;i<wid;i++)
          for(j=0;j<hit;j++)
              sum+=b[i][j];
         printf("%I64d\n",sum);
 
   
   
  
}   
    
}


 




G.       Printed Circuit Board

In a printed circuit board, conductive wiresare laid on a non-conductive board. Because the conductors in the same layercannot cross without creating short-circuits, boards with conductors dividedinto several layers separated by non-conductive board material are used in morecomplex cases. However, boards with more layers are more expensive. So,manufacturers try to allocate the required conductors to layers in a way thatminimizes the number of layers needed.

In this task we look at boards where eachconductor is connecting two ports located on opposite edges of the board andseek to minimize the cost of such a board.

Consider, for example, the board shown on theleft on the figure below. If one conductor has to connect A to B and another Dto C, this could be achieved in a single layer, as shown in the middle on thefigure. But a conductor connecting A to C and another connecting D to B couldnot be laid out in the same layer, as can be seen on the right on the figure.

Write a program that is given the locationsof the endpoints of the N conductors on a WxH board and determines the minimalnumber of layers needed to accommodate all of them.

It may be assumed the width of the conductorsis very small compared to the distances between the ports. That is, between anytwo conductors, there is always enough room for a third one.

Input Data

Each test case contains N(1 ≤ N ≤ 105), the number of connectors. Each of thefollowing N lines contains two integers, Xi1 and Xi2(0 ≤ Xij ≤ 106), separated by a space, meaning that thei-th conductor has to connect the points (Xi1,0) and (Xi2,H). It may be assumedthat all the 2N endpoints given in the input are distinct.

Output Data

For each case, output only line contained asingle integer, the minimal number of layers needed to accommodate all therequired conductors.

Examples

in

out

2            
1 1
3 3

2
1 3
3 1

1             

2

 

 


H.       数字游戏

给你一个N位数,从中去掉K个数字,能得到的最大的数是多少?

INPUT

有T测试数据,每组测试数据第一行由N和K2个整数组成(1 ≤ K < N ≤ 500 000),第二行是N位数(非0开头)。

OUTPUT

对每组数据输出去掉K个数字得到的最大数。

SAMPLE TESTS

IN

OUT

3

4 2

1924

7 3

1231234

10 4

4177252841

94

3234

775841

 


删数问题解释:

采用的是利用队列的方式来进行的操作 ,每次进行10次即可找到最大值,总共寻找n-k个最大值就可以了,

例如

                7  3

                1 2 3 1 2 3  4

   位置为   0 1 2 3 4 5 6


从0- 3 的位置上找到最大值        

9个队列    0 1 2 3 4 5 6 7 8 9  

位置            0 1 2

从9-0遍历 遇到队列非空切 left >遍历的位置时即可 选择 3;

left=2  即 选择3时的位置

然后记录下一个位置

 

9个队列    0 1 2 3 4 5 6 7 8 9  

位置            0 1 

                  3

让后从9-0遍历 到2时 将位置1踢走 然后 到1时 将位置0踢走,然后到3得出正确结果。

 

另一种算法:

将 所有的位置 都放到0-9 这个位置中 用一个区间来卡住,从9-0开始遍历,在区间左边的值pop,不管区间右边的值,如果遇到刚好在区间里面的值就是你要找到的值,以此值所在的位置当做左区间,右区间加1即可。

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
queue<int> my[11];
char a[501000];
char b[501000];
int main()
{
  
    int i,k,n,x,ji,left,max,j,flag,num;
    scanf("%d",&x);
  while(x--)
  {     
    memset(b,'0',sizeof(b));
    for(i=0;i<=9;i++)    
     {  
       while(my[i].empty()==0)    //将队列里全部的值pop 
        {
          my[i].pop();
        }                
     }
     
     ji=0;
     scanf("%d %d",&n,&k);
     scanf("%s",a);
     
     for(i=0;i<=k;i++)                //输入【】里面是值的大小 push的是值的位置
        {
        my[ a[i]-'0' ].push(i);     
           }
     for(j=9;j>=0;j--)
         {
          if( my[j].empty()==0 ){    如果不为空 将之赋给只包括正确答案的b数组内。
          b[ji]=('0'+j);
          ji++; 
          left=my[j].front();
          my[j].pop(); break;}             
         }
         max=k+1;
     for(i=1;i<=n-k-1;i++)
       {
         my[ a[max]-'0'].push(max);
         max++;
         flag=0;
         for(j=9;j>=0;j--)
          {
             while(my[j].empty()==0)
             {
               if(left>my[j].front() ){my[j].pop();continue;}  //要注意这里的continue,因为没加 wa了好多次。
              
               if(left<my[j].front() ){
                                      flag=1;
                                      b[ji]='0'+j;
                                      ji++;
                                      left=my[j].front();
                                      my[j].pop();
                                      break;}
             }                         
             if(flag==1)break;
          }                                  
       }
       b[ji]='\0';
       printf("%s\n",b);    输出正确答案。
  }  
}







 

你可能感兴趣的:(usc 队内练习赛8-19 思维训练)