Codeforces 583 DIV2 Robot's Task 贪心

原题链接:http://codeforces.com/problemset/problem/583/B

题意:

就。。要打开一个电脑,必须至少先打开其他若干电脑,每次转向有个花费,让你设计一个序列,使得总花费最小。

题解:

就傻傻的走就好。。从左走到右,再走回来,更新序列和答案就好。

代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#define MAX_N 1003
using namespace std;

int a[MAX_N];
int n;

int cnt=0;
int ans=0;
int d=1;

bool used[MAX_N];

int main() {
    cin.sync_with_stdio(false);
    cin >> n;
    for (int i = 0; i < n; i++)cin >> a[i];
    int x = 0;
    while (cnt != n) {
        if (cnt >= a[x] && used[x] == 0) {
            cnt++;
            used[x] = 1;
        }
        if (cnt == n)break;
        x += d;
        if (x == n) {
            x = n - 2;
            d = -1;
            ans++;
        }
        if (x == -1) {
            x = 1;
            d = 1;
            ans++;
        }
    }
    cout << ans << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/HarryGuo2012/p/4854890.html