QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#723092#7781. Sheep Eat WolvesPoorGhostWA 0ms3560kbC++202.4kb2024-11-07 21:09:202024-11-07 21:09:22

Judging History

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

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

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;
void solve()
{
    cin >> x >> y >> p >> q;
    if (y - p > x + q)
    {
        cout << -1 << endl;
        return;
    }
    int op = max(y - q, 0ll);
    if (op <= p)
    {   //只运羊就可以
        int ans = ((x - op) / p + ((x - op) % p != 0)) * 2 + 1;
        cout << ans << endl;
        return;
    }
    int ans = 0;
    int xx = 0, yy = 0, idx = 0;//0代表去,1代表回
    while (x != 0)
    {
        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});
                if (tmp == 0)
                {
                    //要同时运羊和狼
                    int tmp = min({x, y, p / 2});
                    if (tmp == 0)
                    {
                        cout << -1 << endl;
                        return;
                    }
                    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++;
        }
        // cout << x << " " << y << " " << xx << " " << yy << 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;
}

詳細信息

Test #1:

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

input:

4 4 3 1

output:

3

result:

ok 1 number(s): "3"

Test #2:

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

input:

3 5 2 0

output:

5

result:

ok 1 number(s): "5"

Test #3:

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

input:

2 5 1 1

output:

-1

result:

ok 1 number(s): "-1"

Test #4:

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

input:

1 1 1 0

output:

1

result:

ok 1 number(s): "1"

Test #5:

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

input:

3 3 1 1

output:

-1

result:

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