Codeforces Beta Round #10

Click to open the link cf#10


A

Idea: Simulation
Analysis:
1 The topic asks for the total computer consumption. The title clearly states that the computer belongs to the first state within n time periods, and if it is not the first state, it simply changes to the first state by moving the mouse or pressing the keyboard.
The 2 question also states that each set of inputs guarantees L<R and Ri-1<Li. Then we just need to process one for each input.

code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int n , p1 , p2 , p3 , t1 , t2;

int main(){
    int sum;
    int l , r , pre;
    while(scanf("%d%d%d%d%d%d" , &n , &p1 , &p2 , &p3 , &t1 , &t2) != EOF){
        scanf("%d%d" , &l , &r);
        sum = (r-l)*p1;
        pre = r;
        for(int i = 1 ; i < n ; i++){
           scanf("%d%d" , &l , &r);
           sum += (r-l)*p1;
           int dis = l-pre;
           if(dis > t1){
             sum += t1*p1;
             dis -= t1;
             
             if(dis > t2){
                sum += t2*p2;
                dis -= t2;
                sum += dis*p3;
             }
             else
                sum += dis*p2;
           } 
           else
             sum += dis*p1;
           Pre = r; / * record the previous position * /
       }
       printf("%d\n" , sum);
    }
    return 0;
}


B

Idea: Simple simulation questions
Analysis:
1 The title gives n possible m values, and then requires the corresponding x, yl, yr to be output for each m value;
2 xc and yc are the most central positions of the rectangle, then xc = (k+1)/2 and yc = (k+1)/2. According to the formula given by the topic, you can simulate
3 The position between the simulations cannot be reused, so a vis[][] should be used here to mark whether the current point is being seated.
4 Note that when the condition is met, consider the case where the array is out of bounds. See the judge() function.

code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

#define MAXN 110

int n , k;
int xc , yc;
int vis[MAXN][MAXN];

bool judge(int x , int y , int m){
       If(y+m-1 > k)/* If not enough to return false*/ directly
      return false;
   for(int j = y ; j < y+m ; j++){
      if(vis[x][j])
        return false;
   }
   return true;
}

void solve(int m){
    int min = 0x7FFFFFFF;
    int x , yl , yr;
    for(int i = 1 ; i <= k ; i++){
       for(int j = 1 ; j <= k ; j++){
          if(!vis[i][j]){
            if(judge(i , j , m)){
               int tmp = 0;
               for(int t = j ; t < j+m ; t++){
                  tmp += abs(i-xc)+abs(t-yc);
               }
               if(tmp < min){
                 min = tmp;
                 x = i , yl = j , yr = j+m-1;
               }
            }
          }
       }
    }
    if(min == 0x7FFFFFFF)
      printf("-1\n");
    else{
      printf("%d %d %d\n" , x , yl , yr);
             For(int j = yl ; j <= yr ; j++)/* marks the x line from yl~yr as sitting */
         vis[x][j] = 1;
    }
}

int main(){
    int m;
    while(scanf("%d%d" , &n , &k) != EOF){
        memset(vis , 0 , sizeof(vis));
        xc = (k+1)/2 , yc = (k+1)/2;
        for(int i = 0 ; i < n ; i++){
           scanf("%d" , &m);
           solve(m);
        }
    }
    return 0;
}



C

Idea: Number theory
Analysis:
1 The problem is to find the number of d(AB) = d(c) and AB != C. For example, when n = 4, there are two (3, 4, 3) and (4, 3, 3).

2 According to the meaning of the question d(xy) = d(d(x)d(y)), then d(AB)=d(d(A)d(B)) And d(AB) = d(C) , so d(C) = d(d(A)d(B)). Suppose now that x is a number between [1, n];
x = d(C) has num[d(C)], x = d(A) has num[d(A)], x = d(B) has num [d(B)]. Then the number of (A, B, C) is num[d(A)]*num[d(B)]*num[d(C)].

3 tree root
1 If you add a large number of digits to get a sum, add the sum of the digits of the sum and get a sum, and then continue to make the number sum until the last digit and It is a single digit, and this last number is called the "digital root" of the first number. For example, d(2345) = d(14) = d(5), then the number of 2345 is 5
2 The root of a number is the remainder of it divided by 9. d(x) = x%9 != 0 ? x%9 : 9; note that if x%9 = 0, then the number is 9.

