Codeforces Global Round 13 A-D题题解

写在前边

链接:Codeforces Global Round 13
A,B,C,D

A. K-th Largest Value

链接:A题链接

题目大意:

有一个字串只由01组成,有两个操作,1是让其中ax变为1ax2是询问数组中第k大的数。

思路

很简单了,字串中元素只在01中变化,要问第k大的数字,那么非10,就用一个map维护01的数量就好了

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
#include <cstring>

//#pragma GCC optimize(2)
//#pragma GCC optimize(3,"Ofast","inline")

using namespace std;

#define Inf 0x3f3f3f3f
#define PII pair<int, int>
#define P2LL pair<long long, long long>
#define endl '\n'

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<long long> VLL;
typedef vector<int> VI;

const int Mod = 10000007;

const int N = 1E5 + 10;
int a[N];

void solve() {
    int n, q;
    scanf("%d%d", &n, &q);
    map<int, int> cnt;
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        cnt[a[i]]++;
    }
    while (q--) {
        int t, x;
        scanf("%d%d", &t, &x);
        if (t == 1) {
            if (a[x] == 1) {
                a[x] = 0;
                cnt[0]++, cnt[1]--;
            } else {
                a[x] = 1;
                cnt[1]++, cnt[0]--;
            }
        } else if (t == 2) {
            if (cnt[1] >= x) puts("1");
            else puts("0");
        }
    }
}

int main()
{
    //ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    solve();
    return 0;
}

B. Minimal Cost

链接:B题链接

题目大意:

每行一个箱子,第i行箱子的坐标为ai,你的任务就是推箱子,然后把利用最小的花费把开一条路使得你可以从左上角走到右下角。

思路

注意

  1. 每行就一个!
  2. 你可以随便方向的走!
    那么这样就很简单了,只需要在一列箱子中开一个口就好了,然后不断更新答案即可。

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
#include <cstring>

//#pragma GCC optimize(2)
//#pragma GCC optimize(3,"Ofast","inline")

using namespace std;

#define Inf 0x3f3f3f3f
#define PII pair<int, int>
#define P2LL pair<long long, long long>
#define endl '\n'

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<long long> VLL;
typedef vector<int> VI;

const int Mod = 10000007;

const int N = 110;
int a[N];

void solve() {
    int n, u, v;
    scanf("%d%d%d", &n, &u, &v);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    
    int res = 2e9 + 10;
    for (int i = 2; i <= n; i++) {
        if (abs(a[i] - a[i - 1]) > 1) res = 0;
        if (a[i] == a[i - 1]) res = min(res, v + min(v, u));
        if (abs(a[i] - a[i - 1]) == 1) res = min(res, min(v, u));
    }

    printf("%d\n", res);
}

int main()
{
    //ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int t;
    scanf("%d", &t); 
    while (t--) {
        solve();
    }

    return 0;
}

C. Pekora and Trampoline

链接:C题链接

题目大意:

跳床,有n个跳床,床的高度为si,每在上边跳一次那么si减小1,每次跨越着跳,当前在i那么一步会跳到i+si去,每次可以从任意一个跳床开始跳,直到跳出所有跳床算一次操作,那么经过多少次操作,所有的si都变为1,注意已经变为1的床不会再减小。

思路

贪心的想,假如我们要是所有的床都变为1,那么我们每次一定得从最左边跳到最右边,所以不如直接贪心加模拟的方法解题。

1.维护一个p数组,pi表示已经在第i张床上跳了pi次。
2.更新答案res+=max(0,s[i]p[i]1),即如果s[i]1>p[i]说明还需要在第i张床上跳s[i]p[i]1次,如果s[i]1p[i],那么说明第i张床上s[i]已经变为1。同时如果在第i张床上跳,因为s[i]每次减一,直到减法为1(s[i]=1就没必要跳了),那么会影响后边第i+s[i],...,i+2床。
3. 还有就是,如果第i张床上跳的过多,即s[i]已经变为1了,那么第i张床上多余的跳跃次数可以转移到下一张床上。

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
#include <cstring>

//#pragma GCC optimize(2)
//#pragma GCC optimize(3,"Ofast","inline")

using namespace std;

#define Inf 0x3f3f3f3f
#define PII pair<int, int>
#define P2LL pair<long long, long long>
#define endl '\n'

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<long long> VLL;
typedef vector<int> VI;

const int Mod = 10000007;

LL gcd(LL a, LL b) {
    return b ? gcd(b, a % b) : a;
}

const int N = 5010;
int s[N];
int p[N];

void solve() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &s[i]);

    LL res = 0;
    memset(p, 0, sizeof p);
    for (int i = 1; i <= n; i++) {
        int x = max(0, s[i] - p[i] - 1); 
        res += x;
        for (int j = i + 2; j <= min(n, i + s[i]); j++) {
            p[j]++;
        }
        p[i + 1] += max(p[i] - s[i] + 1, 0);
    }
    printf("%lld\n", res);
}

int main()
{
    //ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int t;
    scanf("%d", &t); 
    while (t--) {
        solve();
    }
    return 0;
} 

D. Zookeeper and The Infinite Zoo

链接:D题链接

题目大意:

可以在结点uu+v连一条边,当前仅当u&v=v的时候,然后给定q个询问,每次两个端点ui, vi问是否能从uivi

思路

首先发现两个性质:

  1. 假如现结点为v,那么它一定可以转移到2v,很显然。
  2. 还有就是要满足u&v=v,那么起码满足uv,否则直接pass掉即可

然后再推到发现,u1的个数必须要大于等于v1的个数,为什么呢?
我们令k=vu.
我们发现,因为u&k=k,那么说明,uk的二进制数中存在1的位数一定在同一位啊,否则不可能按位与得到k,然后实现u转移到u+k=v,那么v相对于u来说肯定是u其中的某些1进位才得到了u+k=v,那么所以说v1得个数肯定是小于等于u1得个数的,举两个例子:
u(2)=011, k(2)=001,那么v(2)=1001的个数减少了。
u(2)=011, k(2)=011,那么v(2)=1101的个数不变。
那么单纯满足这个条件就对了吗,当然不是。
还要满足,v中的每一位1要高于u中每一位的1,总不能越转移越低吧。那么满足这两个条件,uv就可以进行转移了,那么上边的操作都可以用lowbit运算就可以了

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
#include <cstring>

//#pragma GCC optimize(2)
//#pragma GCC optimize(3,"Ofast","inline")

using namespace std;

#define Inf 0x3f3f3f3f
#define PII pair<int, int>
#define P2LL pair<long long, long long>
#define endl '\n'
#define pub push_back
#define pob pop_back

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<long long> VLL;
typedef vector<int> VI;

const int Mod = 10000007;

int lowbit(int x) {
    return x & (-x);
}

void solve() {
    int u, v;
    scanf("%d%d", &u, &v);
    
    if (u > v) {
        puts("NO");
        return;
    }
    int uu = u, vv = v;
    int cnt1 = 0, cnt2 = 0;
    while (uu) { //u中1的个数
        uu -= lowbit(uu);
        cnt1++;
    }
    while (vv) { //v中1的个数
        vv -= lowbit(vv);
        cnt2++;
    }
    while (u && v) { //必须往高位推才行
        if (lowbit(u) > lowbit(v)) {
            puts("NO");
            return;
        }
        u -= lowbit(u), v -= lowbit(v); 
    }
    if (cnt1 >= cnt2) puts("YES");
    else puts("NO");
}

int main()
{
    //ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int t;
    scanf("%d", &t); 
    while (t--) {
        solve();
    }

    return 0;
}
posted @   Xxaj5  阅读(89)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示