Codeforces Round #653 (Div. 3)

Required Remainder

Thinking(binary search)

既然是找最大值问题,我又懒得去推式子,于是我直接就上了一个二分,二分写法比结论稍微繁琐了一点吧,但是还是挺好想的。

根据题意,我们的任务就是找到一个最大的数,满足ans=k∗x+y<=nans = k * x + y <= nans=k∗x+y<=n,于是我们就可以通过二分枚举kkk,来得到我们的答案。通过题目给定的x,y,zx, y, zx,y,z的范围,我们可以确定二分的区间最多不过0190 ~ 1^90 19。

Coding

#include <bits/stdc++.h>
#define mp make_pair
#define pb push_backusing namespace std;typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-7;inline ll read() {ll f = 1, x = 0;char c = getchar();while(c < '0' || c > '9') {if(c == '-')  f = -1;c = getchar();}while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (48 ^ c);c = getchar();}return f * x;
}const int N = 1e6 + 10;char str[N];int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int _ = read();while(_--) {ll x = read(), y = read(), n = read();ll l = 0, r = 1e9 + 10;while(l < r) {ll mid = l + r + 1 >> 1;if(mid * x + y <= n)  l = mid;else r = mid - 1;}printf("%lld\n", l * x + y);}return 0;
}

Multiply by 2, divide by 6

Thinking

判断能否通过0个或者多个乘二的操作,使数字变成6的倍数。我们想想6的两个质因子2,32, 32,3,要想达到这个目的,对于初始的nnn,只可能有这两种质因子,否则我们一定达不到我们的目标。于是我们可以先对nnn,进行2,32, 32,3的质因数提取,假设得到的222的因子个数是num2num2num2, 333的因子个数是num3num3num3,因为我们是同时消去2,32, 32,3因子的,并且只能增加或者不增加222的因子个数,所以只有当num2<=num3num2 <= num3num2<=num3时,才能保证我们可以消去所有的2,32, 32,3因子,最后变成111。

Coding

#include <bits/stdc++.h>
#define mp make_pair
#define pb push_backusing namespace std;typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-7;inline ll read() {ll f = 1, x = 0;char c = getchar();while(c < '0' || c > '9') {if(c == '-')  f = -1;c = getchar();}while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (48 ^ c);c = getchar();}return f * x;
}const int N = 1e6 + 10;char str[N];int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int _ = read();while(_--) {ll n = read();int num2 = 0, num3 = 0;while(n % 3 == 0) {n /= 3;num3++;}while(n % 2 == 0) {n /= 2;num2++;}//最后n不是1说明还存在其他的质因子。if(n != 1 || num2 > num3) puts("-1");else  printf("%d\n", num3 + num3 - num2);}return 0;
}

Move Brackets

Thinking(Stack)

