cf1556A. A Variety of Operations
Posted Jozky86
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cf1556A. A Variety of Operations相关的知识,希望对你有一定的参考价值。
cf1556A. A Variety of Operations
题意:
有两个数a,b一开始都是0,现在有三种操作:
- 给a和b都加k
- a加k,b减k
- a减k,b加k
问从a=0,b=0到a=c,b=d最少需要几步?
题解:
k=(c+d)/2
我们可以先用第一种操作将0,0都加到k,k.然后一个减,一个加,实现到c,d,这样需要两步
如果c和d的差为奇数,则无法通过操作实现
代码:
// Problem: A. A Variety of Operations
// Contest: Codeforces - Deltix Round, Summer 2021 (open for everyone, rated, Div. 1 + Div. 2)
// URL: https://codeforces.com/contest/1556/problem/A
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// Data:2021-08-31 23:35:41
// By Jozky
#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
void read(){};
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{
x= 0;
char c= getchar();
bool flag= 0;
while (c < '0' || c > '9')
flag|= (c == '-'), c= getchar();
while (c >= '0' && c <= '9')
x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();
if (flag)
x= -x;
read(Ar...);
}
template <typename T> inline void write(T x)
{
if (x < 0) {
x= ~(x - 1);
putchar('-');
}
if (x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef LOCAL
startTime= clock();
freopen("in.txt", "r", stdin);
#endif
}
void Time_test()
{
#ifdef LOCAL
endTime= clock();
printf("\\nRun Time:%lfs\\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
int main()
{
//rd_test();
int t;
read(t);
while (t--) {
int c, d;
read(c, d);
if (c > d)
swap(c, d);
int w= d - c;
if (d == 0 && c == 0)
printf("0\\n");
else if (w == 0)
printf("1\\n");
else {
if (w % 2 == 1)
printf("-1\\n");
else
printf("2\\n");
}
}
return 0;
//Time_test();
}
以上是关于cf1556A. A Variety of Operations的主要内容,如果未能解决你的问题,请参考以下文章
cf1556Compressed Bracket Sequence