Building Shops

题意:有一些教室,可以在这些位置建立超市,建立费用ci;不建花费就是其最左边超市的距离d;

思路:dp;不建立的地方到其最左边的超市之间全部都没建超市的!!!,要加上他们距离的和;map没开longlong哇~~

#include<algorithm>
#include<set>
#include<vector>
#include<queue>
#include<cmath>
#include<cstring>
#include<iostream>
#include<set>
#include<vector>
#include<queue>
#include<cmath>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<map>
#include<stack>
#include<string>
#include<ctime>
using namespace std;

#define sfi(i) scanf("%d",&i)
#define pri(i) printf("%d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-14
#define PI acos(-1)
#define lowbit(x) ((x)&(-x))
#define zero(x) (((x)>0?(x):-(x))<eps)
#define fl() printf("flag\n")
#define MOD(x) ((x%mod)+mod)%mod
#define FASTIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
ll gcd(ll a,ll b){while(b^=a^=b^=a%=b);return a;}
const int maxn=1e6+9;
const int mod=1e8+7;

inline ll read()
{
    ll f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return f*x;
}

ll dp[maxn][2];
pair<ll,ll>p[maxn];
bool cmp(pair<int,int> a,pair<int,int> b)
{
    return a.first<b.first;
}
int main()
{
    //FASTIO;
    //#define endl '\n'
    int n;
    while(cin>>n)
    {
        for(int i=0;i<=n;i++) dp[i][0]=dp[i][1]=1e18;
        for(int i=1;i<=n;i++)
        {
            cin>>p[i].first>>p[i].second;
        }
        sort(p+1,p+n+1,cmp);
        dp[1][1]=p[1].second;
        for(int i=2;i<=n;i++)
        {
            ll tmp=0;
            dp[i][1]=min(dp[i-1][0]+p[i].second,dp[i-1][1]+p[i].second);
            for(int j=i-1;j>=1;j--)
            {
                tmp+=(i-j)*(p[j+1].first-p[j].first);
                dp[i][0]=min(dp[j][1]+tmp,dp[i][0]);
            }
        }
        cout<<min(dp[n][0],dp[n][1])<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_39132605/article/details/88979782
今日推荐