Solution to a problem Luogu P2695 [work] Knight

Topic Link Luogu P2695

Method: Enumeration of violence

This question is an enumeration Knight

Then determine the knight can not cut the monster's head

Time complexity is large, but can live

Look at the code it

#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>

const int MaxN = 20000 + 10;

using namespace std;

int n, m, s, sum;

int money[MaxN], head[MaxN];
bool people[MaxN];//判断第i个头被砍了没

int cmp(int x, int y) {
    return x < y;   //排序
}
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        scanf("%d", &head[i]);
    for (int i = 1; i <= m; i++)
        scanf("%d", &money[i]);
    sort(head + 1, head + 1 + n, cmp);
    sort(money + 1, money + 1 + m, cmp);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if (!people[j] && money[j] >= head[i]) {//如果没被砍并且骑士可以砍这个头
                people[j] = true;
                sum += money[j];
                s++;
                break;
            }
    if (s == n)//头全砍完了
        cout << sum;
    else cout << "you died!";
    return 0;//好习惯
}

\ (update ~~ 2019.11.18 \)
to modify the code wind

Guess you like

Origin www.cnblogs.com/chz-hc/p/12220879.html