Codeforces Round #643 (Div. 2) 题解 (ABCDE)
https://codeforces.com/contest/1355
打一半和室友开黑去了哈哈哈哈哈哈反正不计分(不要学我)
A. Sequence with Digits
这题我会证!首先对于百位来说,不可能从x跳到x+2,只能从x变成x+1或者不变(因为最大变化量为 \(9\times9=81\))
这样的话大约1000次内,百位不可避免地从9变成0,这样min的值是0,变化量min*max就是一直是0了
#include <bits/stdc++.h>
using namespace std;
#define repeat(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define repeat_back(i,a,b) for(int i=(b)-1,_=(a);i>=_;i--)
int cansel_sync=(ios::sync_with_stdio(0),cin.tie(0),0);
const int N=200010; typedef long long ll;
#define int ll
int a,k;
string s;
signed main(){
int T; cin>>T;
while(T--){
cin>>a>>k;
while(--k){
s=to_string(a);
int x=*min_element(s.begin(),s.end())-48;
int y=*max_element(s.begin(),s.end())-48;
if(x==0)break;
a+=x*y;
}
cout<<a<<endl;
}
return 0;
}
B. Young Explorers
比赛中我自始至终都以为所有探险者都要组队,然后emmm...
贪心,尽量组规模小的队,所以排个序就好了
感谢zkx巨佬提供的代码(没错,我懒了补题)
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+7;
int n;
int a[N];
inline void solve() {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
sort(a+1, a+n+1);
int res = 0;
for (int i = 1, cnt = 0; i <= n; ++i) {
++cnt;
if (cnt >= a[i]) {
++res;
cnt = 0;
}
}
cout << res << endl;
}
signed main() {
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int T = 1;
cin >> T;
for (int t = 1; t <= T; ++t) {
solve();
}
return 0;
}
C. Count Triangles
先讲一下我的思路
\(A≤x≤B≤y≤C≤z≤D\)
因为 \(x\) 范围不大,考虑枚举 \(x\) 的值。这样对于任意满足要求的 \(y\),都有 \(C\le z\le \min(D,x+y-1)\),或者说,\(z\) 的方案数为 \(\max(0,\min(D,x+y-1)-C+1)\)。令这个东西为 \(f(y)=\max(0,\min(D,x+y-1)-C+1)\),函数图像大概长这样

