Codeforces Round #756 (Div. 3)
A. Make Even
分析:
三种情况
- 本身是偶数, 答案为0
- 十进制最高位是偶数, 答案为1
- 十进制非最高位存在偶数, 答案为2
代码:
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize(2)
#define close(); ios::sync_with_stdio(false);
#define endl '\n'
#define rep(i, l, r) for(int i = l; i <= r; i++)
#define dwn(i, r, l) for(int i = r; i >= l; i--)
typedef long long LL;
const int N = 3e5+100;
int a[N];
void solve()
{
LL n; cin >> n;
if(n % 2 == 0) cout << 0 << endl;
else
{
int cnt = 0;
int x = 0;
while(n)
{
a[++cnt] = n%10;
if(a[cnt] % 2 == 0) x = cnt;
n/=10;
}
if(x == 0) cout << "-1" << endl;
else
{
if(x == cnt) cout << 1 << endl;
else cout << 2 << endl;
}
}
}
int main()
{
int T; cin >> T;
while(T--) solve();
// system("pause");
}
B. Team Composition: Programmers and Mathematicians
分析:
显然答案是 min ( ⌊ a + b 4 ⌋ , a , b ) \min(\lfloor\frac{a+b}{4}\rfloor, a, b) min(⌊4a+b⌋,a,b)
代码:
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize(2)
#define close(); ios::sync_with_stdio(false);
#define endl '\n'
#define rep(i, l, r) for(int i = l; i <= r; i++)
#define dwn(i, r, l) for(int i = r; i >= l; i--)
typedef long long LL;
const int N = 3e5+100;
void solve()
{
LL a, b; cin >> a >> b;
cout << min( (a+b)/4, min(a, b) ) << endl;
}
int main()
{
int T; cin >> T;
while(T--) solve();
// system("pause");
}
C. Polycarp Recovers the Permutation
分析:
容易发现数组a的最大值n, 一定出现在p数组的两端.
也就是说如果如果数组p的两端不存在n, 那么就是不合法的.
然后我们发现了一种构造做法:
在构造数组a的时候, 把n放在一端, 那么每次拿去构造数组p的元素都是在非n端的元素.
于是只需要把数组p翻转即可得到一种可行的数组a.
代码:
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize(2)
#define close(); ios::sync_with_stdio(false);
#define endl '\n'