zoukankan      html  css  js  c++  java
  • 【每日一题】26.codeJan与旅行 (贪心)

    补题链接:Here

    算法涉及:贪心

    思路:

    首先,我们处在p位置,显然要么向左走到相邻的城市,要么向右走走到相邻的城市。
    有一个不会证明但观察到的结论,从这两个位置走的话,只有四种可能。
    可能1:一直向左走
    可能2:一直向右走
    可能3:一直向左走到第i个城市,然后再向右走到第i+1个城市,然后再向左走到第i个城市,然后再向右走到第i+1个城市...
    可能4:一直向右走到第i个城市,然后再向左走到第i-1个城市,然后再向右走到第i个城市,然后再向左走到第i-1个城市...

    贴一个证明过程:Here

    using ll = long long;
    const int N = 1e5 + 6, M = 1e5;// val 1e9
    void solve() {
    	int n, m, p, pos = 0;
    	cin >> n >> m >> p;
    	ll a[n + 1];
    	for (int i = 1; i <= n; ++i) {
    		cin >> a[i];
    		if (a[i] < p)pos = i;
    	}
    	ll ans = 1e18;
    	for (int i = 1; i < n; ++i) {
    		if (i <= pos) {
    			if (pos - i  + 1 > m)continue;
    			ll num = m - (pos - i + 1), dist = p - a[i];
    			ans = min(ans, dist + num * (a[i + 1] - a[i]));
    			if (num > 0 and pos + 1 <= n)
    				ans = min(ans, (a[pos + 1] - p) * 2 + dist + (num - 1) *
    				          (a[i + 1] - a[i]));
    		} else {
    			if (i - pos > m)continue;
    			ll num = m - (i - pos), dist = a[i] - p;
    			ans = min(ans, dist + num * (a[i + 1] - a[i]));
    			if (num > 0 and pos >= 1)
    				ans = min(ans, (p - a[pos]) * 2 + dist + (num - 1) *
    				          (a[i + 1] - a[i]));
    		}
    	}
    	cout << ans << endl;
    }
    

    The desire of his soul is the prophecy of his fate
    你灵魂的欲望,是你命运的先知。

  • 相关阅读:
    无向图判断三元环
    POJ 2785 4 Values whose Sum is 0
    lower_bound和upper_bound
    2153: 2018湖南多校第二场-20180407(网络同步赛)
    前缀和、前缀积
    hdu 4686 Arc of Dream
    UVA Recurrences 矩阵相乘+快速幂
    UVA 11149 Power of Matrix 构造矩阵
    poj 1258 Agri-Net prim模板 prim与dijkstra的区别
    poj 1182 食物链 (并查集)
  • 原文地址:https://www.cnblogs.com/RioTian/p/14770929.html
Copyright © 2011-2022 走看看