第八周总结

这一周我们训练了区间dp和背包问题

F

There are two strings A and B with equal length. Both strings are made up of lower case letters. Now you have a powerful string painter. With the help of the painter, you can change a segment of characters of a string to any other character you want. That is, after using the painter, the segment is made up of only one kind of character. Now your task is to change A to B using string painter. What’s the minimum number of operations?
Input
Input contains multiple cases. Each case consists of two lines:
The first line contains string A.
The second line contains string B.
The length of both strings will not be greater than 100.
Output
A single line contains one integer representing the answer.
Sample Input
zzzzzfzzzzz
abcdefedcba
abababababab
cdcdcdcdcdcd
Sample Output
6
7

题意:有两个相同长度的字符串,你可以执行操作——将字符串1任意区间内的都刷成相同的字符,求刷成字符串2的最小操作数。
读到题我首先想到了括号配对的那道题,错误的将它们联系在一起,括号配对那一题,其实相当于求最长回文序列。但是这一题不只包括回文这一情况,他是求相同字符区域对的最小个数。设置区间(i,j)如果b[i]!=a[j],dp[i][j]=dp[i+1][j]+1;在i-j区间内,如果有第k个跟第i个相同,那么就可以将i-j区间借助k分成两部分dp[i][j]=min(dp[i+1][k]+dp[k+1][j]);这些是空字符串转化为字符串2,然后讨论字符串1转化,如果第i个字符串相同那么就不用刷了a[i]=a[i-1];如果不同a[i]=min(a[j]+a[j+1][i],a[i]);

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define N 2000
char you[N],others[N];
int dp[N][N],A[N];
void check(int dp[N][N])
{
    for(int i=1;i<=10;i++){
        for(int o=1;o<=10;o++)
            cout<<dp[i][o]<<" ";
        cout<<endl;}
};
int main()
{
    int n,v,s;
    while(scanf("%s%s",you+1,others+1)!=EOF)
    {
        int l=1,r=strlen(you+1);
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=r;i++)
            dp[i][i]=1;
        for(int len=2;len<=(r+1-l);len++)
            for(int i=l,j=len;j<=r;i++,j++)
        {
            dp[i][j]=dp[i][j-1]+1;
            for(int k=i;k<j;k++)
            if(others[k]==others[j])
            {
                dp[i][j]=min(dp[i][j],dp[i][k-1]+dp[k][j-1]);
            }
        }
        check(dp);

        memset(dp,0,sizeof(dp));
        for(int i=1;i<=r;i++)
            dp[i][i]=1;
        for(int len=2;len<=(r+1-l);len++)
            for(int i=l,j=len;j<=r;i++,j++)
        {
            if(others[i]==others[j])
            {
                dp[i][j]=dp[i+1][j-1]+1;
            }
            else dp[i][j]=INF;
            for(int k=i;k<j;k++)
                dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]);
        }
       check(dp);
        for(int i=1;i<=r;i++)
        {
            A[i]=dp[1][i];
            if(you[i]==others[i])
            {
                if(i==1)
                    A[i]=0;
                else A[i]=A[i-1];
            }
            for(int k=1;k<i;k++)
                A[i]=min(A[i],A[k]+dp[k+1][i]);
        }
        cout<<A[r]<<endl;
    }
    return 0;
}

When we are focusing on solving problems, we usually prefer to stay in front of computers rather than go out for lunch. At this time, we
may call for food delivery.

Suppose there are N people living in a straight street that is just
lies on an X-coordinate axis. The ith person’s coordinate is Xi
meters. And in the street there is a take-out restaurant which has
coordinates X meters. One day at lunchtime, each person takes an order
from the restaurant at the same time. As a worker in the restaurant,
you need to start from the restaurant, send food to the N people, and
then come back to the restaurant. Your speed is V-1 meters per minute.

You know that the N people have different personal characters;
therefore they have different feeling on the time their food arrives.
Their feelings are measured by Displeasure Index. At the beginning,
the Displeasure Index for each person is 0. When waiting for the food,
the ith person will gain Bi Displeasure Index per minute.

If one’s Displeasure Index goes too high, he will not buy your food
any more. So you need to keep the sum of all people’s Displeasure
Index as low as possible in order to maximize your income. Your task
is to find the minimal sum of Displeasure Index.

Input

The input contains multiple test cases, separated with a blank line.
Each case is started with three integers N ( 1 <= N <= 1000 ), V ( V >
0), X ( X >= 0 ), then N lines followed. Each line contains two
integers Xi ( Xi >= 0 ), Bi ( Bi >= 0), which are described above.

You can safely assume that all numbers in the input and output will be
less than 231 - 1.

Please process to the end-of-file.

Output

For each test case please output a single number, which is the minimal
sum of Displeasure Index. One test case per line.

Sample Input

5 1 0 1 1 2 2 3 3 4 4 5 5

Sample Output

55

题意:有n个在不同坐标点的人同时点了外卖,且每过一分钟他们的不满意度都会不同地增加,已知外卖小哥的出发点及速度的倒数,求外卖小哥送完订单后,顾客的最小不满意的度。首先将n个地点排序,然后外卖小哥在一个区间送完外卖,要么在左端要么在右端。现将不满意度累加。分别求出区间两端路程*不满意度的最小值,最后 与倒数相乘。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
struct guest
{
    int loc;
    int des;
} tony[13521];
int f[14521],dp[2][1521][1521];
bool operator <(const guest&a,const guest&b)
{
    return a.loc<b.loc;
}
int main()
{
    int n,v,s;
    while(scanf("%d%d%d",&n,&v,&s)!=EOF)
    {
        memset(dp,INF,sizeof(dp));
        f[0]=0;
        for(int i=1; i<=n; i++)
            scanf("%d%d",&tony[i].loc,&tony[i].des);
        sort(tony+1,tony+n+1);
        for(int i=1; i<=n; i++)
            f[i]=(tony[i].des+f[i-1]);
        for(int i=1; i<=n; i++)
            dp[0][i][i]=dp[1][i][i]=abs(tony[i].loc-s)*f[n];
        for(int l=2; l<=n; l++)
            for(int i=1,j=l; j<=n; j++,i++)
            {
                dp[0][i][j]=min(dp[0][i][j],min(dp[0][i+1][j]+abs(tony[i+1].loc-tony[i].loc)*(f[n]-(f[j]-f[i])),dp[1][i+1][j]+abs(tony[j].loc-tony[i].loc)*(f[n]-(f[j]-f[i]))));
                dp[1][i][j]=min(dp[1][i][j],min(dp[1][i][j-1]+abs(tony[j].loc-tony[j-1].loc)*(f[n]-(f[j-1]-f[i-1])),dp[0][i][j-1]+abs(tony[j].loc-tony[i].loc)*(f[n]-(f[j-1]-f[i-1]))));
            }
        printf("%d\n",min(dp[0][1][n],dp[1][1][n])*v);//注意是速度的倒数
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值