QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#723189#7781. Sheep Eat WolvesPoorGhostWA 0ms3612kbC++203.3kb2024-11-07 21:22:482024-11-07 21:22:49

Judging History

你现在查看的是最新测评结果

  • [2024-11-07 21:22:49]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3612kb
  • [2024-11-07 21:22:48]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
const int N = 1e5 + 5, M = 1e4 + 5;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f3f3f3f3f;
int x, y, p, q;
struct node
{
    int x, y, p, q;
    bool operator<(const node & w)const {
        if (x != w.x) return x < w.x;
        if (y != w.y) return y < w.y;
        if (p != w.p) return p < w.p;
        return q < w.q;
    }
};
void solve()
{
    cin >> x >> y >> p >> q;
    if (y - p > x + q)
    {
        cout << -1 << endl;
        return;
    }
    int op = max(y - q, 0ll);
    // cout << "op = " << op << endl;
    if (op <= p)
    {   //只运羊就可以
        int ans = ((x - op) / p + ((x - op) % p != 0)) * 2 + (op > 0);
        ans -= (op == 0);
        cout << ans << endl;
        return;
    }
    int ans = 0;
    int xx = 0, yy = 0, idx = 0;//0代表去,1代表回
    set<node>st;
    st.insert({x, y, 0, 0});
    while (x != 0)
    {
        // cout << x << " " << y << " " << xx << " " << yy << endl;
        if (!idx)
        {
            if (x <= p)//能直接把羊全部运走
            {
                ans++;
                break;
            }
            if (x + q < y)//要把狼运过去
            {
                int tmp = min(p, max(y - x - q, y));
                y -= tmp;
                yy += tmp;
            }
            else
            {
                //可以运羊
                int tmp = min({p, x + q - y, x});
                // cout << "tmp = " << tmp << endl;
                if (tmp == 0)
                {
                    //要同时运羊和狼
                    int tmp = min({x, y, p / 2});
                    if (tmp == 0)
                    {
                        //运狼
                        int res = min({p, y});
                        // cout << "res = " << res << endl;
                        y -= res, yy += res;
                    }
                    else
                    {
                        x -= tmp, y -= tmp;
                        xx += tmp, yy += tmp;
                    }
                }
                else
                {
                    x -= tmp;
                    xx += tmp;
                }
            }
            ans++;
        }
        else
        {
            if (xx + q < yy && xx > 0)
            {
                int tmp = min({p, yy - xx + q, yy});
                yy -= tmp;
                y += tmp;
            }
            ans++;
        }
        if (idx == 1)
        {
            if (st.count({x, y, xx, yy}))
            {
                cout << -1 << endl;
                return;
            }
            st.insert({x, y, xx, yy});
        }

        // cout << x << " " << y << " " << xx << " " << yy << endl;
        // cout << "------------------" << endl;
        if ((x + q < y && x > 0 && idx == 0) || (xx + q < yy && xx > 0 && idx == 1))
        {
            cout << -1 << endl;
            return;
        }
        idx ^= 1;
    }
    cout << ans << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int test = 1;
    // cin >> test;
    for (int i = 1; i <= test; i++)
    {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3524kb

input:

4 4 3 1

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3520kb

input:

3 5 2 0

output:

5

result:

ok 1 number(s): "5"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3612kb

input:

2 5 1 1

output:

-1

result:

ok 1 number(s): "-1"

Test #4:

score: 0
Accepted
time: 0ms
memory: 3604kb

input:

1 1 1 0

output:

1

result:

ok 1 number(s): "1"

Test #5:

score: 0
Accepted
time: 0ms
memory: 3600kb

input:

3 3 1 1

output:

7

result:

ok 1 number(s): "7"

Test #6:

score: 0
Accepted
time: 0ms
memory: 3604kb

input:

3 3 2 1

output:

3

result:

ok 1 number(s): "3"

Test #7:

score: 0
Accepted
time: 0ms
memory: 3480kb

input:

10 9 1 10

output:

19

result:

ok 1 number(s): "19"

Test #8:

score: 0
Accepted
time: 0ms
memory: 3528kb

input:

15 20 2 5

output:

27

result:

ok 1 number(s): "27"

Test #9:

score: -100
Wrong Answer
time: 0ms
memory: 3476kb

input:

18 40 16 7

output:

-1

result:

wrong answer 1st numbers differ - expected: '5', found: '-1'