Codeforces Round 665 赛后解题报告(暂A-D)
Codeforces Round 665 赛后解题报告
A. Distance and Axis
我们设 \(B\) 点 坐标为 \(x(x\leq n)\)。由题意我们知道
\]
\]
因此 \(n,k\) 同奇偶。我们分类来讨论,如果 \(n<k\),那么最优方案一定是让 \(k'=n,x=n\)。这一定是最小的,因为再要满足条件就 \(k'>n\) 了,肯定不优。若 \(n\geq k\)。则我们只需要满足同奇偶的条件,判断一下即可
//Don't act like a loser.
//You can only use the code for studying or finding mistakes
//Or,you'll be punished by Sakyamuni!!!
#include<bits/stdc++.h>
#define int long long
using namespace std;
int read() {
char ch=getchar();
int f=1,x=0;
while(ch<'0'||ch>'9') {
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') {
x=x*10+ch-'0';
ch=getchar();
}
return f*x;
}
int n,k;
signed main() {
int T=read();
while(T--) {
n=read();k=read();
if(n<k) {
cout<<k-n<<endl;
}
else {
cout<<(n-k)%2<<endl;
}
}
return 0;
}
B. Ternary Sequence
这个题就是直接贪心讨论即可。
我们来分析一下,其实这个题目 \(c_i\) 的计算根本没那么麻烦.我们只需贪心计算。我们先尽可能让 \(b\) 中的 \(2\) 产生最小负面影响。我们第一选择是拿 \(a\) 中的 \(0\) 一个一个去送死抵消。再拿 \(2\) 去抵消。最后才是 \(1\) 因为只有此时每一对都会产生 \(-2\) 的负面 BUFF。然后我们尽可能多的让 \(a\) 中的 \(2\) 产生正面 BUFF,和 \(b\) 中的 \(1\) 一个一个配对,剩下的随意排布都不会产生贡献或负贡献,算法就结束了。
//Don't act like a loser.
//You can only use the code for studying or finding mistakes
//Or,you'll be punished by Sakyamuni!!!
#include<bits/stdc++.h>
#define int long long
using namespace std;
int read() {
char ch=getchar();
int f=1,x=0;
while(ch<'0'||ch>'9') {
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') {
x=x*10+ch-'0';
ch=getchar();
}
return f*x;
}
int x[2],y[2],z[2];
signed main() {
int T=read();
while(T--) {
for(int i=0;i<=1;i++) {
x[i]=read();y[i]=read();z[i]=read();
}
int ans=0;
int tmp=min(z[1],x[0]);
z[1]-=tmp;
x[0]-=tmp;
if(z[1]) {
int t1=min(z[1],z[0]);
z[1]-=t1;
z[0]-=t1;
if(z[1]) {
ans-=z[1]*2;
y[0]-=z[1];
}
}
ans+=min(z[0],y[1])*2;
cout<<ans<<endl;
}
return 0;
}
C. Mere Array
虽然比赛的时候我写挂了一次,但我还是蛮喜欢这道题的
这个题的思路很妙,我们先来看题目中的条件:
In one operation, you can choose two different indices \(i\) and \(j\) (\(1\leq i\),\(j\leq n\)). If \(\gcd(a_i,a_j)\) is equal to the minimum element of the whole array a, you can swap \(a_i\) and \(a_j\)
这证明我们可以先找出这个最小值,即为 \(divi\)(全称divisor,注意 div 在头文件中出现过,不能用!)。我们只要判断一个数 \(a_i\),若 \(divi\nmid a_i\),我们要判断他是否在正确的位置上,即有多少的数比它小。若不在正确位置上,直接 \(\text{NO}\),否则继续判断。
一开始我是这样写的,但是由于写挂了,于是怀疑这个是错误的
其实这个怀疑很正常,因为我们认为的是两个都能被 \(divi\) 整除的数可以直接交换位置,但是如果 \(divi=2\),那么 \(4,8\) 不能直接交换位置。这边是我一开始的疑惑。但是后来,我们可以发现,\(4,8\),不能直接交换位置,我们可以间接交换呀。我们先把 \(2,4\) 换一下,再换一下 \(2,8\),最后再换一下 \(2,4\) 就完成了。因此原算法正确。只是我写挂了而已qwq
特别注意一下如果 \(a_i\)=\(a_{i-1}\),我们的要特殊处理
//Don't act like a loser.
//You can only use the code for studying or finding mistakes
//Or,you'll be punished by Sakyamuni!!!
#include<bits/stdc++.h>
#define int long long
using namespace std;
int read() {
char ch=getchar();
int f=1,x=0;
while(ch<'0'||ch>'9') {
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') {
x=x*10+ch-'0';
ch=getchar();
}
return f*x;
}
const int maxn=1e5+10;
int n,a[maxn],divi,b[maxn];
signed main() {
int T=read();
while(T--) {
n=read();
divi=1e9+10;
for(int i=1;i<=n;i++) {
a[i]=read();
b[i]=a[i];
divi=min(divi,a[i]);
}
sort(b+1,b+n+1);//排序
bool flag=1;
int pos=0;
for(int i=1;i<=n;i++) {
if(a[i]%divi!=0) {
pos=upper_bound(b+1,b+i+1,a[i])-b;
if(!(pos-1==i&&b[pos-1]==a[i])) {//如果 $a_i$=$a_{i-1}$,我们的要特殊处理
printf("NO\n");
flag=0;
break;
}
}
}
if(flag) {
printf("YES\n");
}
}
return 0;
}
D. Maximum Distributed Tree
贪心题石锤。
这个题有一个突破口,就是我们可以先用一遍 \(\text{dfs}\) 来求出所有边在答案 \(\sum\limits_{i=1}^{n-1}\sum\limits_{j=i+1}^{n} f(i,j)\) 中的贡献次数。所以我们可以有以下的式子:
\]
其中 \(con_{(u,v)}\) 表示 \((u,v)\) 这条边被经过次数,\(w_{(u,v)}\) 表示 \(u,v\) 这条边的边权。
如果我们以 \(1\) 为根,\(size_u\) 表示以 \(u\) 为根的子树的大小。那么连接 \(u,v\),且 \(u\) 为 \(v\) 的父亲的这条边对答案的贡献就是 \(con_{(u,v)}=size_u\times (n-size_u)\)。
我们再来维护边权。有两种情况,我们分别讨论
- 如果 \(m<n-1\) 那么一定会有边权为 \(1\) 的边出现。那么为了让 \(1\) 最少(题目要求),我们只有唯一解
- 否则我们可以让一些质因子合并,构造出 \(n-1\) 个边权。那么我们为了让答案最大,一定是把最大的 \(m-n+2\) 个边权堆在一起。证明很简单,就是因为如果我们有 \(a>b,k>0\),那么 \(k\cdot a>k\cdot b\)。最后给 \(w,con\) 数组排个序即可。
注意,对于 \(con\) 数组,先排序在取模;对于 \(w\) 数组,按顺序处理,由于数字太大在处理时就要取模,千万别排序!!!
//Don't act like a loser.
//You can only use the code for studying or finding mistakes
//Or,you'll be punished by Sakyamuni!!!
#include<bits/stdc++.h>
#define int long long
using namespace std;
int read() {
char ch=getchar();
int f=1,x=0;
while(ch<'0'||ch>'9') {
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') {
x=x*10+ch-'0';
ch=getchar();
}
return f*x;
}
const int maxn=1e5+10,mod=1e9+7;
int n,m;
int p[maxn],a[maxn],h[maxn],cnt,w[maxn],sz[maxn],con[maxn],tot;
struct edge {
int v,next;
}e[maxn<<1];
void addedge(int u,int v) {
e[++cnt].v=v;
e[cnt].next=h[u];
h[u]=cnt;
}
void insert(int u,int v) {
addedge(u,v);
addedge(v,u);
}
void dfs(int u,int fa) {
sz[u]=1;
for(int i=h[u];i;i=e[i].next) {
int v=e[i].v;
if(v!=fa) {
dfs(v,u);
sz[u]+=sz[v];
}
}
if(fa!=0) {
con[++tot]=sz[u]*(n-sz[u]);//不着急取模
}
}
signed main() {
int T=read();
while(T--) {
n=read();
fill(h+1,h+n+1,0);
cnt=0;
for(int i=1;i<n;i++) {
insert(read(),read());
}
m=read();
fill(w,w+n+1,0);
for(int i=1;i<=m;i++) {
p[i]=read();
}
sort(p+1,p+m+1);//这里要先排序
if(n-1<m) {//第一种
for(int i=1;i<n-1;i++) {
w[i]=p[i];
}
w[n-1]=p[n-1];
for(int i=n;i<=m;i++) {
w[n-1]=w[n-1]*p[i]%mod;
}
}
else {//第二种
for(int i=1;i<=m;i++) {
w[i]=p[i];
}
for(int i=m+1;i<n;i++) {
w[i]=1;
}
sort(w+1,w+n);
}
tot=0;
dfs(1,0);//维护con
int ans=0;
sort(con+1,con+n);
for(int i=1;i<n;i++) {
ans+=con[i]%mod*w[i]%mod;//贪心
ans%=mod;
}
cout<<ans<<endl;
}
return 0;
}
这里放一个拓展。因为在比赛时我有一个朋友把 product 理解为 sum,所以浪费了很多时间。不是无中生友qwq。大家看到这篇题解可以想一想如果是 sum 怎么做,可以在评论里交流一下~
Codeforces Round 665 赛后解题报告(暂A-D)的更多相关文章
- Codeforces Round 662 赛后解题报告(A-E2)
Codeforces Round 662 赛后解题报告 梦幻开局到1400+的悲惨故事 A. Rainbow Dash, Fluttershy and Chess Coloring 这个题很简单,我们 ...
- Codeforces Educational Round 92 赛后解题报告(A-G)
Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...
- Codeforces Round#320 Div2 解题报告
Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Fin ...
- Codeforces Round #277.5 解题报告
又熬夜刷了cf,今天比正常多一题.比赛还没完但我知道F过不了了,一个半小时贡献给F还是没过--应该也没人Hack.写写解题报告吧= =. 解题报告例如以下: A题:选择排序直接搞,由于不要求最优交换次 ...
- Codeforces Round #299 Div2 解题报告
这场比赛并没有打现场,昨天晚上做了ABCD四道题,今天做掉了E题 以前还没有过切完一场比赛的所有题呢~爽~ A. Tavas and Nafas Today Tavas got his test ...
- Codeforces Round #665 (Div. 2)
Codeforces Round #665 (Div. 2) A. Distance and Axis 如果\(B\)在\(O\)左边,那么只能是定值\(OA\) 如果\(B\)在\(OA\)中间 ...
- Codeforces Round #665 (Div. 2) 题解
Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...
- codeforces 581C. Developing Skills 解题报告
题目链接:http://codeforces.com/problemset/problem/581/C 题目意思:给出 n 个数:a1, a2, ..., an (0 ≤ ai ≤ 100).给出值 ...
- codeforces B. Simple Molecules 解题报告
题目链接:http://codeforces.com/problemset/problem/344/B 题目意思:这句话是解题的关键: The number of bonds of an atom i ...
随机推荐
- vue简单案例_动态添加删除用户数据
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 我的Python自学之路-001 列表的知识
#_date_:2020/9/11 '''列表和字典是python中用的最多的数据类型 假如要存储一个班级的人名,需要怎么做?有这么几种方法:1.定义很多个变量: name0 = 'wucaho' n ...
- Erlang+RabbitMQ Server的详细安装
Erlang(['ə:læŋ])是一种通用的面向并发的编程语言, 它有瑞典电信设备制造商爱立信所辖的CS-Lab开发,目的是创造一种可以应对大规模并发活动的编程语言和运行环境. Erlang官网:ht ...
- 01.vue数据绑定
Vue特点 渐进式: 渐进, 可以理解成一步一步的. 在使用Vue的时候, 我们不需要把整个Vue框架的东西都用上, 可以一步一步的根据需要慢慢的替换之前的代码. 自底向上逐层应用: 由底层开始, 把 ...
- 跟我一起学.NetCore之路由的最佳实现
前言 路由,这词绝对不陌生,不管在前端还是后端都经常提到,而这节不说其他,就聊.NetCore的路由:在之前的Asp.Net MVC 中,路由算是面试时必问的考点,可见其重要性,它的主要作用是映射UR ...
- Redis中String类型的相关命令操作
String append 如果key已存在,则直接在value追加值,如果key不存在,则会插件一个新的value为空的key,然后在追加 127.0.0.1:6379> set name l ...
- 手写:javascript中的关键字new
简单介绍一下new new再熟悉不过了,new后面跟着构造函数,可以创建对象,这个对象的原型指向构造函数的原型对象,说起来可能有点绕,直接看代码吧 function Person(name, age) ...
- Fowsniff靶机
Fowsniff靶机 主机探测+端口扫描. 扫目录没扫到什么,看一下页面源代码. 网站主页告诉我们这个站现在不提供服务了,并且因为收到了安全威胁,攻击者将他们管理员信息发布到了社交媒体上. 大家要科学 ...
- jpa基本常识
1.hibernate更新表结构配置 jpa hibernate框架配置 spring.jpa.properties.hibernate.hbm2ddl.auto = create-drop 其意思是 ...
- Centos-查看磁盘分区占用情况-df
df 检查linux系统中磁盘分区占用情况 相关选项 -h 以人类友好读方式显示 -k 以KB为单位输出磁盘分区使用情况 -m 以MB为单位输出磁盘分区使用情况 -a 列出所有文件系统分区情况,包 ...