Individual competition on April 12, 2020

A - Balloons

Title: The title of this question is relatively simple. In short, it is divided into numbers, so that the sum of the numbers obtained by A is greater than the sum of the numbers obtained by B, and then the subscript of the number assigned to A is output.

Solution: Pay attention to the special judgments of n == 1 and n == 2.

Code:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ll long long
using namespace std;
int main() {
    int n;
    cin>>n;
    int num[20]= {0};
    int sum=0;
    for(int i=1; i<=n; i++) {
        cin>>num[i];
        sum=sum+num[i];//总数
    }
    if(n==1) {
        cout<<-1<<endl;
    } else if(n==2) {
        if(num[1]==num[2]) {
            cout<<-1<<endl;
        } else {
            cout<<1<<endl;
            cout<<1<<endl;
        }
    } else { //大于等于 3
        int su=0;
        int i;
        int f=0;
        for(i=1; i<=n; i++) {
            su=su+num[i];
            sum=sum-num[i];
            if(su!=sum&&sum!=0&&su!=0) {
                f=1;
                break;
            }
        }
        if(f==1) {
            cout<<i<<endl;
            for(int j=1; j<=i; j++) {
                cout<<j;
                if(j<i) {
                    cout<<" ";
                }
            }
            cout<<endl;
        }else{
            cout<<-1<<endl;
        }
    }
    return 0;
}

B - Cutting

Question meaning: This question is also relatively simple. The title gives you an array of length n. Two numbers can be "cut off" at the cost of the absolute value of the difference between the two numbers.

Problem Solution: There are several solutions to this problem. Here I wrote it with a backpack (0-1). The key is to judge whether the two numbers can be "cut", the cost is consumed, and the benefit gained depends on it. It is "1", so it is a naked question of "0-1 backpack".

Code:

#include <iostream> 
#include <cstring> 
#include <algorithm>
 using  namespace std;
 int main () {
     int n, B; 
    cin >> n >> B / * This is equivalent to a total volume * / ;
     int num [ 250 ] = { 0 };
     int jnum = 0 , onum = 0 ;
     for ( int i = 0 ; i <n; i ++ ) { 
        cin >> num [i];
         if (num [i]% 2 == 0 ) {
            onum++ ; 
        } else { 
            jnum ++ ; 
        } 
    } // After the data is input, the data will be processed below 
    int t = 0 ; // Number of records 
    int V [ 200 ] = { 0 }, W [ 200 ] = { 0 };
     int tj = 0 , to = 0 ; // The decibel indicates the number of odd and even numbers in front 
     for ( int i = 0 ; i <n; i ++) { // Bar gap 
        if (i + 1 < n) {
             if(num[i]%2==0){
                to++;
            }else{
                tj++;
            }
            if((tj==to)&&((jnum-tj)==(onum-to))){
                W[t]=1;
                V[t++]=abs(num[i]-num[i+1]);
                tj=0;
                to=0;
            }
        }
    }        
    int dp[500]={0};
    for(int i=0;i<t;i++){
        for(int j=B;j>=V[i];j--){
            dp[j]=max(dp[j],dp[j-V[i]]+1);
        }
    }
    cout<<dp[B]<<endl;
    return 0;
}

D - Sonya and Hotels

Question meaning: This question probably means to give you a string of numbers, insert a number between the numbers, and ask for the absolute value of the difference between this number and its adjacent number = d, ask how many such insertion points exist .

Solution: This question is relatively simple. Here we find the difference between two adjacent numbers. If the difference is greater than 2 * d, we can insert two. If the difference is 2 * d, we can insert one. Note that each of the last two endpoints .

Code:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ll long long
using namespace std;
int main() {
    ll n,d;
    cin>>n>>d;
    int temp,p;
    cin>>temp;
    ll ans=0;
    for(int i=1;i<n;i++){
        cin>>p;
        if(p-temp-2*d>0){
            ans++ ; ++ 
            years ; 
        } else  if (p-temp- 2 * d == 0 ) { 
            ans ++ ; 
        } 
        temp = p; 
    } 
    cost << years + 2 ; 

    return  0 ; 
}

 E - Sonya and Exhibition

Title: This question is more interesting. It probably means that there are n flowers, which can be lily or rose, and there are m individuals. Give their observation interval and find their beauty value (equal to the number of roses * Number of lilies), find the maximum value of this beauty value.

Solution: The key to this question is to know that to maximize the single beauty value, you have to make the number of two flowers in the interval equal. There is also a pit where he gives m people. If there is only one person, This is easy to find. Here, we only need to make the two flowers appear alternately. (Like a brain teaser)

Code:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ll long long
using namespace std;
int main() {
    ll n,m;
    cin>>n>>m;
    ll li,ri;
    char ptr[10000];
    for(int i=0;i<m;i++){
        cin>>li>>ri;
    }
    for(int i=0;i<n;i++){
        if(i%2==0){
            cout<<1;
        }else{
            cout<<0;
        }
    }

    return 0;
}

F - Sonya and Robots

Title: The title of this question is also relatively simple. The basic requirement is to give you a string of int arrays with n elements, asking you to find two numbers from it, assign these two numbers to two cars, and make They move in opposite directions. When they encounter the same number in the array, they stop moving. When the final relative position of the two vehicles changes, the car is damaged. Logarithm, how many can keep the car from being damaged.

Solution: This question is also a simple question in essence, but I did n’t read it during the game, and I probably wo n’t write it. The solution on the Internet is to record the number of vehicles on the left side of each car. They can be added together, but unfortunately my idea is just the opposite. What I thought of is to record the number of types of cars on the right of it, but it has not yet been written. It is a lesson in thinking.

Code:

#include<iostream>
#include<cstring>
#include<set>
#include<algorithm>
#define ll long long
const int N=100005;
using namespace std;
int arr[N],vis[N];
set<int> st;
int main(){
    int n,t;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>t;
        vis[t]=st.size();
        st.insert(t);
    }
    ll sum=0;
    for(int i=0;i<=100000;i++){
        sum=sum+vis[i];
    }
    cout<<sum<<endl;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/blogxsc/p/12723453.html