首页 > 其他分享 >HZNU Winter Trainning 8 补题

HZNU Winter Trainning 8 补题

时间:2023-01-01 11:45:12浏览次数:60  
标签:std const Winter int HZNU cin 补题 字符串 define

CodeForces - 1353D
Constructing the Array
题目传送门:https://vjudge.net/contest/536385#problem/D
题意:给你一个全是0的数组,用1-n的数将这个数组填满,规则是从左至右筛选出0最多的子序列,然后如果子序列长度是奇数,就填在中间,如果长度是偶数,就填在中间偏左,让你输出填完的数组序列
题解:优先队列,我们需要时刻知道哪一段序列0最多,说白了插进去一个数,一段全是0的序列就会被分成2段,所以我们可以利用优先队列动态维护0序列的长度,优先处理0序列长的序列。

点击查看代码
#include <bits/stdc++.h>
#define Zeoy std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define endl '\n'
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int N = 2e5 + 10;
typedef struct node
{
    int l, r;
    bool operator<(const node &t) const
    {
        if (this->r - this->l != t.r - t.l)
            return this->r - this->l < t.r - t.l;
        else
            return this->l > t.l;
    }
} node;
priority_queue<node> q;
int a[N] = {0};
int main(void)
{
    Zeoy;
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        q.push({1, n});
        int num = 1;
        while (q.size())
        {
            node t = q.top();
            int l = t.l;
            int r = t.r;
            q.pop();
            a[(l + r) / 2] = num++;
            if (l != (l + r) / 2)
                q.push({l, (l + r) / 2 - 1});
            if (r != (l + r) / 2)
                q.push({(l + r) / 2 + 1, r});
        }
        for (int i = 1; i <= n; ++i)
            cout << a[i] << " ";
        cout << endl;
    }
    return 0;
}

CodeForces - 1353E
periodic Garland
题目传送门:https://vjudge.net/contest/536385#problem/E
题意:给你一个字符串,0代表灯是灭的,1代表灯是亮的,现在给定k,使得每隔k个位置必须要有连续的亮的灯,或者没有灯亮,例如k=3,以下字符串是合法的"00010010", "1001001", "00010","0",现在你可以用1花费使得0和1反转,问你最少需要多少花费能够把这个字符串变为合法的字符串。
题解:贪心,首先我们要知道i%k相等的位置一定距离相隔为k,所以我们只要去遍历[0,k-1]即i的余数,然后对i,i+k,i+2k,i+3k...这些位置进行操作,那我们怎么操作呢?
我们模拟一下:假设k=3,如果现在字符串是这个样,010010000,实际上我们根本不用去改变,现在本身就是最优解;如果0100000100,那么我们遍历到第一个1的时候发现使字符串合法的最小代价是1,就是让位置8的1变为0,当我们遍历到位置4的0的时候,使字符串合法的最小代价还是1,就是让位置8的1变为0,当我们遍历到位置8的1时,使字符串合法的最小代价还是1,要么让位置4的0变为1,要么让位置2的1变为0,我们仔细想想这是不是一个求最大字段和的问题,遇到1,sum++,遇到0,sum--,中间时刻维护sum的最大值,如果sum<0说明前面的和对后面已经产生不了贡献了,直接舍弃,这样的话sum其实就代表不需要被改变的最大值,答案就是1的数量减去sum的最大值。

点击查看代码
#include <bits/stdc++.h>
#define Zeoy std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define endl '\n'
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int N = 2e5 + 10;
int main(void)
{
    Zeoy;
    int t;
    cin >> t;
    while (t--)
    {
        int n, k;
        cin >> n >> k;
        string s;
        cin >> s;
        s = "*" + s;
        int cnt = 0;
        for (int i = 1; i <= n; ++i)
            cnt += (s[i] == '1');
        int ans = cnt;
        for (int i = 0; i <= k - 1; ++i)
        {
            int sum = 0;
            for (int j = i + 1; j <= n; j += k)
            {
                if (s[j] == '1')
                    sum++;
                else
                    sum--;
                if (sum < 0)
                    sum = 0;
                ans = min(ans, cnt - sum);
            }
        }
        cout << ans << endl;
    }
    return 0;
}

标签:std,const,Winter,int,HZNU,cin,补题,字符串,define
From: https://www.cnblogs.com/Zeoy-kkk/p/17017890.html

相关文章

  • UNIQUE VISION Programming Contest 2022 Winter(AtCoder Beginner Contest 283)
    A-PowerGivenintegersAandB,printthevalueA^B.基础不解答B-FirstQueryProblem基础不解答C-CashRegisterTakahashiisacashier.Thereis......
  • HZNUOJ-1503公路乘车--DP
    题目传送门:https://acm.hznu.edu.cn/OJ/problem.php?id=1503题解:我们发现后一状态由前一状态决定,即后一公里由前面十公里的状态决定,经典dp,我们直接列出状态转移方程:dp[1]......
  • LF Professional及WINTERACTER产品简介
    LF专业版v7.9LFProfessionalv7.8将32/64位Rainier编译器与经典的Lahey/FujitsuLF95编译器相结合!Rainier完全符合Fortran95/90/77标准,并广泛支持Fortran2003......
  • 【JZWinter Camp 2017】欠题小结
    2017.1.12Day1【GDKOI2017模拟】T2[JZOJ4938]序列T3[JZOJ4939]平均数2017.1.13Day2【NOIP2017提高组模拟】T2[JZOJ3824][CFRCC2014warmupDiv.1D]渴2017.1......
  • Codeforces 随机补题记录
    日期序号题号点评12.2029CF1694E很有借鉴意义的化用算法题12.2138CF1713F子集启蒙题12.2954CF1761E有很多细节,要想清楚,下次不要面向数据编程了......
  • HZNU Winter Trainning 7 补题 - Zeoy
    CodeForces-1660C题目传送门:https://vjudge.net/contest/535955#problem/C题意:询问一个字符串最少删去几个字符,能够把这个字符串变成aabbccdd这种两两相同的字符串题......
  • 2022 winter training.1
    A-SearchingLocalMinimumhttps://codeforces.com/problemset/problem/1480/C题意交互题有一个1~n的序列最多询问100次问i位置上的数是什么最后要找出一个局部最......
  • ICPC2022杭州站(补题)
    A-ModuloRuinstheLegend#include<bits/stdc++.h>#definelllonglongusingnamespacestd;lln,m,sum,n1,n2;llGcd(lla,llb){if(!b)returna;ret......
  • HHKB Programming Contest 2022 Winter(AtCoder Beginner Contest 282)
    前言好久没有打AtCoder了。有点手生。只拿到了\(\operatorname{rk}1510\),应该上不了多少分。只切了\(\texttt{A,B,C,D}\)四题。A-GeneralizedABC简要题意给出......
  • 【补题】2022年中国大学生程序设计竞赛女生专场
    比赛链接2022年中国大学生程序设计竞赛女生专场A.减肥计划签到题暴力模拟,如果模拟到最重的人到队首还没有人连续获胜\(k\)轮,那么答案显然就是最重的人。方便起见可以......