Algoogle

Algorithm for Programming Contest

PKU 3618 Exploration

Category: PKU Tag: greedy

Exploration

問題概要


解法


原点に一番近い点を選んでいく.

コード


(3618.cpp) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>

using namespace std;

struct dist{
    int x;
    dist(){}
    dist(int x): x(x){}
    bool operator<(const dist &d)const{ return abs(x) < abs(d.x);}
};

int T, N, ans, tmp, pos;
vector<dist> v;

int main(){
    scanf("%d%d", &T,&N);
    v.resize(N);
    for(int i = 0; i < N; i++)
        scanf("%d", &v[i].x);
    sort(v.begin(), v.end());

    for(int i = 0; i < N; i++){
        T -= abs(v[i].x-pos);
        pos = v[i].x;
        if(T < 0) break;
        ans++;
    }
    printf("%d\n", ans);
}

Comments