我们先找到所有的符合匹配的括号,最后就只剩下一种非法的括号了,以这种形式存在)() ()(形成)))))((((()))))((((()))))(((((这样的排列,所以我们只需要将其后面的移到前面去,或者前面的移到后面去,任选一种进行操作,因此我们的花费将会是最后无法匹配的括号的对数,也就是栈中的元素的一半。

Coding

#include <bits/stdc++.h>
#define mp make_pair
#define pb push_backusing namespace std;typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-7;inline ll read() {ll f = 1, x = 0;char c = getchar();while(c < '0' || c > '9') {if(c == '-')  f = -1;c = getchar();}while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (48 ^ c);c = getchar();}return f * x;
}const int N = 1e6 + 10;char str[N];int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int _; cin >> _;while(_--) {int n; cin >> n;stack<char> stk;for(int i = 1; i <= n; i++) {char temp;  cin >> temp;if(stk.empty() || stk.top() == temp || temp == '(')  stk.push(temp);else  stk.pop();}printf("%d\n", stk.size() / 2);}return 0;
}

Zero Remainder Array

Thinking

对于给定的序列,我们需要的就是x(modk)=(1,2……k−2,k−1)x \pmod k = (1, 2 …… k - 2, k - 1)x(modk)=(1,2……k−2,k−1),这样的数,才能使我们的序列变成都是kkk的倍数,假定k=4k = 4k=4,数组中存在两个数分别为3,73, 73,7, 他们有一个共同点3(modk)=7(modk)3 \pmod k \ = 7 \pmod k3(modk) =7(modk),也就是说我们在x(modk)x \pmod kx(modk)从0−>k−10 -> k - 10−>k−1,的一趟循环中,最多只能使其中的一个数变成ai(modk)=0a_i \pmod k = 0ai​(modk)=0,想必看到这里应该就搞懂了这道题了,我们就是要找到ai(modk)a_i \pmod kai​(modk)后出现的次数最多的非零数,当有多个出现次数相同的数时我们取ai(modk)a_i \pmod kai​(modk)的最小值,因为那个最小值一定是当xxx足够大的时候才会满足条件。

Coding

#include <bits/stdc++.h>
#define mp make_pair
#define pb push_backusing namespace std;typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-7;inline ll read() {ll f = 1, x = 0;char c = getchar();while(c < '0' || c > '9') {if(c == '-')  f = -1;c = getchar();}while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (48 ^ c);c = getchar();}return f * x;
}const int N = 2e5 + 10;int a[N];int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int _ = read();while(_--) {int n = read(), k = read();for(int i = 1; i <= n; i++) {a[i] = read();a[i] %= k;}sort(a + 1, a + 1 + n, greater<int> ());int now_num = 1, ansn = a[1], num = 1;for(int i = 2; i <= n && a[i] != 0; i++) {if(a[i] == a[i - 1])  now_num++;else  now_num = 1;if(now_num >= num)  ansn = a[i], num = now_num;}if(ansn == 0){puts("0");continue;}// cout << num << " " << ansn << endl;printf("%lld\n", 1ll * k * (num - 1) + k - ansn + 1);}return 0;
}

Reading Books (easy version)

Thinking(Sort, greedy)

题意这里就不说明了,对于给定的条件,我们要同时满足AliceandBobAlice and BobAliceandBob都要读至少kkk本书,所以我们可以指定一个策略,不管这本书是AliceorBobAlice or BobAliceorBob喜欢,还是他们两同时喜欢,我们都同时增加AliceandBobAlice and BobAliceandBob的当前的书的数量,因此在之前我们就需要对书本分类AliceAliceAlice喜欢的数组a,BobBobBob喜欢的数组b,两个人都喜欢的数组c。接下来就时对这三个数组分别按照元素大小从小到大进行排序

当我们当前枚举的ai+bj<=cka_i + b_j <= c_kai​+bj​<=ck​时我们显然贪心的选择ai,bja_i, b_jai​,bj​这两本书,所以我们的总花费将会变成ans+=ai+bjans += a_i + b_jans+=ai​+bj​,否则的话我们将会选择ckc_kck​,花费将变成ans+=ckans += c_kans+=ck​。

当我们第一个点枚举完了后,大致存在三种情况aaa不可选,bbb不可选,ccc不可选。

所以接下来的枚举我们必须分类讨论了当a∣∣ba || ba∣∣b,不可选的时候,我们要达到条件只能通过选择ccc来进行。否则的话我们就只能选择a,ba, ba,b两个组合选取了。

Code

#include <bits/stdc++.h>
#define mp make_pair
// #define pb push_backusing namespace std;typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-7;inline ll read() {ll f = 1, x = 0;char c = getchar();while(c < '0' || c > '9') {if(c == '-')  f = -1;c = getchar();}while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (48 ^ c);c = getchar();}return f * x;
}const int N = 2e5 + 10;struct Node {int value, fa, fb;void input() {value = read(), fa = read(), fb = read();}void out() {printf("%d %d %d\n", value, fa, fb);}bool operator < (const Node & t) const {return value < t.value;}
}a[N], b[N], c[N], in;
//结构体就是数组的用处,不用管。是我自己一开始思路想的有点复杂,然后就写了这么一个结构体。int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int n = read(), k = read(), na = 0, nb = 0, nc = 0;for(int i = 1; i <= n; i++) {in.input();if(in.fa && in.fb)  c[++nc] = in;else if(in.fa)  a[++na] = in;else if(in.fb)  b[++nb] = in;//一定要注意特判 0 0的情况。}// cout << na << " " << nb << " " << nc << endl;sort(a + 1, a + 1 + na);sort(b + 1, b + 1 + nb);sort(c + 1, c + 1 + nc);int pa = 1, pb = 1, pc = 1, flag = 0;int numa = 0, numb = 0;ll ans = 0;while(pa <= na && pb <= nb && pc <= nc) {if(a[pa].value + b[pb].value <= c[pc].value) {ans += a[pa].value + b[pb].value;pa++, pb++;}else {ans += c[pc].value;pc++;}numa++, numb++;if(numa >= k && numb >= k) {flag = 1;break;}}// cout << ans << " " << numa << " " << numb << endl;if(flag) {printf("%lld\n", ans);return 0;}if(pa > na || pb > nb) {while(pc <= nc) {ans += c[pc].value;pc++;numa++, numb++;if(numa >= k && numb >= k) {flag = 1;break;}}}else {while(pa <= na && pb <= nb) {ans += a[pa].value + b[pb].value;pa++, pb++;numa++, numb++;if(numa >= k && numb >= k) {flag = 1;break;}}}if(flag) {printf("%lld\n", ans);return 0;}puts("-1");return 0;
}

