B. 网格(dp / 二维偏序问题) (智算之道复赛高校组)

传送门

B. 网格(dp / 二维偏序问题) (智算之道复赛高校组)_第1张图片
B. 网格(dp / 二维偏序问题) (智算之道复赛高校组)_第2张图片
B. 网格(dp / 二维偏序问题) (智算之道复赛高校组)_第3张图片
思路:

  • 显然若w2 > w1*2的话,魔法点就没有什么意义了。
  • 在听了学姐说的k^2建图后,个人觉得只需要考虑k个魔法点,但是不知道为什么就是只有20分,呜呜。

代码实现:

#include
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 2e5 + 5;

int n, k, w1, w2;
map<pii, int> mp;

signed main()
{
    IOS;

    cin >> n >> k >> w1 >> w2;
    mp[{n, n}] = n*2*w1;
    if(w2 >= w1){
        cout << mp[{n, n}] << endl;
        return 0;
    }

    for(int i = 0; i < k; i ++){
        int x, y; cin >> x >> y;
        if(x+1 <= n && y+1 <= n){
            mp[{x+1, y+1}] = (x+y)*w1+w2;
            for(map<pii, int>::iterator it = mp.begin(); it != mp.end(); it ++){
                int xx = it -> first.first, yy = it -> first.second;
                if(xx <= x || yy <= y)
                     mp[{x+1, y+1}] = min(mp[{x+1, y+1}], mp[{xx, yy}] + (x-xx+y-yy)*w1+w2);
            }
        }
    }

    for(map<pii, int>::iterator it = mp.begin(); it != mp.end(); it ++){
        int xx = it -> first.first, yy = it -> first.second;
        if(xx <= n || yy <= n){
            mp[{n, n}] = min(mp[{n, n}], mp[{xx, yy}] + (n*2-xx-yy)*w1);
        }
    }
    cout << mp[{n, n}] << endl;

    return 0;
}

队友AC的dp代码:

#include
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 2e5 + 5;

inline void read(int &x){
    char t=getchar();
    while(!isdigit(t)) t=getchar();
    for(x=t^48,t=getchar();isdigit(t);t=getchar()) x=x*10+(t^48);
}

int n, k, w1, w2, dp[2010], m, a, b;

struct node{
    int x,y;
};
bool cmp(node a,node b){
    if(a.x == b.x) return a.y < b.y;
    return a.x < b.x;
}
vector<node> v;

signed main(){
	IOS;
    read(n); read(k); read(w1); read(w2);
    while(k --){
        read(a); read(b);
        if(a == n || b == n) continue;
        dp[m ++] = 1;
        v.push_back({a, b});
    }
    sort(v.begin(),v.end(),cmp);
    if(w2 >= 2*w1) cout << 2*n*w1 << endl;
    else{
        int sum = 0, maxn = 0;
        for(int i = 1; i < v.size(); i ++)
            for(int j = 0; j < i; j ++)
                if(v[i].x > v[j].x && v[i].y > v[j].y)
                    dp[i] = max(dp[i], dp[j]+1);
        for(int i = 0; i < m; i ++)
            maxn = max(maxn, dp[i]);
        cout << (2*n-maxn*2)*w1+w2*maxn << endl;
    }
    return 0;
}

你可能感兴趣的:(比赛&训练,不大会合集)