4 [1,n] Any three integers A, B, C in this interval, satisfying the number of A*B = C (n/1+n/2+... +n/n);
For example, [1,4], the number that satisfies A*B = C
1*1 =1 , 1*2 = 2 , 1*3 = 3 , 1*4 = 5;
2*1 = 2 , 2*2 = 4;
3*1 = 3;
4*1 = 4;
A total of 8 = 4/1+4/2+4/3+4/4

5 Based on the above knowledge, find all (A, B, C) - (A, B, C) {AB = C}.

code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

#define MAXN 1000010
#define N 15

int n;
long long num[N];

 /* find the number of each number * /
void init(){
   memset(num , 0 , sizeof(num));
   for(int i = 1 ; i <= n ; i++){
      int tmp = i%9 != 0 ? i%9 : 9;
      num[tmp]++;
   }
}

int main(){
   while(scanf("%d" , &n) != EOF){
       init();
       long long ans = 0;

       for(int i = 1 ; i <= 9 ; i++){
          for(int j = 1 ; j <= 9 ; j++){
            int tmp = i*j;
            tmp %= 9;
            if(!tmp) 
              tmp = 9;
            if(num[tmp])
              ans += num[i]*num[j]*num[tmp];
          }
       }
       for(int i = 1 ; i <= n ; i++)
           ans -= n/i;
       cout<<ans<<endl;
   }
   return 0;
}



D

Idea: dp+lcis
Analysis:
1 Defines the state dp[i][j] represents the length of the LCIS consisting of the first j characters of the first i character b string of the a string and ending with b[j]
2 Assuming a[i] != b[j] , then dp[i][j] = dp[i-1][j];
Assuming a[i] = b[j] , then dp[i][j] = max{dp[i-1][k]}+1 ; 1<=k< =j-1
3 It is not difficult to see that this is a DP with a time complexity of O(n^3) and a distance from the square.
However, the most critical aspect of this algorithm is that if we follow a reasonable recursive order, the value of max(dp[i-1][k]) can be accessed before dp[i ][k] is obtained by maintaining a max variable by maintenance. How to get it? The order of the first recursion must be that the first dimension of the state is looped in the outer layer and the second dimension is looped in the inner layer. That is to say, dp[1][len(b)] is calculated and dp[2] [1] is calculated.
If you follow this recursion order, we can add a max variable to 0 at the beginning of each outer loop and start the inner loop.
When a[i]>b[j], let max=dp[i-1][j]. If looping to a[i]==b[j], let dp[i][j]=max+1.
The final answer is the maximum value of dp[len(a)][1]..dp[len(a)][len(b)]

code:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;

#define MAXN 510

int n , m;
int num1[MAXN] , num2[MAXN];
int dp[MAXN][MAXN];
int pre[MAXN];

 /* Recursive output */
void output(int x){
   if(pre[x] == -1){
      printf("%d" , num2[x]);
      return;
   }
   output(pre[x]);
   printf(" %d" , num2[x]);
}

int DP(){
   memset(dp , 0 , sizeof(dp));
   memset(pre , -1 , sizeof(pre));
   int max , k;
   for(int i = 1 ; i <= n ; i++){
      max = 0 ;
      k = -1;
      for(int j = 1 ; j <= m ; j++){
        dp[i][j] = dp[i-1][j];
        if(num1[i] > num2[j] && max < dp[i-1][j]){
          max = dp[i-1][j];
          k = j;
        }
        if(num1[i] == num2[j]){
          dp[i][j] = max+1;
                     Pre[j] = k;/*record precursor*/
        }
      }
   }
   max = 0;
   for(int i = 1 ; i <= m ; i++){
      if(max < dp[n][i]){
         max = dp[n][i];
         k = i;
      }
   }
   printf("%d\n" , max);
   if(max) 
      output(k);
   printf("\n");
}