Codeforces Round #653 (Div. 3)(A, B, C, D, E1详解)相关推荐

  1. B. Multiply by 2, divide by 6(数学) Codeforces Round #653 (Div. 3)

    原题链接:https://codeforces.com/problemset/problem/1374/B 题意:有一个数能进行两种操作,除以6乘以2,问经过多少次操作能变成1.若不行则输出-1. 解 ...

  2. Codeforces Round #653 (Div. 3)部分题解

    文章目录 A - Required Remainder B - Multiply by 2, divide by 6 C - Move Brackets D - Zero Remainder Arra ...

  3. Codeforces Round #653 (Div. 3)

    A.Required Remainder 二分 #include<iostream> #include<algorithm> using namespace std; int ...

  4. Codeforces Round #506 (Div. 3)

    Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...

  5. Codeforces Round #563 (Div. 2)/CF1174

    Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...

  6. 构造 Codeforces Round #302 (Div. 2) B Sea and Islands

    题目传送门 1 /* 2 题意:在n^n的海洋里是否有k块陆地 3 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 4 输出完k个L后,之后全部输出S:) 5 5 10 的例子可以 ...

  7. Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解 比赛链接:h ...

  8. Codeforces Round #712 Div.2(A ~ F) 超高质量题解(每日训练 Day.15 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #712 Div.2(A ~ F) 题解 比赛链接:https:// ...

  9. Codeforces Round #701 (Div. 2) A ~ F ,6题全,超高质量良心题解【每日亿题】2021/2/13

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 A - Add and Divide B - Replace and Keep Sorted C ...

最新文章

  1. eclipse导入项目后出现红色叉号的解决方案
  2. mysq改变字段类型
  3. 前端开发工程师做些什么?
  4. python3爬取网易云歌单数据清洗_网页抓取网易云音乐及评论数据分析
  5. Linux系统上的库文件的生成与使用
  6. 亦云小组KTV点歌系统简介
  7. html基础元素案例笔记(1)
  8. qt中生成含有中文的json文件,读取含有中文的json文件
  9. 在ASP.NET中防止注入攻击
  10. Wet Shark and Two Subsequences
  11. 马哥linux脚本,马哥linux shell笔记
  12. selenium+crop+chaojiying 之模拟登录超级鹰
  13. Chrome浏览器模拟微信客户端访问网址,方法图文讲解模拟微信
  14. [UE4]Viewport中摄像机镜头缩放速度修改
  15. 计算机基础知识在哪里学,学习电脑基础知识先从哪方面入手?
  16. oracle ndb,NDB语法 - ivaneeo's blog - BlogJava
  17. NDN网络学习笔记(一)——NDN基础
  18. Do you kown Asp.Net Core -- 配置Kestrel端口
  19. 台式计算机dvd光驱在哪里,用台式电脑怎么放DVD
  20. 开关二极管和肖特基二极管比较

热门文章

  1. 软件项目组织管理(六)项目时间管理
  2. html5制作线路图,HTML5绘制上海地铁线路图
  3. 它是世界上最轻的固体!1000℃下不会熔化,上过火星,还能进你家......
  4. 史上最让数学家无奈的规定!背后真相让人不敢相信,可是没有人能证明对错.........
  5. 从串行线程封闭到对象池、线程池
  6. 12星座程序员写代码
  7. java搜索string_java – 在数组列表中搜索最常见的String
  8. gamaredon_Gamaredon组织某样本分析
  9. oracle cpu 100%原因,oracle 12.1 cpu 100%
  10. 怎么让图片手机上排列_荣耀手机系列档次怎么排列?