POJ 3486 & HDU 1913 Computers解题报告 有关线段树的或dp 特别是线段树问题,查询l,r段,mid=tree[root].left+tree[root].right>>1

Everybody is fond of computers, but buying a new one is always a money challenge. Fortunately, there is always a convenient way to deal with. You can replace your computer and get a brand new one, thus saving some maintenance cost. Of course, you must pay a fixed cost for each new computer you get.

Suppose you are considering an n year period over which you want to have a computer. Suppose you buy a new computer in year y1<=y<=n Then you have to pay a fixed cost c, in the year y, and a maintenance cost m(y,z) each year you own that computer, starting from year y through the year zz<=n, when you plan to buy - eventually - another computer.

Write a program that computes the minimum cost of having a computer over the n year period.

Input

The program input is from a text file. Each data set in the file stands for a particular set of costs. A data set starts with the cost c for getting a new computer. Follows the number n of years, and the maintenance costs m(y,z)y=1..nz=y..n. The program prints the minimum cost of having a computer throughout the nyear period.

White spaces can occur freely in the input. The input data are correct and terminate with an end of file.

Output

For each set of data the program prints the result to the standard output from the beginning of a line.

Sample Input

3
3
5 7 50      ---‘1’
6 8         ----‘2’
10          ---‘3’
//这里说明一下,在第i年时,如果买电脑的话只能购买第i年的的电脑,例如这个输入样例中在第二年时要买
电脑的话只能买上面标记‘2’这个电脑,而不能买标记为‘3’的电脑
这个可以理解为每一年的卖的电脑是不同的,输入样例给出的是在第i年出售的电脑的维修价格一直到第n年这种电脑所需总的维修价格。
也就是说不能在第一年和第二年用‘1’编号的电脑在第3年的时候买了‘2’编号的电脑,3年所花费的money是 3+7+3+6=19  这个理解是错的
因为在第三年如果要买电脑的话只能买编号‘3’的电脑
而下面的Sample Output的19应该是应该是编号‘1’的电脑用了一年,在第二年的时候买了编号‘2’的电脑,一直用到了第三年
花费为3+5+3+8=19


Sample Output

19

Hint

An input/output sample is shown above. There is a single data set. The cost for getting a new computer is c=3. The time period n is n=3 years, and the maintenance costs are:

  • For the first computer, which is certainly bought: m(1,1)=5m(1,2)=7m(1,3)=50,
  • For the second computer, in the event the current computer is replaced: m(2,2)=6m(2,3)=8,
  • For the third computer, in the event the current computer is replaced: m(3,3)=10.
     

下面是我用线段树敲的代码,结果竟然ac了,感觉这题这几用dp更简短,下面的代码还是有些烦杂

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int maxn=1e6+10;
struct tree
{
    int left,right;
    int val;

}tree[maxn<<2];
int c;
int a[maxn];//用来记录第1年到第i年的最小的花费的money

void pushup(int root)
{
    tree[root].val=max(tree[root<<1].val,tree[root<<1|1].val);
}
void build(int l,int r,int root)
{
    tree[root].left=l;
    tree[root].right=r;
    tree[root].val=0;
    if(l==r)
    {
        int x;
        scanf("%d",&x);
        tree[root].val=x+c;
        a[l]=tree[root].val;
        return;
    }
    int mid=(l+r)>>1;
    build(l,mid,root<<1);
    build(mid+1,r,root<<1|1);
    pushup(root);
}
void update(int l,int r,int root,int i)
{
    if(l==r)
    {
        int x;
        scanf("%d",&x);
        if(a[i-1]+x+c<a[l])
        {
            a[l]=a[i-1]+x+c;
            tree[root].val=a[l];

        }
        return ;
    }
    int mid=l+r>>1;
    if(l<=mid) update(l,mid,root<<1,i);
    if(r>mid) update(mid+1,r,root<<1|1,i);

}
int main()
{
    int n;
    int x,y,z;
    while(scanf("%d",&c)!=EOF)
    {
        memset(a,0,sizeof(a));
        scanf("%d",&n);
        build(1,n,1);
        for(int i=2;i<=n;++i)
        {
            update(i,n,1,i);
            
        }
        printf("%d\n",a[n]);

    }
    return 0;
}

下面是没用线段树的:(思想都是一样的)

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int maxn=1e6+10;

int a[maxn];//用来记录第1年到第i年的最小的花费的money


int main()
{
    int n;
    int x,y,z;
    int c;
    while(scanf("%d",&c)!=EOF)
    {
        memset(a,0,sizeof(a));
        scanf("%d",&n);

        for(int i=1;i<=n;++i)
        {
            for(int j=i;j<=n;j++)
            {
                scanf("%d",&x);
                if(i==1)
                    a[j]=x+c;
                else
                {
                     a[j]=min(a[i-1]+x+c,a[j]);
                }

            }


        }
        printf("%d\n",a[n]);

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42373330/article/details/81745369