int main(){
   while(scanf("%d" , &n) != EOF){
       for(int i = 1 ; i <= n ; i++)
          scanf("%d" , &num1[i]);
       scanf("%d" , &m);
       for(int i = 1 ; i <= m ; i++)
          scanf("%d" , &num2[i]);
       DP();
   }
   return 0;
}


E

analysis:

1 First of all, this is a paper conclusion. There is a paper devoted to this issue (in fact, the title description on CF and almost none of the words in the paper have changed. The examples are the same) The title of this paper is "APolynomial-timeAlgorithmfortheChange-MakingProblem"

2 In fact, we can emotionally guess a "looking very reliable" algorithm:

• Sort all currencies from big to small first.

• Consider a representation of a number greater than w[i] but less than w[i−1]. In order to get a bad solution for normal people's greedy algorithm, we should make this number just a little more than w[i]. It is conceivable that we must make people greedy w[i] and find that the remaining number must use a large number of Small money can be spelled out.

• We need to determine how many small currencies will be used to indicate this number S greedy w[i]. So we find a j, so w[j+1]<S−w[i]<w[j], as the largest available currency after greed. We enumerate this j and find out what is the maximum price strictly less than w[i] that can be spelled out using i+1 to j currencies, and then add this value to w[j].

• We check all such values ​​and find the smallest one. This approach is intuitive and intuitive. In fact, we can rigorously prove the correctness of this algorithm, but it is more complicated, and I am interested to refer to the paper myself.

Code:


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int n , ans , a[401];

int main(){

	scanf("%d",&n);
	for(int i = 1 ; i <= n ; i++) 
		scanf("%d", &a[i]);

	ans = 1<<30;

	for(int i = 2 ; i <= n ; i++){
		for(int j = i ; j <= n ; j++){
			int x = a[i-1]-1;
			int y = 0;
			for (int k = i ; k <= j ; k++){
				y += x/a[k];
			    x %= a[k];
			}
			x = a[i-1]-1-x+a[j];
			y++;
			int x1 = x;
			if(x1 < ans){
				for(int k = 1 ; k <= n ; k++){
					y -= x/a[k];
			        x %= a[k];
				}
		        if(y < 0) 
			    	ans = x1;
			}
		}
	}
	if(ans == 1<<30) 
		puts("-1");
	else 
		printf("%d\n",ans);
	return 0;
}


Intelligent Recommendation

Codeforces Beta Round #1

Click to open the link A B...

Codeforces Beta Round #3

Codeforces Beta Round #3 A. Shortest path of the king continue digging A. Shortest path of the king time limit per test1 second memory limit per test64 megabytes inputstandard input outputstandard out...

CodeForces Beta Round Patience

A - Chord answer: Three keys simultaneously press the composition and strings If the three keys differ from left to right, it is divided into 4, and then a wave of Major. Differentiation 4, 3 for mino...

[Solution] Codeforces Beta Round #1

table of Contents   A. Theatre Square Description of the topic Intentional analysis AC code B. Spreadsheets Description of the topic Intentional analysis AC code A. Theatre Square Description of ...

More Recommendation

Codeforces Beta Round #45 48A

Original link Codeforces Beta Round #45 48A Subject Three people play rock-paper-scissors, and the game is divided when only one person wins the other two. Problem-solving ideas The first letter is ex...

Codeforces Beta Round #1B Spreadsheets

Topic Description: Everyone has seen the Excel list or two-dimensional array, and we will give the following two ways for the description of rows and columns: 1.RxCyThe R represents ROW, C represents ...

Codeforces Beta Round #2 A. Winner

http://codeforces.com/contest/2/problem/A Ideal: Give you some people's results Seeking the name of the people who have the greatest achievement If there are many outputs, the first to be up> = RET...

Codeforces Beta Round #73(Div2)

This CF exercises feeling a little lost, a lot of pit Portal A - Chord Idea: To give C, C #, D, D #, E, F, F #, G, G #, A, B, H, these 12 symbols, the title given to 3 symbols, if separated by 43, the...

【Codeforces Beta Round #26 】Codeforces 26E Multithreading

First of all w<0 、 w>∑ni 、 n=1 Case. It must then be legal. may wish to arrange order, according to w with n1 The purpose of the relationship classification is the last operation 1 And befor...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top