被教育咯
A - Tricky Template
题意:
思路:读题读了半天..可以发现,若对于第位而言,,那么c就一定与模板匹配。否则模板只需要取大写的即可。因此若所有的 ,都有,那么就不能构造,否则一定可以构造出模板。
// Problem: A. Tricky Template
// Contest: Codeforces - Educational Codeforces Round 161 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1922/problem/0
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second
#define endl '\n'
const LL maxn = 4e05+7;
const LL N = 5e05+10;
const LL mod = 1e09+7;
const int inf = 0x3f3f3f3f;
const LL llinf = 5e18;
typedef pair<int,int>pl;
priority_queue<LL , vector<LL>, greater<LL> >mi;//小根堆
priority_queue<LL> ma;//大根堆
LL gcd(LL a, LL b){
return b > 0 ? gcd(b , a % b) : a;
}
LL lcm(LL a , LL b){
return a / gcd(a , b) * b;
}
int n , m;
vector<int>a(N , 0);
void init(int n){
for(int i = 0 ; i <= n ; i ++){
a[i] = 0;
}
}
void solve()
{
cin >>n;
string s[3];
for(int i = 0 ; i < 3; i ++)
cin >>s[i];
int cnt1 = 0 , cnt0 = 0;
for(int i = 0 ; i < n ; i ++){
if(s[0][i] != s[2][i] && s[1][i] != s[2][i]){//不匹配
cout <<"YES\n";
return;
}
}
cout <<"NO\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout.precision(10);
int t=1;
cin>>t;
while(t--)
{
solve();
}
return 0;
}
B - Forming Triangles
题意:
思路:显然,需要确定拿的策略:一定要拿两根一样长的木棍和一根比他们短的木棍。因此对a数组进行排序,然后模拟一下就行了。
// Problem: B. Forming Triangles
// Contest: Codeforces - Educational Codeforces Round 161 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1922/problem/B
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second
#define endl '\n'
#define int long long
const LL maxn = 4e05+7;
const LL N = 5e05+10;
const LL mod = 1e09+7;
const int inf = 0x3f3f3f3f;
const LL llinf = 5e18;
typedef pair<int,int>pl;
priority_queue<LL , vector<LL>, greater<LL> >mi;//小根堆
priority_queue<LL> ma;//大根堆
LL gcd(LL a, LL b){
return b > 0 ? gcd(b , a % b) : a;
}
LL lcm(LL a , LL b){
return a / gcd(a , b) * b;
}
int n , m;
vector<int>a(N , 0);
void init(int n){
for(int i = 0 ; i <= n ; i ++){
a[i] = 0;
}
}
int pre[N];
int sum[N];
void solve()
{
cin >> n;
int a[n + 5];
for(int i = 1 ; i <= n ; i ++){
cin >> a[i];
}
sort(a + 1, a + 1 + n);
int cnt = 0;
int ans = 0;
for(int i = 1 ; i <= n ; i ++){
if(i == 1 || a[i] == a[i - 1]){
cnt ++;
}
else{
cnt = 1;
}
ans += sum[i - 1] - sum[i - cnt];
}
cout << ans << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout.precision(10);
for(int i = 0 ; i < N ; i ++){
pre[i] = i - 1;
}
for(int i = 1 ; i < N ; i ++){
sum[i] = sum[i - 1] + pre[i];
}
int t=1;
cin>>t;
while(t--)
{
solve();
}
return 0;
}
C - Closest Cities
题意:
思路:可以发现:若要从走到,不可能先往远离 的地方走,然后再跳到。因此从走到的花费其实就是直接朝着走的花费,分别预处理一下向右走和向左走的花费即可。
// Problem: C. Closest Cities
// Contest: Codeforces - Educational Codeforces Round 161 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1922/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second
#define endl '\n'
const LL maxn = 4e05+7;
const LL N = 5e05+10;
const LL mod = 1e09+7;
const int inf = 0x3f3f3f3f;
const LL llinf = 5e18;
typedef pair<int,int>pl;
priority_queue<LL , vector<LL>, greater<LL> >mi;//小根堆
priority_queue<LL> ma;//大根堆
LL gcd(LL a, LL b){
return b > 0 ? gcd(b , a % b) : a;
}
LL lcm(LL a , LL b){
return a / gcd(a , b) * b;
}
int n , m;
vector<int>a(N , 0);
void init(int n){
for(int i = 0 ; i <= n ; i ++){
a[i] = 0;
}
}
void solve()
{
cin >> n;
LL a[n + 5];
a[0] = -llinf;
a[n + 1] = llinf;
for (int i = 1 ; i <= n ; i ++){
cin >> a[i];
}
LL suml[n + 5];
suml[0] = suml[1] = 0;
for(int i = 1 ; i <= n ; i ++){
if(a[i] - a[i - 1] > a[i + 1] - a[i]){
suml[i + 1] = suml[i] + 1;
}
else{
suml[i + 1] = suml[i] + a[i + 1] - a[i];
}
}
LL sumr[n + 5];
sumr[n] = 0;
for(int i = n ; i > 1; i --){
if(a[i] - a[i - 1] < a[i + 1] - a[i]){
sumr[i - 1] = sumr[i] + 1;
}
else{
sumr[i - 1] = sumr[i] + a[i] - a[i - 1];
}
}
int m;
cin >> m;
for(int i = 0 ; i < m ; i ++){
int x , y;
cin >> x >>y;
if(x >y){
cout <<sumr[y] - sumr[x] << endl;
}
else{
cout << suml[y] - suml[x]<<endl;
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout.precision(10);
int t=1;
cin>>t;
while(t--)
{
solve();
}
return 0;
}
D - Berserk Monsters
题意:
思路:可以发现:在一轮游戏过后,只有死去的怪物的两边的怪物可能会在下一轮死去。因此每一轮游戏,只需要处理可能会死去的那些怪物即可。(第一轮所有都有可能死去)由于涉及到某只怪兽的左右两只怪兽和删除某只怪兽的操作,因此可以用链表来模拟所有怪兽的情况,这样每次删除的花费都是O(1)的。 每只怪兽最多只会形成两只可能死亡的怪兽,因此整体模拟的复杂度应该是O(N)的。
// Problem: D. Berserk Monsters
// Contest: Codeforces - Educational Codeforces Round 161 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1922/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second
#define endl '\n'
const LL maxn = 4e05+7;
const LL N = 5e05+10;
const LL mod = 1e09+7;
const int inf = 0x3f3f3f3f;
const LL llinf = 5e18;
typedef pair<int,int>pl;
priority_queue<LL , vector<LL>, greater<LL> >mi;//小根堆
priority_queue<LL> ma;//大根堆
LL gcd(LL a, LL b){
return b > 0 ? gcd(b , a % b) : a;
}
LL lcm(LL a , LL b){
return a / gcd(a , b) * b;
}
int n , m;
vector<int>a(N , 0);
void init(int n){
for(int i = 0 ; i <= n ; i ++){
a[i] = 0;
}
}
void solve()
{
cin >> n;
LL a[n + 5] , b[n + 5] , l[n + 5] , r[n + 5];//链表存储
for(int i = 1 ; i <= n ; i ++){
cin >> a[i];
}
for(int i = 1 ; i <= n ; i ++){
cin >> b[i];
}
for(int i = 1 ; i <= n ; i ++){
l[i] = i - 1;
r[i] = i + 1;
}
l[1] = -1 , r[n] = -1;
vector<int>v;//当前有效的怪兽
for(int i = 1 ; i <= n ; i ++)
v.pb(i);
vector<int>vis(n + 5 , -1);
for(int i = 0 ; i < n ; i ++){
vector<int>die;
for(auto it : v){
int sum = 0;
if(l[it] != -1){
sum += a[l[it]];
}
if(r[it] != -1){
sum += a[r[it]];
}
if(sum > b[it]){
die.pb(it);
}
}
cout << die.size()<<" ";
v.clear();
for(auto it : die){
vis[it] = i;//第几轮死亡的
}
for(auto it : die){
if(l[it] != -1){
r[l[it]] = r[it];
if(vis[l[it]] < i){//未死亡且不在v中
v.pb(l[it]);
vis[l[it]] = i;//标记在v中
}
}
if(r[it] != -1){
l[r[it]] = l[it];
if(vis[r[it]] < i){
v.pb(r[it]);
vis[r[it]] = i;
}
}
}
}
cout << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout.precision(10);
int t=1;
cin>>t;
while(t--)
{
solve();
}
return 0;
}
E - Increasing Subsequences
题意:
思路:可以发现,在考虑空集的情况下,若在当前已有数组的最后加一个最大值,那么整体的方案数就是原来的两倍,而在已有数组的最后加一个最小值,那么方案数就比原来多1。因此本题变成了从原本的1,通过*2 或者 +1 操作来变成X。同样按照二进制模拟就行。
// Problem: E. Increasing Subsequences
// Contest: Codeforces - Educational Codeforces Round 161 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1922/problem/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second
#define endl '\n'
const LL maxn = 4e05+7;
const LL N = 5e05+10;
const LL mod = 1e09+7;
const int inf = 0x3f3f3f3f;
const LL llinf = 5e18;
typedef pair<int,int>pl;
priority_queue<LL , vector<LL>, greater<LL> >mi;//小根堆
priority_queue<LL> ma;//大根堆
LL gcd(LL a, LL b){
return b > 0 ? gcd(b , a % b) : a;
}
LL lcm(LL a , LL b){
return a / gcd(a , b) * b;
}
int n , m;
vector<int>a(N , 0);
void init(int n){
for(int i = 0 ; i <= n ; i ++){
a[i] = 0;
}
}
LL k;
void solve()
{
cin >> k;
vector<int>ans;
int l = 0 , r = 0;
//末尾添加最大数 -> 方案数 * 2
//末尾添加最小数 -> 方案数 + 1
vector<int>t;
while(k){
if(k & 1){
t.pb(1);
}
else{
t.pb(0);
}
k /= 2;
}
for(int i = t.size() - 2 ; i >= 0 ; i --){
if(t[i] == 1){
ans.pb(++r);
ans.pb(--l);
}
else{
ans.pb(++r);
}
}
cout << ans.size() << endl;
for(auto it :ans){
cout << it <<" ";
}
cout << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout.precision(10);
int t=1;
cin>>t;
while(t--)
{
solve();
}
return 0;
}