首页 > 其他分享 >2023.1.13

2023.1.13

时间:2023-01-14 01:00:10浏览次数:50  
标签:cnt cin int sum 13 pos -- 2023.1

A.Fill The Bag

https://codeforces.com/contest/1303/problem/D

Statement

给定一个正整数 \(n\) 和 \(m\) 个数 \(a_i\),保证所有 \(a_i\) 都是 \(2\) 的整数次幂。询问是否存在一个长度为 \(m\) 的序列 \(x_i\),使得:

\[\sum _ {i = 1} ^ {m} \frac {a_i}{2 ^ {x_i}} = n \]

且 \(\sum _{i = 1} ^ {m} x_i\) 最小。若存在则输出 \(\sum _{i = 1} ^ {m} x_i\) 。

Solution

由于 \(a_i\) 是 \(2\) 的整数次幂,则 \(\frac {a_i}{2 ^ {x_i}}\) 则可以看作是 \(n\) 的二进制表示。原问题可以转化为:是否可以通过已有的一些个 \(2\) 的整数次幂,“凑”出 \(n\) 的二进制表示。

大的数只能往小变,小的数可以拼凑成大的数。且由于要最小化 div次数,我们可以按位从低到高考虑。假设当前即将考虑第 \(i\) 位,我们可以先试着用 \(i - 1\)位的来凑第 \(i\) 位的。若凑不出来,那就用 \(i\) 后面的有的最小的来凑 \(i\)。

Code

#include <bits/stdc++.h>
typedef long long ll;
const int N = 1e5 + 5;
using namespace std;

int T, m, a[N];
ll n, cnt[100];

int main() {
    ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	freopen("in","r", stdin);
    cin >> T;
    while (T--) {
        cin >> n >> m;
        ll sum = 0;
        for (int i = 1; i <= m; i++) {
            cin >> a[i];
            sum += a[i];
            int ex = log2(a[i]);
            cnt[ex]++;
        }
        if (sum < n) cout << "-1" << endl;
        else {
            ll ans = 0;
            for (int i = 0; i < 60; i++) {
                if ((n >> i) & 1) {
                    int pos = i;
                    while (!cnt[pos]) ++pos;
                    --cnt[pos];
                    while (pos > i) {
                        --pos;
                        cnt[pos]++;
                        ++ans;
                    }
                }
                cnt[i + 1] += cnt[i] / 2;
            }
            cout << ans << endl;
        }
        memset(cnt, 0, sizeof(cnt));
    }
	return 0;
}

B.Obtain The String

https://codeforces.com/contest/1295/problem/C

Statement

给定两个字符串 \(s\) 与 \(t\),令 \(tt\) 为空串。定义一种操作:每次可以选择 \(s\) 的一个字串 \(ss\)(非连续也可以),并把 \(ss\) 加入 \(tt\) 的末尾。询问是否能使 \(tt = t\),若可以输出最小的操作次数。

Solution

定义 \(f_{i,c}\) 表示 \(s\) 中从 第 \(i\) 位开始的后缀串中字母 \(c\) 出现的最早位置。 从后往前扫一遍即可得到这个 \(f\)。结合贪心的思想,每一次新的操作从第一位往后跳即可,跳到末尾则进行下一次操作。

Code

#include <bits/stdc++.h>
const int INF = 1 << 30;
const int N = 2e5 + 5;
using namespace std;

int T, g[N][30];
char s[N], t[N];
set<char> ss;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    freopen("in","r", stdin);
    cin >> T;
    while (T--) {
        cin >> (s + 1) >> (t + 1);
        int lens = strlen(s + 1), lent = strlen(t + 1);
        for (int i = 1; i <= lens; i++) ss.insert(s[i]);
        bool flag = false;
        for (int i = 1; i <= lent; i++) {
            if (!ss.count(t[i])) {
                flag = true;
                break;
            }
        }
        ss.clear();
        if (flag) cout << "-1" << endl;
        else {
            for (int j = 1; j <= 26; j++) g[lens + 1][j] = INF;
            for (int i = lens; i >= 1; i--) {
                for (int j = 1; j <= 26; j++) g[i][j] = g[i + 1][j];
                g[i][s[i] - 'a' + 1] = i;
            }
            int pos = 1, now = 1, cnt = 0;
            bool sign = false;
            while (now <= lent) {
                if (g[pos][t[now] - 'a' + 1] == INF) {
                    cnt++;
                    pos = 1;
                    sign = false;
                }
                else pos = g[pos][t[now] - 'a' + 1] + 1, now++, sign = true;
                if (pos == lens + 1) {
                    cnt++;
                    pos = 1;
                    sign = false;
                }
            }
            if (sign) cnt++;
            cout << cnt << endl;
        }
    }
	return 0;
}

标签:cnt,cin,int,sum,13,pos,--,2023.1
From: https://www.cnblogs.com/BeyondLimits/p/17051056.html

相关文章

  • 2023.1.12
    A.OddDivisorLinkhttps://codeforces.com/contest/1475/problem/AStatement给定一个正整数\(n\(2\leqn\leq10^{14})\),判断其是否至少有一个大于\(1\)的奇......
  • 230113_50_SpringBoot入门
    EnableAutoConfiguration详解​ SpringBoot可以实现自动配置;@EnableAutoConfiguration注解用于告知springboot开启自动配置功能,这样自动配置才能生效。@AutoConfigura......
  • 【230113-1】解方程:a的立方+a的平方=252(据传为89年高考题)
    ......
  • POJ1321 棋盘问题
    POJ1321棋盘问题在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状......
  • Educational Codeforces Round 13
    EducationalCodeforcesRound13AJohnyLikesNumbers做法:假设答案为\(t*k\)考虑$(t-1)*k$,所以答案显然为$n-n$\(\%\)$k+k$代码:voidsolve(){in......
  • 【1.7-1.13】博客精彩回顾
    一、优秀文章推荐1.​​使用Bind提供域名解析服务​​2.​​场景编程集锦-你是谁?​​3.​​那些炫酷的CSS文字效果之诗词《兔》​​4.​​嵌入式:人机交互接口设计详解​​......
  • 13 图像像素值统计
    13图像像素值统计opencv知识点:图像像素最小/最大值-minMaxLoc()图像像素均值/标准差-meanStdDev()本课所解决的问题:如何获取图像像素的最小/最大值?如何......
  • 零基础学习SpringBoot3笔记01_2023-01-13
    零基础学习SpringBoot3笔记01_2023-01-132023-01-131.环境1.1.软件环境安装JDK17并配置环境变量,略安装MySQL5.5并配置环境变量,略安装MySQL客户端工具HeidiSQL,略......
  • 解决docker中mongo报Restarting (132) 5 seconds ago
    报的一直自动重启原因是自建服务器的机器不支持avx指令可以通过cat/proc/cpuinfo|grepavxorsudocat/proc/cpuinfo|grepavx查看你的系统是否支持avx指令,如......
  • 【组会】2023_1_13 看openradar 整理板子v1
    在学校采的数据,计算出来正确的数据大小应该是102400KB,但采下来的数据是51200KB(正确数据的1/2)(所以以后每次采数据之后要先手算一下bin文件大小对不对看openradar代码,......