CSP-X simulation competition third Wang Yinhao supplementary question report

11 篇文章 1 订阅

date:Year2023 Oct.5th   test time: 9:30->11:30

Student id=S07738   name=Wang Yinhao

My question score:

T1 late:Accepted(AC) score:100

T2 ladder:Accepted(AC) score:100

T3 hamster:Wrong_Answer(WA) score:60

T4 square:Time_Limited_Exceed(TLE) score:90

score=100+100+50+90,the score is 350(Oh My God!)

2. Competition process

Question 1(late):

This question is purely a water problem, and it can be calculated directly.

Question 2(ladder)

This question, like the first question, is purely a water question, and only needs to be simulated

Question 3 (hamster)

This question in the exam I directly a sort + enumeration gcd + judgment, so the score only stole to 60 points, and the correct idea is to find the rule, only need to judge whether the first two numbers of gcd is less than or equal to 2, if there is such a situation, it is Peace, otherwise it is Fight

Question 4 (square)

On the fourth trip, I used priority_queue priority queue array + set to deduplicate, and I reached 90 points, just because I used too much STL, so the time exceeded

Topic analysis:

Question 1: late

Title Description:

The coordinates of Xiaoke's home are x, and the coordinates of the school are y. Now there are two modes of transportation:

1. Walk. The distance traveled with a unit length of 1 consumes time a

2. Ride a shared bicycle. Riding a shared bicycle through a distance of 1 unit length takes b, but it takes c time to borrow and return the bike.

If you choose to walk, you can walk directly to the school. But if you choose to ride a shared bicycle, you also need to walk to coordinate z first, then borrow a car, ride, arrive at school, and then return the bike.

How should Xiaoke get to school the fastest?

Idea of the question:

According to the topic description, we can sort out the following formula:

Walking: ABS(Y-X)*A

Automotive: ABS(Z-X)*A+C*2+(Z-Y)*B

According to the topic description, we can write the following code (AC code!! ):

#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
int main(){
    //freopen("late.in","r",stdin);
    //freopen("late.out","w",stdout);
    int t;
    scanf("%d",&t);
    while(t--){
        int x,y,z,a,b,c;
        scanf("%d%d%d%d%d%d",&x,&y,&z,&a,&b,&c);
        int sum1=abs(x-y)*a;
        int sum2=(abs(x-z)*a)+(abs(y-z)*b+c+c);
        if(sum1<=sum2) printf("Walk\n");
        else printf("Ride\n");
    }
    //fclose(stdin);
    //fclose(stdout); 
    return 0;
}

Question 2: ladder

Title Description:

The rules of the game are: divide all playing cards (except the big and small kings) into two parts, one for each person. Next, take turns to play cards, line up the cards played, and if the same card appears (ignoring the suit), you can take all the cards between the two identical cards.

Note: Cards that are taken away are set aside for final scoring instead of being added to the hand.

Now Xiaoke and Dada have decided on the order in which they are played. Obviously, at this time, the winner and loser of the game have been divided, so they have no interest in playing. But they want to know the outcome of the game. So they found you and wanted you to figure out who won.

Enter a description

Playing cards 10 are represented by 0, and all others are represented by corresponding characters.

The first line is 26 characters and represents Xiaoke's hand.

The second line, 26 characters, represents Dada's hand.

Output description

The first line is a string, if the small can win, it is output, if Dada wins, it is output, and the draw is output;xkddEqual

The second line has two numbers, representing the last number of cards for Xiaoke and Dada.

Problem solving ideas:

input

Loop through 52 times, each time doing the following

        if(i%2==1){
            c[cnt++]=a[(i/2)+1];
            for(int j=cnt-2; j>=1; j--){
                if(c[j]==c[cnt-1]){
                    ans1=ans1+(cnt-j);
                    cnt=j;
                    break;
                }
            }
        }
        else{
            c[cnt++]=b[i/2];
            for(int j=cnt-2; j>=1; j--){
                if(c[j]==c[cnt-1]){
                    ans2=ans2+(cnt-j);
                    cnt=j;
                    break;
                }
            }
        }

Final output

AC Code:

