D. Winter Is Coming 贪心(好题)

Posted stupid_one

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了D. Winter Is Coming 贪心(好题)相关的知识,希望对你有一定的参考价值。

http://codeforces.com/contest/747/problem/D

大概的思路就是找到所有两个负数夹着的线段,优先覆盖最小的长度。使得那时候不用换鞋,是最优的。

但是这里有个坑点,就是最后一段,如果最后一段的长度和中间某一段的长度相等,那么应该优先覆盖中间那段,因为中间的那些,如果没覆盖,则换鞋2次,而最后的那一段,换鞋只需一次。

22 11
1 -1 1 2 3 -1 1 2 3 4 5 6 7 -1 1 2 3 4 -1 1 2 3

 

答案是4

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define ios ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;


#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 2e5 + 20;
int a[maxn];
struct node {
    int val, id;
    node(int vval, int iid) : val(vval), id(iid) {}
    bool operator < (const struct node & rhs) const {
        if (val != rhs.val) return val < rhs.val;
        else return id < rhs.id;
    }
};
vector<int>pos;
vector<int>dis;
set<struct node>ss;
bool vis[maxn];
void work() {
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        if (a[i] < 0) pos.push_back(i);
    }
    if (k < pos.size()) {
        cout << -1 << endl;
        return;
    }
    if (pos.size() == 0) {
        cout << 0 << endl;
        return;
    }
    for (int i = 1; i < pos.size(); ++i) {
        dis.push_back(pos[i] - pos[i - 1] - 1);
        ss.insert(node(pos[i] - pos[i - 1] - 1, i - 1));
    }
    int lef = k - pos.size();
    for (set<struct node> :: iterator it = ss.begin(); it != ss.end(); ++it) {
//        cout << it->val << endl;
        if (lef >= it->val) {
//            cout << "ff" << endl;
            lef -= it->val;
            vis[it->id] = true;
        } else break;
    }
    int ans = 1;
    for (int i = 0; i < dis.size(); ++i) {
        if (vis[i]) continue;
        ans += 2;
    }
    if (lef < n - pos.back()) {
        ans += 1;
    }
    cout << ans << endl;
}

int main() {
#ifdef local
    freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
    work();
    return 0;
}
View Code

 

以上是关于D. Winter Is Coming 贪心(好题)的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces 747D Winter Is Coming

CF747D Winter Is Coming

2016级算法第三次上机-G.Winter is coming

(容斥)Codeforces Round #428 (Div. 2) D. Winter is here

Codeforces Round #428 (Div. 2) D. Winter is here[数论II][容斥原理]

Codeforces Round #428 (Div. 2) D. Winter is here 数学