zoukankan      html  css  js  c++  java
  • bzoj1597: [Usaco2008 Mar]土地购买

    农夫John准备扩大他的农场,他正在考虑N (1 <= N <= 50,000) 块长方形的土地. 每块土地的长宽满足(1 <= 宽 <
    = 1,000,000; 1 <= 长 <= 1,000,000). 每块土地的价格是它的面积,但FJ可以同时购买多快土地. 这些土地的价
    格是它们最大的长乘以它们最大的宽, 但是土地的长宽不能交换. 如果FJ买一块3x5的地和一块5x3的地,则他需要
    付5x5=25. FJ希望买下所有的土地,但是他发现分组来买这些土地可以节省经费. 他需要你帮助他找到最小的经费.
    Input

    • 第1行: 一个数: N
    • 第2..N+1行: 第i+1行包含两个数,分别为第i块土地的长和宽
      Output
    • 第一行: 最小的可行费用.
      sort,then X->increase,Y->decrease
      so dp[i]=min(dp[j]+p[i].fi*p[j+1].se);
    /**************************************************************
        Problem: 1597
        User: walfy
        Language: C++
        Result: Accepted
        Time:148 ms
        Memory:2660 kb
    ****************************************************************/
     
    //#pragma comment(linker, "/stack:200000000")
    //#pragma GCC optimize("Ofast,no-stack-protector")
    //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
    //#pragma GCC optimize("unroll-loops")
    #include<bits/stdc++.h>
    #define fi first
    #define se second
    #define mp make_pair
    #define pb push_back
    #define pi acos(-1.0)
    #define ll long long
    #define vi vector<int>
    #define mod 1000000007
    #define ld long double
    #define C 0.5772156649
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    #define pil pair<int,ll>
    #define pli pair<ll,int>
    #define pii pair<int,int>
    #define cd complex<double>
    #define ull unsigned long long
    #define base 1000000000000000000
    #define fio ios::sync_with_stdio(false);cin.tie(0)
     
    using namespace std;
     
    const double eps=1e-6;
    const int N=50000+10,maxn=23000000+10,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f;
     
    pii a[N],p[N];
    ll dp[N];
    int q[N];
    inline ll X(int i)
    {
        return p[i+1].se;
    }
    inline ll Y(int i)
    {
        return dp[i];
    }
    inline double slope(int i,int j)
    {
        return (Y(j)-Y(i))/(X(j)-X(i));
    }
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)scanf("%d%d",&a[i].fi,&a[i].se);
        sort(a,a+n);
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            while(cnt&&a[i].se>=p[cnt].se)--cnt;
            p[++cnt]=a[i];
        }
        int head=1,last=1;q[head]=0;
        for(int i=1;i<=cnt;i++)
        {
            while(head<last&&slope(q[head],q[head+1])>-p[i].fi)head++;
            int j=q[head];
            dp[i]=dp[j]+(ll)p[i].fi*p[j+1].se;
            while(head<last&&slope(q[last],q[last-1])<slope(q[last],i))last--;
            q[++last]=i;
        }
        printf("%lld
    ",dp[cnt]);
        return 0;
    }
    /********************
     
    ********************/
    
  • 相关阅读:
    P6406 [COCI2014-2015] Norma 分治+数学
    CF547D Mike and Fish 欧拉回路
    P6628 [省选联考 2020 B 卷] 丁香之路 欧拉路+最小生成树
    2020 CSP-S2 游记
    CF594D REQ 树状数组+质因数分解
    CF416E President's Path floyd
    CF1385F Removing Leaves 拓扑排序
    CF449C Jzzhu and Apples 思维题
    回溯法与八皇后问题
    codewars-7kyu:Sum of the first nth term of Series
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/9273226.html
Copyright © 2011-2022 走看看