Summer Training Diary - 8.6 (Codforce)

Posted by Colton.Wagner on Mon, 07 Oct 2019 18:10:27 +0200

D. Suitable Replacement
Topic: In the S string? Convert to lowercase letters, so that string S has the largest number of disjoint strings of string T, (letters in S can be exchanged in order)
Solution: Greed

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef long double ld;


const int N=1e7+10;
const int MAXN=20010;
const int INF=0x3f3f3f3f;
const double eps=0.0000001;
const ll mod=1e9+7;
int n,m,x,y,t;
char a[N],b[N],c[N];
int num[N];

int main()
{
    scanf("%s",a);
    scanf("%s",b);
    int len1=strlen(a),len2=strlen(b);
    int cnt=0;
    for(int i=0;i<len1;i++)
    {
        if(a[i]=='?') cnt++;
        else num[a[i]-'a']++;
    }
    int p=0,flag=0;
    while(1)
    {
        for(int i=0;i<len2;i++)
        {
            if(num[b[i]-'a']>0)
                num[b[i]-'a']--;
            else
            {
                if(cnt>0)
                {
                    cnt--;
                    c[p++]=b[i];
                }
                else
                flag=1;
            }
        }
        if(flag==1) break;
    }
    p=0;
    for(int i=0;i<len1;i++)
    {
        if(a[i]=='?') printf("%c",c[p++]);
        else printf("%c",a[i]);
    }
}

D. Welfare State
Title:
There are n people, the next line is everyone's money, a total of q operations.
Operation 1: Change the number of money of the first person into xxx
Operation 2: Change all money less than XXX into xxx.
Solution: Simple simulation, see the code for details.

#include<bits/stdc++.h>

#define fi first
#define se second
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef long double ld;


const int N=1e7+10;
const int MAXN=20010;
const int INF=0x3f3f3f3f;
const double eps=0.0000001;
const ll mod=1e9+7;
int n,m,x,y,t;
int a[N],b[N];
pair<int,pii>p[N];

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&p[i].fi);
        if(p[i].fi==1)
            scanf("%d%d",&p[i].se.fi,&p[i].se.se);
        else
            scanf("%d",&p[i].se.se);
    }
    int temp=0;
    /// Because the last operation is processed, and because the second operation after operation 1 will affect the value of a[i], the reverse order is chosen.
    for(int i=m;i>=1;i--)
    {
        if(p[i].fi==1&&b[p[i].se.fi]==0)/// Processing only the last operation of 1
        {
            b[p[i].se.fi]=1;
            a[p[i].se.fi]=max(temp,p[i].se.se);
        }
        if(p[i].fi==2)/// Find the maximum two operations
        {
            temp=max(temp,p[i].se.se);
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(b[i]!=1) a[i]=max(temp,a[i]);
        printf("%d ",a[i]);
    }
}

D. Yet Another Subarray Problem
Title:
Given a sequence array and two constants m, km, km, k, we define a costcostcostcost of a subsequence equal to the sum of the elements of the subsequence and subtract the product of the upward integer value of the length of the subsequence and the quotient of mmm with kkk. Find the maximum costCost that can be obtained by choosing a continuous subsequence. (pspsps ps: empty sets can be taken, at which time costcostcostcost1000 for subsequences)
Problem Solution: (Dynamic Programming)
dp[i]dp[i]dp[i] DP [i] denotes the costcostcost value of a continuous subsequence ending in iii
dp[i] has two modes of transfer:
The first is when (l_r)(l-r)(l_r) <=mmm
([i_x, i][i-x, i][i_x, i][i_x, i]) the interval and the maximum of -kkk (0 (0 <= XXX <= m) m)
The second is when (l_r)(l-r)(l_r) > mmm
([i_m, i][i-m, i][i_m, i][i_m, i] interval sum)-kkk++ (costcostcost value of a continuous subsequence ending in i_mi-mi_m)

#include<bits/stdc++.h>

#define fi first
#define se second
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef long double ld;


const int N=10e5+10;
const int MAXN=20010;
const ll MAX=2e18;
const int INF=0x3f3f3f3f;
const double eps=0.0000001;
const ll mod=1e9+7;
ll n,m,x,y,t,k,p,ans;
ll dp[N],a[N],sum[N];

int main()
{
    scanf("%lld%lld%lld",&n,&m,&k);
    memset(dp,-INF,sizeof(dp));
    for(int i=1;i<=n;i++)
        scanf("%lld",&a[i]),sum[i]=sum[i-1]+a[i];
    for(int i=1;i<=n;i++)
    {
        for(int j=max(1ll,i-m+1);j<=i;j++)
            dp[i]=max(dp[i],sum[i]-sum[j-1]-k);
        dp[i]=max(dp[i],dp[max(0ll,i-m)]+sum[i]-sum[max(0ll,i-m)]-k);
 		ans=max(ans,dp[i]);
    }
    printf("%lld",ans);

}


Topics: less Programming