SDUT 2021 Spring Individual Contest(for 20) - 8 补题

Question A Large Summation
idea: find the number that is closest to 1e9+6 when added to the i-th number, and this number cannot be the i-th number

#include<bits/stdc++.h>
#define ll long long 
using namespace std;
const int N=1e5+10;
const int mod=1e9+7;
pair<ll,int>a[N];
int t,n;
ll ans[N];
int main()
{
    
    
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>t;
    while(t--)
    {
    
    
        memset(ans,-1,sizeof(ans));
        cin>>n;
        for(int i=1;i<=n;i++)
        {
    
    
            ll x;
            cin>>x;
            a[i]=make_pair(x,i);
        }
        sort(a+1,a+1+n);
        int j=n;
        for(int i=1;i<=n;i++)
        {
    
    
            for(;j>=1;j--)
            {
    
    
                if(i==j) 
                continue;
                if(a[i].first+a[j].first>=mod)
                continue;
                ans[a[i].second]=a[i].first+a[j].first;
                break;
            }
            if(ans[a[i].second]==-1)
            {
    
    
                ans[a[i].second] = (a[i].first + a[i == n ? n - 1 : n].first) % mod;
            }
        }
        for(int i=1;i<=n;i++)
        cout<<ans[i]<<" ";
        cout<<endl;
    }
    return 0;
}

I problem The Crazy Jumper
idea: dp problem, the meaning of dp[i] is the minimum number of steps to the i-th position

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[N], dp[N], pos[N];
int main() 
{
    
    
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--) 
	{
    
    
        int n;
        cin >> n;
        for (int i = 1; i <= n; i++) cin >> a[i];
        memset(dp, 0, sizeof(dp));
        memset(pos, -1, sizeof(pos));
        dp[1] = 0;
        pos[a[1]] = 1;
        for (int i = 2; i <= n; i++)
		 {
    
    
            dp[i] = dp[i - 1] + 1;
            if (pos[a[i]] != -1) dp[i] = min(dp[i], dp[pos[a[i]]] + 1);
            pos[a[i]] = i;
        }
        cout << dp[n] << endl;
    }
    return 0;
}

J question The Hell Boy
idea: the key is to introduce the dp formulaInsert picture description here

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
const int mod=1e9+7;
long long  sum[N];
int a[N];
int main()
{
    
    
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
    
    
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
        cin>>a[i];
        sum[1]=a[1];
        for(int i=2;i<=n;i++)
        sum[i]=(sum[i-1]+a[i]+sum[i-1]*a[i])%mod;
        cout<<sum[n]%mod<<endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_51768569/article/details/115054129