#include<iostream>
#include<cstdio>
using namespace std;
int main(){
    //freopen("ladder.in","r",stdin);
    //freopen("ladder.out","w",stdout);
    char a[30],b[30];
    int ans1=0,ans2=0;
    for(int i=1; i<=26; i++){
        cin>>a[i];
    }
    for(int i=1; i<=26; i++){
        cin>>b[i];
    }
    char c[10005],cnt=1;
    for(int i=1; i<=52; i++){
        if(i%2==1){
            c[cnt++]=a[(i/2)+1];
            for(int j=cnt-2; j>=1; j--){
                if(c[j]==c[cnt-1]){
                    ans1=ans1+(cnt-j);
                    cnt=j;
                    break;
                }
            }
        }
        else{
            c[cnt++]=b[i/2];
            for(int j=cnt-2; j>=1; j--){
                if(c[j]==c[cnt-1]){
                    ans2=ans2+(cnt-j);
                    cnt=j;
                    break;
                }
            }
        }
    }
    cout<<(ans1==ans2?"Equal":(ans1<ans2?"dd":"xk"))<<"\n";
    printf("%d %d",ans1,ans2);
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

Question 3: hamster

Title Description:

Xiaoke has n hamsters, and each hamster has a fixed amount of food AI.

Now it is necessary to line up these n hamsters, and for the first i (i ≥2) hamsters, if their food amount of gcd is greater than i, then they will fight.

Xiaoke wondered if there was an arrangement that would keep all hamsters from fighting.

Enter a description

Multiple sets of inputs. The first line is a positive integer t, representing the number of groups of multiple inputs.

For each set of inputs, the first line is a positive integer n, representing the number of hamsters.
The second row of n positive integers representing the amount of food per hamster.

Problem-solving ideas

The loop traverses two layers of for loops, and each time if gcd(a[i],a[j])<=2, it directly outputs Peace

If not, output Fight

AC code

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int gcd(int a,int b){
    return a%b==0?b:gcd(b,a%b);
}
int main(){
    //freopen("hamster.in","r",stdin);
    //freopen("hamster.out","w",stdout);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[105];
        for(int i=1; i<=n; i++){
            scanf("%d",&a[i]);
        }
        sort(a+1,a+n+1);
        int flag=0;
        for(int i=1; i<=n; i++){
            int flag1=0;
            for(int j=i+1; j<=n; j++){
                if(gcd(a[i],a[j])<=2){
                    flag1=1;
                    break;
                }
            }
            if(flag1==1){
                flag=1;
                break;
            }
        } 
        cout<<(flag==1?"Peace":"Fight")<<"\n";
    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

Question 4: square

Title Description:

There are n people in Xiaoke's class, and everyone belongs to a group. Groups are numbered from 1 to M (not necessarily every group has people).

Everyone has an executive power. This execution force may be a positive integer, it may be a negative number. The higher the execution, then the faster and better the person will do when he is present.

Now the class has to select a number of groups to go out and line up the phalanx. Select a number of individuals from each selected group. But make sure that the number of people selected is the same for each selected group.

Excuse me, what is the sum of the highest executive power of all selected people?

Problem solving ideas:

Greed No
matter how many people are chosen, then we are definitely the one who prefers the most execution.
So we choose the i individual in group i, and we definitely choose the largest k individual. So we're going to sort each group of people from largest to smallest. You can take both prefixes and optimizations.
Implementation
We can use vector arrays for grouping operations, then sort each group from largest to smallest, and then prefix and sum for each group. There are two implementations here: The first implementation:


More directly, we scan the vector array directly. We specify the length chosen. Then iterate through each group, choosing as you can. This gives you the maximum value when each group chooses 1, 2, 3, ... people. Finally, choose the largest one.
But this is too time-consuming, so we can sort the vector array from largest to smallest in length. This allows you to ignore many unnecessary lookups.
The advantage of this approach is that it is not difficult to think, but it is cumbersome to write.
Second implementation:
Same as above.
But we know the previous greedy strategy, so for each group, when we find the prefix sum, we can determine whether we should choose when we choose individuals. Put the selected case into the ANS array and find the maximum value.
AC Code:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+5;
int n,m,maxsize;
ll ans=0,tot[N];
vector<vector<ll> > v(N);
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        int a,b;
        scanf("%d%d",&a,&b);
        v[a].push_back(b);
    }
    for(int i=1;i<=m;i++){
        if(v[i].size()){
            sort(v[i].begin(),v[i].end(),greater<int>());
            if(v[i][0]>0) tot[0]+=v[i][0];
            for(int j=1;j<v[i].size();j++){
                v[i][j]+=v[i][j-1];
                if(v[i][j]>0){
                    tot[j]+=v[i][j];
                }    
            }
        }
    }
    for(int i=0;i<=n;i++) ans=max(ans,tot[i]);
    printf("%lld\n",ans);
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Programming_Konjac

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值