前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Codeforces Round #825 (Div. 2) (A~C1)

Codeforces Round #825 (Div. 2) (A~C1)

作者头像
浪漫主义狗
发布2022-10-28 17:01:56
3860
发布2022-10-28 17:01:56
举报
文章被收录于专栏:HAUE_LYS'BlogHAUE_LYS'Blog

A. Make A Equal to B


Origional Link

题目大意

  • 给定只含 0,1 的序列 a,b
  • 对序列 a 不限次数执行如下操作: 将 a_i 变为 a_i - 1 。 将 a 按照任意顺序重新排列。
  • 求最少几步可以得到和 b 相同的序列 a

思想

  • 思维题。
  • 分为两种情况讨论:不排序 a 直接操作和先排序 a 再操作的情况。
  • 同时遍历一遍 ab,记录 a[i] != b[i] 的次数即为不排序 a 直接操作时,需要改变 a_i 的步数,记为 cnt
  • a 进行排序,尽可能的一一对应 b,由于对排序无要求,则只需记录 ab 中相同元素出现的次数,分别设为 t,p
  • 那么在进行一次排序后,最少改变 a 的步数即为 abs(t - 1)
  • 综合上述两种情况最少步数为 min(cnt, abs(t - p) + 1)

代码

代码语言:javascript
复制
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const int N = 1e6 + 10;
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6, PI = acos(-1);

int a[N], b[N];

void solve(){

    int n; cin >> n;

    int p = 0, t = 0;
    for(int i = 0; i < n; i ++){
        cin >> a[i];
        if(a[i] == 1) p ++;
    }

    for(int i = 0; i < n; i ++){
        cin >> b[i];
        if(b[i] == 1) t ++;
    }

    int cnt = 0;
    for(int i = 0; i < n; i ++){
        if(a[i] != b[i]) cnt ++;
    } 

    cout << min(cnt, abs(t - p) + 1) << endl;

}

int main(){

    IOS;

    int _ = 1;

    cin >> _;

    while(_ --){
        solve();
    }

    return 0;

}

B. Playing with GCD


Origional Link

题目大意

  • 给定一个长度为 n 的序列 a
  • 问是否存在一个 n + 1 长度的序列 b,使得 a_i= gcd(bi,b{i+1}),1\le i \le n

思想

  • 数学推理,构造。
  • n\le2 时一定存在 b 成立。
  • n \gt 2 时,设 2\le i \le n-2,若 b 存在,必满足: b_i=lcm(a_{i-1},a_i),b_{i+1} = lcm(a_i,a_{i+1})。 且保证 a_i=gcd(b_i,b_{i+1})。
  • 实际上 a_1b_1 和 a_n 的 b_{n+1} 是一定存在的。

代码

代码语言:javascript
复制
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const int N = 1e6 + 10;
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6, PI = acos(-1);

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

int a[N];

void solve(){

    int n; cin >> n;
    for(int i = 0; i < n; i ++) cin >> a[i];

    bool flag = 1;

    if(n > 2){
        int b = a[0] * a[1] / gcd(a[0], a[1]);

        for(int i = 1; i + 1 < n; i ++){
            int t = a[i] * a[i + 1] / gcd(a[i], a[i + 1]);
            if(gcd(b, t) != a[i]){
                flag = 0;
                break;
            }
            else b = t;
        }
    }

    if(flag) cout << "YES" << endl;
    else cout << "NO" << endl;

}

int main(){

    IOS;

    int _ = 1;

    cin >> _;

    while(_ --){
        solve();
    }

    return 0;

}

C1. Good Subarrays (Easy Version)


Origional Link

题目大意

  • 给定一个长度为 n 的序列 b
  • 对于一个区间 (l,r),1\le l \le r \le n 满足 b_i\ge i,l\le i \le r,则称该区间为一个好区间。
  • 求序列 b 中的好区间数量。

思想

  • 双指针。
  • 定义两个指针 iji 表示以i 为起点的序列的数量,j 表示从i开始的最长序列的位置。
  • j 是第一个不符合条件的位置时,则当前符合条件的区间长度为 j - i
  • 每次 i 向右走一格,显然起点后移,原来符合条件的位置同样符合条件,因此此时 j 不需要左移,继续右移即可。
  • 统计每次 j 不满足条件时区间长度之和即可。

代码

代码语言:javascript
复制
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const int N = 1e6 + 3;
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6, PI = acos(-1);

int a[N];

void solve(){

    int n; cin >> n;
    for(int i = 1; i <= n; i ++) cin >> a[i];

    LL sum = 0;
    for(int i = 1, j = 1; i <= n; i ++){
        while(j <= n && a[j] >= j - i + 1){
            sum += j - i + 1;
            j ++;
        }
    }

    cout << sum << endl;

}

int main(){

    IOS;

    int _ = 1;

    cin >> _;

    while(_ --){
        solve();
    }

    return 0;

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-10-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • A. Make A Equal to B
  • B. Playing with GCD
  • C1. Good Subarrays (Easy Version)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档