20200229VJ训练总结

关于这场为期五天的DP和贪心算法训练,我受益颇深。以下为我觉得解题思路很巧妙或很经典的题目。
1.Boredom添加链接描述
Alex doesn’t like boredom. That’s why whenever he gets bored, he comes up with games. One long winter evening he came up with a game and decided to play it.

Given a sequence a consisting of n integers. The player can make several steps. In a single step he can choose an element of the sequence (let’s denote it ak) and delete it, at that all elements equal to ak + 1 and ak - 1 also must be deleted from the sequence. That step brings ak points to the player.

Alex is a perfectionist, so he decided to get as many points as possible. Help him.
INPUT
The first line contains integer n (1 ≤ n ≤ 105) that shows how many numbers are in Alex’s sequence.

The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 105).
OUTPUT
Print a single integer — the maximum number of points that Alex can earn.
EXAMPLES
Input
2
1 2
Output
2
Input
3
1 2 3
Output
4
Input
9
1 2 1 3 2 2 2 2 3
Output
10


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>

using namespace std;

long long d[100005];

int main()
{
 long long a=0,b=0,c,x;
 int n;
 cin>>n;
 for(int i=0;i<n;i++)
 {
     cin>>x;
     d[x]+=x;
 }
 for(int i=1;i<=100000;i++)
 {
     c=a+d[i];
     a=max(a,b);
     b=c;
 }
 cout<<max(a,b);
 return 0;
}

2.Vasya and String添加链接描述
High school student Vasya got a string of length n as a birthday present. This string consists of letters ‘a’ and ‘b’ only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.

Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
INPUT
The first line of the input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n) — the length of the string and the maximum number of characters to change.

The second line contains the string, consisting of letters ‘a’ and ‘b’ only.
OUTPUT
Print the only integer — the maximum beauty of the string Vasya can achieve by changing no more than k characters.
EXAMPLES
Input
4 2
abba
Output
4
Input
8 1
aabaabaa
Output
5
Note
In the first sample, Vasya can obtain both strings “aaaa” and “bbbb”.

In the second sample, the optimal answer is obtained with the string “aaaaabaa” or with the string “aabaaaaa”.

#include<iostream>
#include<cstring>
using namespace std;
char str[100005];
int n,k,Max,ans,sum,index,temp,x,y;
int main()
{
    cin>>n>>k;
    int i,j;
    str[0]=='c';
    for(i=1;i<=n;i++)
    {
        cin>>str[i];
        if(str[i]=='a') x++;
        else y++;
    }
    if(k>=min(x,y))
    {
        cout<<n;
        return 0;
    }
    ans=0;
    i=1;
    sum=0;
    for(;i<=n;)
    {
        if(str[i]=='a')
        {
            sum++;
            i++;
        }
        else if(i<=n&&str[i]=='b'&&ans<k)
        {
            ans++;
            sum++;
            i++;
        }
        if(ans==k)
        {
            temp=sum;
            index=i;
            while(str[index]=='a'&&index<=n)
            {
                sum++;
                index++;
            }
            Max=max(Max,sum);
            sum=temp-1;
            if(str[i-sum-1]=='b') ans--;
        }
    }
    ans=0;
    i=1;
    sum=0;
    for(;i<=n;)
    {
        if(str[i]=='b')
        {
            sum++;
            i++;
        }
        else if(i<=n&&str[i]=='a'&&ans<k)
        {
            ans++;
            sum++;
            i++;
        }
        if(ans==k)
        {
            temp=sum;
            index=i;
            while(str[index]=='b'&&index<=n)
            {
                sum++;
                index++;
            }
            Max=max(Max,sum);
            sum=temp-1;
            if(str[i-sum-1]=='a') ans--;
        }
    }
    cout<<Max;
    return 0;
}

另一种思路

#include <bits/stdc++.h>
#include<iostream>
using namespace std;
int n, k;
string s;
int sum[100005];
int check(int x)
{
    for (int i = 0; i + x <= n; i++)
    {
        if (sum[i + x] - sum[i] >= x - k || sum[i + x] - sum[i] <= k)
            return 1;
    }
    return 0;
}

int main()
{
    cin >> n >> k >> s;
    sum[0] = 0;
    for (int i = 0; s[i]; i++)
    {
        if (s[i] == 'b')
            sum[i + 1] = sum[i] + 1;
        else
            sum[i + 1] = sum[i];
    }
    //二分答案
    int L = 1, R = n, ans;
    while (L <= R)
    {
        int mid = (L + R) / 2;
        //检查成功,mid可以是答案,继续搜索
        if (check(mid))
            ans = mid, L = mid + 1;
        else
            R = mid - 1;
    }
    cout << ans << "\n";
    return 0;
}

3.Tetrahedron添加链接描述
You are given a tetrahedron. Let’s mark its vertices with letters A, B, C and D correspondingly.
在这里插入图片描述
An ant is standing in the vertex D of the tetrahedron. The ant is quite active and he wouldn’t stay idle. At each moment of time he makes a step from one vertex to another one along some edge of the tetrahedron. The ant just can’t stand on one place.

You do not have to do much to solve the problem: your task is to count the number of ways in which the ant can go from the initial vertex D to itself in exactly n steps. In other words, you are asked to find out the number of different cyclic paths with the length of n from vertex D to itself. As the number can be quite large, you should print it modulo 1000000007 (109 + 7).
INPUT
The first line contains the only integer n (1 ≤ n ≤ 107) — the required length of the cyclic path.
OUTPUT
Print the only integer — the required number of ways modulo 1000000007 (109 + 7).
EXAMPLES
Input
2
Output
3
Input
4
Output
21
Note
The required paths in the first sample are:

D - A - D
D - B - D
D - C - D

#include<iostream>
#define N 10000000+5
#define mod  1000000007
using namespace std;
long long dp[N],n;
int main()
{
    cin>>n;
    dp[1]=0;
    dp[2]=3;
    for(long long i=3;i<=n;i++)
    {
        dp[i]=2*dp[i-1]+3*dp[i-2];
        dp[i]%=mod;
    }
    cout<<dp[n];
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值