为什么贪心WA #4#5
查看原帖
为什么贪心WA #4#5
833124
BIOS楼主2023/5/13 20:43
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int N = 2e4 + 5;
long long f[N];
int n, last, a, b, k1, k2, dist, pist;
struct node
{
    int l, r;
};
vector<node> ac;
int main()
{
    cin >> n;
    ac.push_back({-1, -1});
    for (int i = 1; i <= n; i++)
        scanf("%d%d", &a, &b), ac.push_back({a, b});
    f[1] = ac[1].r, last = ac[1].r;
    for (int i = 2; i < n; i++)
    {
        k1 = fabs(last - ac[i].l), k2 = fabs(last - ac[i].r);
        if (last <= ac[i].l || last >= ac[i].r)
        {
            if (k1 > k2)
                dist = k1, last = ac[i].l;
            else
                dist = k2, last = ac[i].r;
            f[i] = f[i - 1] + dist + 1;
        }
        else
        {
            if (k1 > k2)
                dist = k2, last = ac[i].l, pist = k1;
            else
                dist = k1, last = ac[i].r, pist = k2;
            f[i] = f[i - 1] + dist * 2 + pist + 1;
        }
    }
    if (n > 1)
    {
        k1 = fabs(last - ac[n].l);
        f[n] = f[n - 1] + k1 + n - ac[n].l;
    }
    cout << f[n] << endl;
}

基于模拟的贪心,感觉没啥问题啊,有什么数据hack到我了吗,4和5都是比期望值高1

2023/5/13 20:43
加载中...