Codeforces Round 988 (Div. 3) A~D
Codeforces Round 988 (Div. 3)
A. Twice
这个题就是简单的贪心题,在相同大小的元素里面,我们只能选择两个来对评分更新,所以最多能更新(相同元素的个数) / 2次,记录相同元素的个数就行了
// Problem: A. Twice
// Contest: Codeforces - Codeforces Round 988 (Div. 3)
// URL: https://codeforces.com/contest/2037/problem/0
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//by codeforcer ——
// ____ _ _ _ _ _ _ ____ _
// / ___|| | | || | | || | | | |___ \ | |
//| | | |_| || |_| || |_| | __) | | |
//| | | _ || _ || _ | |__ < | |
//| |___ | | | || | | || | | | ___) | | |
// \____||_| |_||_| |_||_| |_| |____ / |_|
#include<bits/stdc++.h>
using namespace std;
typedef int E;
typedef long long LL;
typedef pair<int, int> PII;
typedef tuple<int, int, int> PIII;
typedef tuple<LL, LL, LL> PLLL;
typedef pair<long long, long long> PLL;
typedef unsigned long long ULL;
#define endl '\n'
#define vec vector
#define pb push_back
#define pob pop_back
#define fir first
#define sec second
#define maxINT 0x3f3f3f3f
#define maxLL 0x3f3f3f3f3f3f3f3fLL
#define umap unordered_map
#define uset unordered_set
#define maxheap priority_queue<E, vector<E>, less<E>>
#define minheap priority_queue<E, vector<E>, greater<E>>
#define prvec(a) \
for (int i = 0; i < (a).size(); i++) { \
cout << (a)[i] << " "; \
} \
cout << endl;
#define debug0(a) \
cout << #a << ": "; \
for (int k = 0; k < (a).size(); k++) { \
cout << (a)[k] << " "; \
} \
cout << endl;
#define debug1(a) \
cout << #a << ": "; \
for (int k = 1; k <= (a).size(); k++) { \
cout << (a)[k] << " "; \
} \
cout << endl;
LL gcd(LL a, LL b) { return (b) ? gcd(b, a % b) : a; }
LL exgcd(LL a, LL b, LL &x, LL &y) {if (b == 0) { x = 1, y = 0; return a; }LL gcd = exgcd(b, a % b, y, x);y -= a / b * x;return gcd;}
LL qmi(LL a, LL b, LL mod) {LL res = 1;while (b) {if (b & 1) res = res * a % mod;a = a * a % mod;b >>= 1;}return res;}
const int N = 200010;
void solve() {
int n;
cin>>n;
map<int,int> mp;
for(int i = 0;i < n;i ++)
{
int x;
cin>>x;
mp[x] ++;
}
long long ans = 0;
for(auto p : mp)
{
int l = p.first,r = p.second;
if(r >= 2)
{
ans += r / 2;
}
}
cout<<ans<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;cin>>n;while(n --)
solve();
return 0;
}
B. Intercepted Inputs
这个题目的题意要理解清楚,他说的是输入已经给出来了,然后输入的数据被打乱了,也就是说题目需要的n m就在给的输入里面。输入n个数也就代表n * m = (n - 2) ,剩下的n - 2个数就是二维数组里面的数,所以我们只需要让(n - 2) % n == 0,然后看(n - 2) / n 在不在输入的n个数里面就行。注意有n = 6 ,然后给的数据是2 1 4 5 5 5的情况,这个直接枚举就会导致答案是2,因为(6 - 2) / 2 = 2但是2只出现了依次所以不是答案不是2 2应该是1 4
// Problem: B. Intercepted Inputs
// Contest: Codeforces - Codeforces Round 988 (Div. 3)
// URL: https://codeforces.com/contest/2037/problem/B
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//by codeforcer ——
// ____ _ _ _ _ _ _ ____ _
// / ___|| | | || | | || | | | |___ \ | |
//| | | |_| || |_| || |_| | __) | | |
//| | | _ || _ || _ | |__ < | |
//| |___ | | | || | | || | | | ___) | | |
// \____||_| |_||_| |_||_| |_| |____ / |_|
#include<bits/stdc++.h>
using namespace std;
typedef int E;
typedef long long LL;
typedef pair<int, int> PII;
typedef tuple<int, int, int> PIII;
typedef tuple<LL, LL, LL> PLLL;
typedef pair<long long, long long> PLL;
typedef unsigned long long ULL;
#define endl '\n'
#define vec vector
#define pb push_back
#define pob pop_back
#define fir first
#define sec second
#define maxINT 0x3f3f3f3f
#define maxLL 0x3f3f3f3f3f3f3f3fLL
#define umap unordered_map
#define uset unordered_set
#define maxheap priority_queue<E, vector<E>, less<E>>
#define minheap priority_queue<E, vector<E>, greater<E>>
#define prvec(a) \
for (int i = 0; i < (a).size(); i++) { \
cout << (a)[i] << " "; \
} \
cout << endl;
#define debug0(a) \
cout << #a << ": "; \
for (int k = 0; k < (a).size(); k++) { \
cout << (a)[k] << " "; \
} \
cout << endl;
#define debug1(a) \
cout << #a << ": "; \
for (int k = 1; k <= (a).size(); k++) { \
cout << (a)[k] << " "; \
} \
cout << endl;
LL gcd(LL a, LL b) { return (b) ? gcd(b, a % b) : a; }
LL exgcd(LL a, LL b, LL &x, LL &y) {if (b == 0) { x = 1, y = 0; return a; }LL gcd = exgcd(b, a % b, y, x);y -= a / b * x;return gcd;}
LL qmi(LL a, LL b, LL mod) {LL res = 1;while (b) {if (b & 1) res = res * a % mod;a = a * a % mod;b >>= 1;}return res;}
const int N = 200010;
void solve() {
int n;
cin>>n;
vector<int> a(n + 1);
map<int,int> mp;
map<int,int> st;
for(int i = 0; i <n;i ++)
{
cin>>a[i];
st[a[i]] ++;
if((n - 2) % a[i] == 0)
{
mp[a[i]] = (n - 2)/a[i];
}
}
for(int i = 0;i < n;i ++)
{
if(st[mp[a[i]]] != 0 )
{
if(mp[a[i]] == a[i] && st[a[i]] <= 1) continue;
cout<<a[i]<<" "<<mp[a[i]]<<endl;
return;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;cin>>n;while(n --)
solve();
return 0;
}
C. Superultra's Favorite Permutation
这个也是贪心题目,首先我们考虑奇数和偶数分开的情况
- 奇数和奇数放一起有:奇数加奇数肯定是偶数,偶数一定不是质数
- 偶数和偶数放一起有:偶数加偶数一定是偶数,偶数一定不是质数
所以我们只需要考虑偶数和奇数之间的分界点,我们需要找到一个偶数和一个奇数加在一起不是质数,最小的情况就是n = 5 的时候,排列是1 3 5 4 2,n逐渐增大奇数往排列的前面放,偶数往后面放
// Problem: C. Superultra's Favorite Permutation
// Contest: Codeforces - Codeforces Round 988 (Div. 3)
// URL: https://codeforces.com/contest/2037/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//by codeforcer ——
// ____ _ _ _ _ _ _ ____ _
// / ___|| | | || | | || | | | |___ \ | |
//| | | |_| || |_| || |_| | __) | | |
//| | | _ || _ || _ | |__ < | |
//| |___ | | | || | | || | | | ___) | | |
// \____||_| |_||_| |_||_| |_| |____ / |_|
#include<bits/stdc++.h>
using namespace std;
typedef int E;
typedef long long LL;
typedef pair<int, int> PII;
typedef tuple<int, int, int> PIII;
typedef tuple<LL, LL, LL> PLLL;
typedef pair<long long, long long> PLL;
typedef unsigned long long ULL;
#define endl '\n'
#define vec vector
#define pb push_back
#define pob pop_back
#define fir first
#define sec second
#define maxINT 0x3f3f3f3f
#define maxLL 0x3f3f3f3f3f3f3f3fLL
#define umap unordered_map
#define uset unordered_set
#define maxheap priority_queue<E, vector<E>, less<E>>
#define minheap priority_queue<E, vector<E>, greater<E>>
#define prvec(a) \
for (int i = 0; i < (a).size(); i++) { \
cout << (a)[i] << " "; \
} \
cout << endl;
#define debug0(a) \
cout << #a << ": "; \
for (int k = 0; k < (a).size(); k++) { \
cout << (a)[k] << " "; \
} \
cout << endl;
#define debug1(a) \
cout << #a << ": "; \
for (int k = 1; k <= (a).size(); k++) { \
cout << (a)[k] << " "; \
} \
cout << endl;
LL gcd(LL a, LL b) { return (b) ? gcd(b, a % b) : a; }
LL exgcd(LL a, LL b, LL &x, LL &y) {if (b == 0) { x = 1, y = 0; return a; }LL gcd = exgcd(b, a % b, y, x);y -= a / b * x;return gcd;}
LL qmi(LL a, LL b, LL mod) {LL res = 1;while (b) {if (b & 1) res = res * a % mod;a = a * a % mod;b >>= 1;}return res;}
const int N = 200010;
void solve() {
deque<int> a = {1,3,5,4,2};
int n;
cin>>n;
if(n < 5)
{
cout<< -1 <<endl;
return ;
}
while(n > 5)
{
if(n % 2)
{
a.push_front(n);
}else
{
a.push_back(n);
}
n --;
}
for(int i = 0; i < a.size();i ++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;cin>>n;while(n --)
solve();
return 0;
}
D. Sharky Surfing
这个题目是一道数据结构模拟题,带一点贪心思想
首先我们在面对障碍之前肯定得有足够的能量跳过这段障碍,那么我们不考虑选哪个位置的能量增益,因为我们要让数量最小肯定是获取最大的,假如我们现在在障碍at[i]之前,能量为k小于right - left + 2,也就是不能跳过去,我们得想想前面哪些增益没拿到,从前面已经经过的路上取,简单点说我们让过去的自己获取这个增益来让现在的自己能跳过这个障碍,这就带了一点贪心思维,我们要跳过这个障碍肯定是得尽可能地拿能量增益来跳过障碍,最终我们只需要考虑以下两种情况:
- 现在的能量k能跳过,就跳过
- 现在的能量k不能跳过这个区间,那么我取前面没用取到的最大能量增益,如果还是不够就继续取,如果取完了还是不行就输出-1
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define maxheap priority_queue<int, vector<int>, less<int>>//大根堆,堆顶的第一个元素是这个堆里面的最大值
void solve() {
int n, m, ed;
cin >> n >> m >> ed;
vector<pair<int, int>> at(n);
for (int i = 0; i < n; i++) {
int l, r;
cin >> l >> r;
at[i] = {l, r};
}
deque<pair<int, int>> b(m); //双端队列
for (int i = 0; i < m; i++) {
int l, r;
cin >> l >> r;
b[i] = {l, r};
}
LL k = 1, sum = 0;
priority_queue<int, vector<int>, less<int>> heap;
for (int i = 0; i < n; i++) { //枚举每一个障碍区间
int l = at[i].first, r = at[i].second;
while (!b.empty() && b.front().first < at[i].first) { //如果跳过之前的区间,在现在这个区间到之前的位置
//之前有新的能量增益,放入大根堆备选
heap.push(b.front().second);
b.pop_front();
}
while (k < (r - l + 2) && !heap.empty()) {
k += heap.top();
sum++;
heap.pop();//用了这个能量增益就得把这个从大根堆里面丢掉
}
if (k < (r - l + 2)) {
cout << -1 << endl;
return;
}
}
cout << sum << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
while (n--) {
solve();
}
return 0;
}