先一次函数上升,然后变成常函数。此时就能成两段讨论(一次函数和常函数),求出两个转折点什么的,太过于数学,逃了
然后放个没人能看懂的代码(经刘老师提醒,2020-5-17 11:45之前放的是错误代码,现已更正)
#include <bits/stdc++.h>
using namespace std;
#define repeat(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define repeat_back(i,a,b) for(int i=(b)-1,_=(a);i>=_;i--)
int cansel_sync=(ios::sync_with_stdio(0),cin.tie(0),0);
const int N=200010; typedef long long ll;
#define int ll
int a,b,c,d;
signed main(){
cin>>a>>b>>c>>d;
int ans=0;
repeat(i,a,b+1){
int x=max(c,i+b-1); //第一个转折点
int y=min(i+c-1,d); //第二个转折点
int l=x-c+1,r=y-c+1;
if(x<=y)ans+=(l+r)*(r-l+1)/2; //一次函数(等差数列求和)
if(i+c-1>max(x-1,y))ans+=(i+c-1-max(x-1,y))*(d-c+1); //常函数
}
cout<<ans<<endl;
return 0;
}
等会补个别人家的代码(雾,我作业来不及了,不补了)
D. Game With Array
这题就一sb题。如果 \(2N> S\),这意味着序列中至少存在一个1,然后。。就NO了(还没证明)
如果 \(2N\le S\),那就构造一个序列:\([2,2,2,...,2,(S-2(N-1))]\),所有数都大于等于 \(2\),所以这个序列的任意子序列之和都不可能是 \(1\),也不可能是 \(S-1\)(因为存在子序列和为 \(S-1\) 就必然存在子序列和为 \(1\))。而子串又是一种特殊的子序列,所以完全ojbk
更:看了一下官方题解,来证明一波 \(2N>S\) 的情况
先假定 \(K\le \dfrac S 2\)。对序列求个前缀和 \(sum[i]=\sum\limits_{j=1}^{i}a[j]\),\(sum[0]=0\)。我们知道区间和相当于两个前缀和的差值,而这个差值不能等于 \(K\) 或者 \(S-K\),这意味着对于每个 \(sum[i]\) 都不能出现另一个 \(sum[j]=(sum[i]+K)\%S\)(非常巧妙,如果 \(sum[i]+K\ge S\),取模后正好等于 \(sum[i]-(S-K)\),差值为 \(S-K\),太牛逼了)
也就是,对每个 \(sum[i]\),我们认为它占用了两个位置 \(sum[i]\) 和 \((sum[i]+K)\%S\)。总共 \(S\) 个座位,要坐 \(2N\) 个人,还有 \(2N>S\) 的限制,想想都不可能
#include <bits/stdc++.h>
using namespace std;
#define repeat(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define repeat_back(i,a,b) for(int i=(b)-1,_=(a);i>=_;i--)
int cansel_sync=(ios::sync_with_stdio(0),cin.tie(0),0);
const int N=200010; typedef long long ll;
#define int ll
int n,s;
signed main(){
cin>>n>>s;
if(2*n<=s){
cout<<"YES"<<endl;
repeat(i,0,n-1)cout<<2<<' ';
cout<<s-(n-1)*2<<endl;
cout<<1<<endl;
}
else
cout<<"NO"<<endl;
return 0;
}
E. Restorer Distance
我们可以很轻松地算出,如果最终状态为每个位置高度为 \(H\) 的代价是多少。因此 \(H\) 的选取就成为本题的关键
大佬们就能一眼看出,代价对 \(H\) 存在单峰(随 \(H\) 先递减后递增),因此三分。那么为什么存在单峰呢?太难顶了,留作习题答案略(qwq)
我的三分其实就是二分,感觉能加一点速(?)感觉最快的可能是1.618优选法,但是我写不来(?)
我的方法是 \(O(n\log n\)),标程是 \((\log^2n)\) 因为处理了前缀和,get_cost() 里只要 lowerbound 一下就能算答案了,真是妙啊
#include <bits/stdc++.h>
using namespace std;
#define repeat(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define repeat_back(i,a,b) for(int i=(b)-1,_=(a);i>=_;i--)
int cansel_sync=(ios::sync_with_stdio(0),cin.tie(0),0);
const int N=100010; typedef long long ll;
#define int ll
int n,A,B,C;
int a[N];
int get_cost(int h){
int up=0,down=0;
repeat(i,0,n)
(h>a[i]?up:down)+=abs(a[i]-h);
int t=min(up,down);
return (up-t)*A+t*B+(down-t)*C;
}
signed main(){
cin>>n>>A>>C>>B; B=min(B,A+C);
repeat(i,0,n)
cin>>a[i];
int l=0,r=1e9+1;
while(l<r){
int x=(l+r)/2,y=x+1;
if(get_cost(x)<get_cost(y))r=y-1;
else l=x+1;
}
cout<<get_cost(l)<<endl;
return 0;
}
Codeforces Round #643 (Div. 2) 题解 (ABCDE)的更多相关文章
- Codeforces Round #646 (Div. 2) 题解 (ABCDE)
目录 A. Odd Selection B. Subsequence Hate C. Game On Leaves D. Guess The Maximums E. Tree Shuffling ht ...
- Codeforces Round #182 (Div. 1)题解【ABCD】
Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...
- Codeforces Round #608 (Div. 2) 题解
目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...
- Codeforces Round #525 (Div. 2)题解
Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...
- Codeforces Round #528 (Div. 2)题解
Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...
- Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F
Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...
- Codeforces Round #677 (Div. 3) 题解
Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...
- Codeforces Round #665 (Div. 2) 题解
Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...
- Codeforces Round #383 (Div. 2) 题解【ABCDE】
Codeforces Round #383 (Div. 2) A. Arpa's hard exam and Mehrdad's naive cheat 题意 求1378^n mod 10 题解 直接 ...
随机推荐
- LRU缓存的实现
文章目录 LRU简介 LRU算法分析 实现代码 节点类 双向链表 LRUCache类 测试类 总结 LRU简介 LRU是"Least Recently Used"的简写,意思是最近 ...
- Session、Cookie与Token
http协议是无状态协议 协议是指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或规则,超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML)文档从Web服务器传送到 ...
- 【Problems】MySQL5.7 datetime 默认值设为‘0000-00-00 00:00:00'值出错
记录 MySQL5.7 datetime 默认值设为'0000-00-00 00:00:00'值出错 我的MySQL版本 mysql --version 5.7.28 C:\Users\x1c> ...
- 【栈和队列】2、栈的基本实现 - Java
简单记录 - bobo老师的玩转算法系列–玩转数据结构 - 栈和队列 栈的实现 Stack<E> void push(E) E pop() E peek() int getSize() b ...
- LeetCode653. 两数之和 IV - 输入 BST
题目 直接暴力 1 class Solution { 2 public: 3 vector<int>ans; 4 bool findTarget(TreeNode* root, int k ...
- CTFHub - Web(五)
eval执行: 1.进入网页,显示源码, <?php if (isset($_REQUEST['cmd'])) { eval($_REQUEST["cmd"]); } els ...
- SparkStreaming和Kafka基于Direct Approach如何管理offset实现exactly once
在之前的文章<解析SparkStreaming和Kafka集成的两种方式>中已详细介绍SparkStreaming和Kafka集成主要有Receiver based Approach和Di ...
- 两节锂电池保护IC,芯片电路图如何设计
两节锂电池出了充电电路外,必须搭配的也就是两节锂电池的保护板电路和芯片了.对两节节串联可再充电锂离子/锂聚合物电池的过充电.过放电和过电流进行保护.和电池反接保护功能,这些都是极其重要的. 首先设计两 ...
- 关于SQL Server 镜像数据库快照的创建及使用
从SQL Server 2005 SP 起,SQL 开始支持数据库镜像.它的设计目的是试图为SQL Server 提供一个具有实时性数据同步的灾难恢复技术,即能够提供数据冗余备份,切换起来比较方便.每 ...
- Linux 安装分区设置分区大小
一.Linux分区挂载点介绍 Linux分区挂载点介绍,推荐容量仅供参考不是绝对,跟各系统用途以及硬盘空间配额等因素实际调整: 分区类型 介绍 备注 /boot 启动分区 一般设置100M-200M, ...