Monthly competition of ACM training team of Taizhou University (New Year's Day venue)

Posted by alvinkoh on Sun, 02 Jan 2022 17:49:12 +0100

The game address is stamped here

1001: math problem:

Find the limit:

1002: Maoming's achievements

Solution:
For an item i, we can choose its first achievement, or choose the second achievement, or not, so that we can use dfs to traverse all situations. The time complexity is O (3 ^ 10)

#include<bits/stdc++.h>
using namespace std;
const int N=10+5,mod=1e9+7;
typedef long long LL;
int tt[N],m[N],T[N],M[N];
int n,t,ans=0;
void dfs(int i,int time,int sum)
{
    //cout<<time<<endl;
    if(time<=t)//Compare if the time is less than t.
    {
        ans=max(sum,ans);
    }
    if(i>n)return;//Return when the number of items exceeds.
    dfs(i+1,time+tt[i],sum+m[i]);
    dfs(i+1,time+T[i],sum+M[i]);
    dfs(i+1,time,sum);
}
void solve()
{

    scanf("%d %d",&n,&t);
    for(int i=1;i<=n;i++)
    {
        scanf("%d %d %d %d",&tt[i],&m[i],&T[i],&M[i]);
    }
    dfs(1,0,0);
    printf("%d",ans);
}
int main()
{
    solve();
}

1003: Maoming's boss battle

If the damage we receive in one second is 2n, we can remove 2n from the total HP first. If the remaining HP is greater than or equal to N, it means that small z can carry this second, otherwise it can't.

#include<bits/stdc++.h>
using namespace std;
const int N=10+5,mod=1e9+7;
typedef long long LL;
int n,m;
void solve()
{
    m-=1;//Pay attention to reduce the blood volume by one first
    int s=2*n;
    int cnt=m/2/n,re=m%(2*n);
    if(re>=n)cnt++;
    printf("%d\n",cnt);
}
int main()
{
    while(~scanf("%d %d",&n,&m))
    {
        solve();
    }
}

1004: running for President:

Solution:
Record the maximum number of votes and the average number of votes. If the maximum number of votes is less than the average number of votes, you need (average number of votes - existing votes); otherwise, if the maximum number of votes is not tied, you need less than the maximum number of votes (maximum number of votes + 1 - existing votes), and if it is equal to 0 votes. If the maximum number of votes is tied, it is required (maximum number of votes + 1 - existing number of votes).

#include <bits/stdc++.h>
using namespace std;

const int N = 2000;

int n;
int a[N];
int main()
{
    cin >> n;
    int m = -1,cnt = 0;
    for(int i = 1; i <= n; i ++)
    {
        scanf("%d",&a[i]);
        if(a[i] == m) cnt ++;
        if(a[i] > m) m = a[i],cnt = 1;
    }
    for(int i = 1; i <= n; i ++)
    {
        if(a[i] < m) printf("%d",m - a[i] + 1);
        else if(a[i] == m)
        {
            if(cnt > 1) printf("1");
            else printf("0");
        }
        if(i != n) printf(" ");
    }
    return(0);
}

1005: Maoming doesn't know anything

Solution:
ps. the data may be weak
First, for a number a[i], there are several smaller numbers on his left, which we record as l[i].
For a number a[i], there are several smaller numbers on his right, which we record as r[i].
For each number a[i], his contribution in the answer is l[i]*r[i]*a[i].
Why. Because it is equivalent to selecting any point in the left area and any point in the right area, the value of this continuous string is a[i].
In this way, we can find l[i] and r[i] in the time complexity of O(n) using the monotone stack (not first).

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5,mod=1e9+7;
typedef long long LL;
LL a[N],l[N],r[N];
struct node{
    LL x,pos;
};
void solve()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
    a[0]=a[n+1]=1e9;
    stack<node>st;
    st.push({a[0],0});
    for(int i=1;i<=n;i++)
    {
        if(!st.empty())
        {
            while(st.top().x<=a[i])st.pop();
        }
        int x=st.top().x,pos=st.top().pos;
        l[i]=pos+1;
        st.push({a[i],i});
    }
    while(!st.empty())st.pop();
    st.push({a[n+1],n+1});
    for(int i=n;i>=1;i--)
    {
        if(!st.empty())
        {
            while(st.top().x<a[i])st.pop();
        }
        int x=st.top().x,pos=st.top().pos;
        r[i]=pos-1;
        st.push({a[i],i});
    }
    LL ans=0;
    for(int i=1;i<=n;i++)
    {
        LL L=(i-l[i]+1),R=(r[i]-i+1);
        ans=((L*R*a[i])%mod+ans)%mod;
    }
    printf("%lld",ans);
}
int main()
{
    solve();
}

1006: Maoming asked for foreign aid

Solution:
After observation (really. We can find that as long as the three letters in the first two lines are determined, the n-order pyramid is determined.
We violently simulate the pyramids of six situations, compare them with the original pyramids, select the ones with the highest degree of reconnection, and then we can.

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+5,mod=1e9+7;
typedef long long LL;
int a[N][N];
int t[7][N][N];
int n;
void st(int x,int y,int id)
{
    t[id][x][y]=6-(t[id][x-1][y]+t[id][x-1][y-1]);
}
void solve()
{

    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        getchar();
        for(int j=1;j<=i;j++)
        {
            char p;
            scanf("%c",&p);
            if(p=='G')a[i][j]=1;
            if(p=='R')a[i][j]=2;
            if(p=='B')a[i][j]=3;
        }
    }
    t[1][1][1]=1;t[1][2][1]=2;t[1][2][2]=3;
    t[2][1][1]=1;t[2][2][1]=3;t[2][2][2]=2;
    t[3][1][1]=2;t[3][2][1]=1;t[3][2][2]=3;
    t[4][1][1]=2;t[4][2][1]=3;t[4][2][2]=1;
    t[5][1][1]=3;t[5][2][1]=1;t[5][2][2]=2;
    t[6][1][1]=3;t[6][2][1]=2;t[6][2][2]=1;
    for(int i=1;i<=6;i++)
    {
        for(int j=3;j<=n;j++)
        {
            for(int k=2;k<n;k++)st(j,k,i);
            //Pay attention to the first and last of each line
            t[i][j][1]=6-(t[i][j-1][1]+t[i][j][2]);
            t[i][j][j]=6-(t[i][j-1][j-1]+t[i][j][j-1]);
        }
    }
    int res=0;
    for(int i=1;i<=6;i++)
    {
        int ans=0;
        for(int j=1;j<=n;j++)
        {
            for(int k=1;k<=j;k++)
            {
                if (t[i][j][k] == a[j][k])ans++;
            }
        }
        res=max(ans,res);
    }
    printf("%d",n*(n+1)/2-res);
}
int main()
{
    solve();
}

Topics: Algorithm