首页 > 其他分享 >CodeTON Round 4 (Div. 1 + Div. 2, Rated, Prizes!)(CF1810)A~D题题解

CodeTON Round 4 (Div. 1 + Div. 2, Rated, Prizes!)(CF1810)A~D题题解

时间:2023-04-01 14:11:37浏览次数:45  
标签:lnew Rated int 题解 times 点击 rnew res Div

今天采用的是新格式。

CF1810A Beautiful Sequence

点击查看原题

image

点击查看思路

如果一个数字的值 \(v\),不大于当前的位置 \(p\),那我们可以通过删除 \(p - v\) 个数字,使它们两个对应上。

比如 \([1, 7, 2, 5, 3]\) 中的 \(3\),其数值为 \(3\),位置为 \(5\),数值 \(3\) 小于等于 \(5\),那么在 \([1, 7, 2, 5]\) 中删除任意两个数字都可以达到目的,变成 \([X, Y, 3]\)。

点击查看代码
#include <bits/stdc++.h>

using namespace std;

const int N = 110;

int a[N];
int n;

void solve() {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) {
        if (a[i] <= i) {
            cout << "YES\n";
            return;
        }
    }
    cout << "NO\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    while (T--) solve();
    return 0;
}

CF1810B Candies

点击查看原题

image

点击查看思路

应为最开始的数字为奇数 \(1\),那么 \(奇数 \times 2 \pm 1\) 也一定是奇数,那么如果运算途中出现偶数,直接判无解即可。

接下来思考有解情况,我们不妨反过来思考。

  • 如果一个数字 \(y = x \times 2 + 1\),那么 \(x = \frac{y - 1}{2}\)。
  • 如果一个数字 \(y = x \times 2 - 1\),那么 \(x = \frac{y + 1}{2}\)。

那么我们可以倒推回去,因为最终的结果为 \(n\),那么我们只要在 \(\frac{y + 1}{2}\) 和 \(\frac{y - 1}{2}\) 中选择一个,并将它赋值给 \(n\) 即可,这就是一次倒推。

那怎么选呢?因为 \(n\) 为奇数,那么 \(\frac{y + 1}{2}\) 和 \(\frac{y - 1}{2}\) 必为一奇一偶,那么肯定选择哪个奇数的就可以了。

点击查看代码
#include <bits/stdc++.h>

using namespace std;

void solve() {
    int x;
    cin >> x;
    if (x & 1) {
        vector<int> opt;
        opt.clear();
        while (x != 1) {
            if ((x + 1 >> 1) & 1) {
                x = (x + 1) >> 1;
                opt.push_back(1);
            }
            else if ((x - 1 >> 1) & 1) {
                x = (x - 1) >> 1;
                opt.push_back(2);
            }
            else {
                cout << "-1\n";
                return;
            }
        }
        if (opt.size() > 40) {
            cout << "-1\n";
            return;
        }
        cout << opt.size() << '\n';
        for (int i = opt.size() - 1; i >= 0; i--) cout << opt[i] << ' ';
        cout << '\n';
    }
    else cout << "-1\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    while (T--) solve();
    return 0;
}

CF1810C Make It Permutation

点击查看原题

image

点击查看思路

我们可以枚举一个 \(i\),

我们保留数组中 \([1, i]\) 的部分,那我们就要将 \([1, a[i]]\) 中缺少的数字补起来,代价为 \(d \times (a[i] - i)\),即共有 \(a[i] - i\) 个数字要加上来,每次耗费 \(d\) 的金钱。

对于 \([i + 1, n]\) 的部分可以删去,共有 \(n - i\) 个要删除的数字,代价为 \(c \times (n - i)\)。

image

注意:
1. 也可以一个数字也不保留,但是一定要插入一个 \(1\) 呀,因为题目中说:image
2. 一定要去重,并将删除重复数字需要的代价加到结果中。
3. 注意开long long

点击查看代码
#include <bits/stdc++.h>

#define int long long

using namespace std;

const int N = 100010;

int n, c, d;
int a[N];

