比赛链接:Codeforces Round 998 (Div. 3)
A-Fibonacciness
思路:a[3] 的值只可能有三种情况,我们枚举这三种情况找出最大所求即可。
#include <bits/stdc++.h>
#include <unordered_set>
#define endl '\n'
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define LL long long
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
using namespace std;
int t;
int a[6], ans[4];
int main()
{
IOS;
cin >> t;
while(t--)
{
cin >> a[1] >> a[2] >> a[4] >> a[5];
int maxn = 0;
memset(ans, 0, sizeof(ans));
rep(k,1,3)
{
if(k==1)
a[3] = a[1] + a[2];
else if(k==2)
a[3] = a[4] - a[2];
else if(k==3)
a[3] = a[5] - a[4];
rep(i, 1, 3)
{
if (a[i] + a[i + 1] == a[i + 2])
ans[k]++;
}
maxn = max(ans[k], maxn);
}
cout << maxn << endl;
}
return 0;
}
B-Farmer John's Card Game
思路:观察发现,奶牛们应该按照从小到大的顺序出牌,我们将所有牌排序,模拟此过程即可。如果在顺序出牌的过程中,有牛前后两次出牌间隔不同,则所求出牌顺序 p 不存在。
#include <bits/stdc++.h>
#include <unordered_set>
#define endl '\n'
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define LL long long
#define inf 0x3f3f3f3f3f
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
using namespace std;
int t;
vector<int> vec;
unordered_map<int, int> niu;
unordered_map<int, int> last;
int main()
{
IOS;
cin >> t;
while (t--)
{
last.clear();
vec.clear();
niu.clear();
int n, m;
cin >> n >> m;
rep(i, 1, n)
{
rep(j, 1, m)
{
int p;
cin >> p;
vec.push_back(p);
niu[p] = i;
}
}
sort(vec.begin(), vec.end());
int ci = 1;
bool no = 0;
for (auto &u : vec)
{
if (ci > n && ci - last[niu[u]] != n)
{
no = 1;
break;
}
last[niu[u]] = ci;
ci++;
}
if (no)
cout << -1 << endl;
else
{
int ci1 = 0;
for (auto &u : vec)
{
if (ci1 == n)
break;
cout << niu[u] << " ";
ci1++;
}
cout << endl;
}
}
return 0;
}
C-Game of Mathletes
思路:由于只有鲍勃对分数有贡献且为后手,即使爱丽丝每次尽量选择没有匹配项的数,鲍勃都能将分数最大化,所以我们只需模拟鲍勃的选数逻辑即可。遍历所有的数 x[i],如果匹配的数 p 与之相等,那么 x[i] 分数对分数的贡献为其数量 vs[x[i]] 的一半,如果不相等,分数取决于 x[i] 和 p 的数量的最小值(如果 p 不存在则最小值为 0 )。
#include <bits/stdc++.h>
#include <unordered_set>
#define endl '\n'
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define LL long long
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
using namespace std;
int t;
vector<int> x;
unordered_map<int, int> vs;
int main()
{
cin >> t;
while (t--)
{
int n, k;
cin >> n >> k;
x.clear();
x.resize(n + 1);
vs.clear();
rep(i, 1, n)
{
cin >> x[i];
vs[x[i]]++;
}
int s = 0;
rep(i, 1, n)
{
int p = k - x[i];//匹配的数
if (p <= 0)
continue;
if (p == x[i])
s += vs[x[i]] / 2;
else
s += min(vs[x[i]], vs[p]);
vs[x[i]] = 0;
vs[p] = 0;
}
cout << s << endl;
}
return 0;
}
D-Subtract Min Sort
思路:观察发现,我们不能不按顺序操作,因为这样会在非零的数中间产生 0 从而使得下一步操作复杂化。于是我们猜测应按顺序模拟操作,只要操作后两个数的大小关系合法我们就尽量操作。题设的操作不会改变被操作的两个数的相对大小,只要在操作过程中遇到了逆序对则必定不可能变成非递减序列。
#include <bits/stdc++.h>
#include <unordered_set>
#define endl '\n'
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define LL long long
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
using namespace std;
int t;
void solve()
{
int n;
cin >> n;
vector<int> a(n + 1);
rep(i, 1, n)
cin >> a[i];
rep(i, 1, n - 1)
{
if (a[i] > a[i + 1])
{
cout << "NO" << endl;
return;
}
else
{
int d = min(a[i], a[i + 1]);
if (a[i] - d >= a[i - 1])
{
a[i] -= d;
a[i + 1] -= d;
}
}
}
cout << "YES" << endl;
return;
}
int main()
{
IOS;
cin >> t;
while(t--)
solve();
return 0;
}