Codeforces Round #632 (Div. 2)补题

Codeforces Round #632 (Div. 2)补题

C

题目

在这里插入图片描述

题目大意

给一串数列,问能选出多少个好数组
好数组的定义是,任一子数组的和不为0

思路

1.先算出前缀和,只要前缀和相同的就说明该区间和等于0
2.记录一下数组i能往前拓展好数组的最前位置,就是前缀和相同的位置
3.tip:如何记录子区间数目?
对于每一个数,我们设a[i]是一个以这个数为端点的子区间数,那么很容易就得到a[i] = i,所以长度为n的子区间数就是1加到n

代码

#include <iostream>
#include <cstdio>
#include <set>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <unordered_map>
using namespace std;
#define dbg(x) cerr << #x " = " << x << endl;
typedef pair<int, int> P;
typedef long long ll;
const int MAXN = 2e5+5;
ll sum[MAXN];
ll a[MAXN];

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll n;
    cin >> n;
    map<ll, ll> m;
    for(ll i = 1; i <= n; i++)
    {
        cin >> a[i];
        sum[i] = sum[i-1] + a[i];
    }
    ll ans = 0;
    m[0] = 0;
    ll MAX = -0x3f3f3f3f;
    for(ll i = 1; i <= n; i++)
    {
        MAX = max(MAX, (m.find(sum[i]) == m.end()) ? 0 : (ll)m[sum[i]] + 1);
        m[sum[i]] = i;
        ans += i - MAX;
    }
    cout << ans << endl;
}

D

题目

在这里插入图片描述

题目大意

一列人,如果面向对方就要转向,一个人一秒只能转一次,一秒可以有多个人转,问能否在k秒钟恰好转完

思路

记录每一秒最多转多少个,记录下来,就可以知道最快要多少秒转完,也知道最慢多少秒,如果k处于最快和最慢之间,可以把一秒钟转多个人改成多秒钟转一个人,可以把答案凑出k

代码

#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> ans;
int a[3050];
char c;
int main()
{
    int n, k, sum = 0;
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
    {
        cin >> c;
        a[i] = (c == 'R');
    }
    while (1)
    {
        vector<int> now;
        for (int i = 1; i < n; i++)
            if (a[i] && !a[i + 1])
                now.push_back(i), swap(a[i], a[i + 1]), i++;
        if (now.empty())
            break;
        sum += now.size();
        ans.push_back(now);
    }
    if (ans.size() > k || sum < k)
        puts("-1");
    else
    {
        int re = k - ans.size();
        for (auto now : ans)
        {
            while (now.size() > 1 && re > 0)
            {
                printf("1 %d\n", now.back());
                now.pop_back();
                re--;
            }
            printf("%d ", now.size());
            for (auto i : now)
                printf("%d ", i);
            puts("");
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值