void solve() {
    cin >> n >> c >> d;
    for (int i = 1; i <= n; i++) cin >> a[i];
    sort(a + 1, a + n + 1);
    int tot = unique(a + 1, a + n + 1) - a - 1;
    int ans = 0x3f3f3f3f3f3f3f3f;
    for (int i = 0; i <= tot; i++) {
        int del = (tot - i + n - tot) * c;		// n - tot为删除重复元素的步骤数量
        int get = (a[i] - i + (i == 0)) * d;
        ans = min(ans, del + get);
    }
    cout << ans << '\n';
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    while (T--) solve();
    return 0;
}

CF1810D Candies

点击查看原题

image

点击查看思路

经典的小学数学奥数题。

设 \(a\) 为每天往上爬的高度,\(b\) 为每天向下降的高度,\(n\) 为给定的需要爬上去的天数。

请注意,第 \(n\) 天爬上去了,就不会下降了。

image

对于操作为 \(1\) 的,我们可以确定其范围。

因为要保证第 \(n\) 天就可以到达,且第 \(n - 1\) 天不能到达,所以其范围为标红部分:

image

用表达式表示为 \([(a - b) \times (n - 2) + a + 1, (a - b) \times (n - 1) + a]\),其中 \((a - b) \times (n - 2) + a\) 为第 \(n - 1\) 天可以到达的最大高度 \(+1\) 才可以符合题意;\((a - b) \times (n - 1) + a\) 为第 \(n\) 天可以到达的最大高度。

需要特判 \(n = 1\) 的情况,此时其范围为 \([1, a]\)。

如果这个区间与之前之前计算的结果有交集,那么就是可以保留的,并更新区间,否则就丢弃之。


对于操作类型为 \(2\) 的,我们先计算出爬上 \(l\) 的高度需要的时间 \(t\),计算方法如下。

假设高度为 \(h\)。

  1. 首先要预留一个 \(a\)。

image

  1. 然后计算 \(\left\lfloor\frac{h - a}{a - b}\right\rfloor\) 表示到达小于等于 \(h - a\) 的位置所需要的时间。

image

  1. 如果刚好到达 \(h - a\) 的位置 \(+1\) 就可以了,否则 \(+2\)。

注意最好不要直接上取整,因为容易引起精度问题。

这样就计算出了 \(t\),然后计算出花 \(t + 1\) 天爬上的高度范围是否与已知范围 \([l, r]\) 有交集,计算方法与前面的操作 \(1\) 类似,如果有那么证明不能准确获取其天数,输出 \(-1\),否则输出天数。

注意我们不能直接判断已知范围 \(l\) 是否等于 \(r\),因为有可能对于这一组询问在该区间内只有一种可能性,也是满足题意的。

点击查看代码
#include <bits/stdc++.h>

#define int long long

using namespace std;

bool check(int& l1, int& r1, int l2, int r2, bool flag) {
    int ll = max(l1, l2);
    int rr = min(r1, r2);
    if (ll > rr) return false;
    if (flag) {
        l1 = ll;
        r1 = rr;
    }
    return true;
}

void solve() {
    int q;
    cin >> q;
    int opt, a, b, c;
    int l = -1, r = 1e18;
    while (q--) {
        cin >> opt;
        if (opt == 1) {
            cin >> a >> b >> c;
            int lnew = -1, rnew = -1;
            if (c == 1) lnew = 1, rnew = a;
            else lnew = (a - b) * (c - 2) + a + 1, rnew = (a - b) * (c - 1) + a;
            if (check(l, r, lnew, rnew, true)) cout << "1 ";
            else cout << "0 ";
        }
        else {
            cin >> a >> b;
            int x = l, res = 0;
            if (a >= l) {
                res = 1;
            }
            else {
                x -= a;
                res = x / (a - b);
                x = res * (a - b);
                if (x == l - a) res++;
                else res += 2;
            }
            
            c = res + 1;
            int lnew = -1, rnew = -1;
            if (c == 1) lnew = 1, rnew = a;
            else lnew = (a - b) * (c - 2) + a + 1, rnew = (a - b) * (c - 1) + a;
            if (check(l, r, lnew, rnew, false)) res = -1;
            cout << res << ' ';
        }
    }
    cout << '\n';
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    while (T--) solve();
    return 0;
}

