QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#723162 | #7781. Sheep Eat Wolves | PoorGhost | WA | 0ms | 3812kb | C++20 | 3.1kb | 2024-11-07 21:19:01 | 2024-11-07 21:19:01 |
Judging History
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);
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代表回
set<node>st;
st.insert({x, y, 0, 0});
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});
// 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;
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: 3588kb
input:
4 4 3 1
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3812kb
input:
3 5 2 0
output:
5
result:
ok 1 number(s): "5"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3616kb
input:
2 5 1 1
output:
-1
result:
ok 1 number(s): "-1"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3808kb
input:
1 1 1 0
output:
1
result:
ok 1 number(s): "1"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3808kb
input:
3 3 1 1
output:
7
result:
ok 1 number(s): "7"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3552kb
input:
3 3 2 1
output:
3
result:
ok 1 number(s): "3"
Test #7:
score: -100
Wrong Answer
time: 0ms
memory: 3612kb
input:
10 9 1 10
output:
21
result:
wrong answer 1st numbers differ - expected: '19', found: '21'