标签:lnew,Rated,int,题解,times,点击,rnew,res,Div
From: https://www.cnblogs.com/PlayWithCPP/p/17278417.html

相关文章

  • CodeTON Round 4 (Div. 1 + Div. 2, Rated, Prizes!) A-D题解
    题目地址A-BeautifulSequence题意:给出一个数组,问是否存在任意一个子区间,存在i,使得ai=iSolution直接比较当前的数和i的大小就行了,当前为x,如果要求答案存在,必须有i>=xvoidsolve(){ intn;cin>>n; intflag=0; for(inti=1;i<=n;i++) { intx;cin>>x; if(i>=x) {......
  • Thinkpad T14升级Windows11ver22h2失败问题解决小记
    背景手头的ThinkPad在近一年的时间里每次升级Windows11的22h2版本每次都会报错,具体有以下几种情况:更新过程中无问题,重启后黑屏更新过程中会卡在26%左右,然后蓝屏报KENERAL_CHECK_FAIL,接着便自动重启进入修复程序在WindowsUpdate更新中报错0xC1900101在上述错误出现后,再次更......
  • cf-div.2-860d
    题目链接:https://codeforces.com/contest/1798/problem/D贪心,比赛时一直搞C没搞出来,回头看D反而更简单。贪心策略:能填正数就填,填不了填负数。大致证明:构造的区间一定呈一个这样的特定区间,正...负正负负...负正....负负,证明一段区间为正且小于给定值易证,下面证最后一段区间的绝......
  • Codeforces Round 859 (Div. 4) ABCDE(交互题)FG1G2
    EFG1G2质量还挺好的A.PlusorMinushttps://codeforces.com/contest/1807/problem/A题目大意:给定a,b,c,问我们是a+b==c还是a-b==c?把正确的符号输出。input1112332129-7347112110336991899019-81910output+--++-++--+......
  • 使用 wine 安装微信遇到的问题解决方法
    1.中文显示成方块添加启动环境变量:LANG=zh_CN.UTF-82.输入框不显示文字安装winetrickssudoaptinstallwinetricks#oryay-Sywinetricks然后安装riched20winetricksriched20......
  • 无所畏惧的求和题解
    无所畏惧的求和题解本题是本人目前出的题中难度最高的题。可能可以评一个黑?可能有点过,但是紫色是肯定可以的。题目链接:无所畏惧的求和-洛谷尽情享受吧!这道题其实做法有很多:待定系数法+矩阵求解推代数公式组合数学+待定系数法推组合公式第一类斯特林数(时......
  • 定时任务@Scheduled中的cron 表达式和 fixedRated类配置参数
    1.cron表达式格式:@Scheduled(cron="******"){秒数}{分钟}{小时}{日期}{月份}{星期}{年份(可为空)}{秒数} ==>允许值范围:0~59,不允许为空值,若值不合法,调度器将抛出SchedulerException异常"*"代表每隔1秒钟触发;","代表在指定的秒数触发,比如"0,15,45"......
  • Div滚动到头以后置顶
    1<!DOCTYPEHTML>2<html>3<head>4<metacharset="utf-8"/>5<title>Div滚动到头以后置顶</title>6</head>7<bodystyle="height:2000px;">8<divstyle="height:200px&q......
  • cf-div2-860c
    题目链接:https://codeforces.com/contest/1798/problem/C大致题意:给你一个长度为\(n(n<=2e5)\)的序列的\(a_i,b_i\),让你把这个序列分成数目最少的段,每一段都有一个值\(c\),\(c=a_i的一个约数乘以b_i\)。比赛没写出的题。思路:\(首先一段里面的所有a_i*b_i能够整除c,易得c是所有b的......
  • 怎么改变div的位置、大小
    http://www.divcss5.com/rumen/r53973.shtml?ivk_sa=1024320uhttps://www.bbsmax.com/A/VGzl2Pk85b/ 可以使用css中的position来对div进行定位来改变div的位置,position属性值的含义。加上position:absolute,位置可以改变。    static:元素框正常生成。块级元素生成一个......