Codeforces Round #601 (Div. 2) ABC 题解
A. Changing Volume
题意:每次可以加减5或2或1,问最少几步将a变成b。
思路:水题,贪心先搞把5取完再取2再取1。
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll a = read(), b = read();
ll d = abs(a - b);
ll ans = 0;
ans += d/5;
d %= 5;
ans += d/2;
d %= 2;
if(d&1) ans++;
cout<<ans<<'\n';
}
return 0;
}
B. Fridge Lockers
题意:n个冰箱,要求每个冰箱至少除自身之外的2或以上的冰箱。连一次代价是a[u]+a[v],问怎么连代价最少。
思路:先将所有点连起来,这个时候用环是耗费最少的而且要的链子也最少。所以m如果小于n的话肯定不行。这个情况下如果有剩的就全连在最小的两个点上。最后注意n=2的时候肯定不行!输出-1。
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e3+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
typedef struct Pos
{
ll val;
ll id;
}P;
P a[maxn];
typedef struct Ans
{
ll u;
ll v;
}A;
A ans[maxn];
bool cmp(P a, P b)
{
return a.val < b.val;
}
int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n = read(), m = read();
rep(i,1,n) a[i].val = read(), a[i].id = i;
if(m<n||n==2)
{
cout<<-1<<'\n';
continue;
}
sort(a+1,a+1+n,cmp);
ll sum = 0; int p = 0;
for(int i=1, j=i+1; i<=n; i++,j=j+1>n?1:j+1)
ans[++p].u = i, ans[p].v = j, sum += a[i].val + a[j].val, m--;
while(m)
ans[++p].u = a[1].id, ans[p].v = a[2].id, sum += (a[1].val + a[2].val), m--;
cout<<sum<<'\n';
rep(i,1,p)
cout<<ans[i].u<<' '<<ans[i].v<<'\n';
}
return 0;
}
C. League of Leesins
题意:有一个1->n的排列,但只告诉你前n-2个三元组,而且三元组彼此的顺序和内部的顺序都是打乱的,问你可能的原序列是什么。
思路:拓扑排序。先脑补一下原序列每个元素在三元组里出现的次数,从头到尾一定是:
1 2 3 3 3 ... 3 3 2 1 的形式。最开头和最后的选了1次,第二个和倒数第二个被选了2次,其他都选了3次。
这里就是用拓扑排序的破题口,不管他给的内部顺序如何,这三个肯定是连在一起的。我们可以先将三元组内部建边,每个三元组内部的元素都入度++。最后跑拓扑排序就是找那些入度为1的。
最后注意一下为了按顺序输出,最后两个2 1我们强行改成3 3。
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
vector<vector<ll> > D(maxn);
ll in[maxn];
vector<ll> ans;
bool vis[maxn];
ll n;
void Topsort()
{
queue<ll> q;
rep(i,1,n) if(in[i]==1) q.push(i), vis[i] = 1;
while(!q.empty())
{
ll cur = q.front(); q.pop();
if(in[cur] <= 1) ans.pb(cur);
if(D[cur].size()==0) continue;
rep(i,0,D[cur].size()-1)
{
ll v = D[cur][i]; in[v] --;
if(!vis[v]&&in[v]<=1)
{
q.push(v);
vis[v] = 1;
}
}
}
}
int main()
{
n = read();
rep(i,1,n-2)
{
ll x = read(), y = read(), z = read();
D[x].pb(y), D[y].pb(x), D[x].pb(z),D[z].pb(x), D[y].pb(z), D[z].pb(y);
in[x]++, in[y]++, in[z] ++;
}
int flag = 1;
for(int i=1; i<=n; i++)
{
if(in[i]==1)
{
if(flag) flag=0;
else
{
in[i] = 3;
for(int j=0; j<D[i].size();j++)
{
if(in[D[i][j]]==2)
{
in[D[i][j]] = 3;
break;
}
}
}
}
}
Topsort();
for(int i=0; i<ans.size();i++)
cout<<ans[i]<<' ';
cout<<'\n';
return 0;
}
Codeforces Round #601 (Div. 2) ABC 题解的更多相关文章
- Codeforces Round #312 (Div. 2) ABC题解
[比赛链接]click here~~ A. Lala Land and Apple Trees: [题意]: AMR住在拉拉土地. 拉拉土地是一个很漂亮的国家,位于坐标线.拉拉土地是与著名的苹果树越来 ...
- # Codeforces Round #529(Div.3)个人题解
Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...
- Codeforces Round #557 (Div. 1) 简要题解
Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...
- 【cf比赛记录】Codeforces Round #601 (Div. 2)
Codeforces Round #601 (Div. 2) ---- 比赛传送门 周二晚因为身体不适鸽了,补题补题 A // http://codeforces.com/contest/1255/p ...
- Codeforces Round #540 (Div. 3) 部分题解
Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...
- Codeforces Round #538 (Div. 2) (A-E题解)
Codeforces Round #538 (Div. 2) 题目链接:https://codeforces.com/contest/1114 A. Got Any Grapes? 题意: 有三个人, ...
- Codeforces Round #531 (Div. 3) ABCDEF题解
Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...
- Codeforces Round #527 (Div. 3) ABCDEF题解
Codeforces Round #527 (Div. 3) 题解 题目总链接:https://codeforces.com/contest/1092 A. Uniform String 题意: 输入 ...
- Codeforces Round #499 (Div. 1)部分题解(B,C,D)
Codeforces Round #499 (Div. 1) 这场本来想和同学一起打\(\rm virtual\ contest\)的,结果有事耽搁了,之后又陆陆续续写了些,就综合起来发一篇题解. B ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
随机推荐
- DAY1--ROS基本认知
1.ROS基本框架 ROS架构如下图所示,可以将其分为三个层次:OS层.中间层和应用层. 1.1 应用层 应用层是用户直接交互的部分,包含以下核心组件: Master: ROS的核心协调者,负责节点( ...
- Nginx日志拆分(linux环境下)
1.新增shell脚本[nginx_log.sh],进行每日自动切割一次,存储在nginx文件夹下的logs下 #!/bin/bash #设置日志文件存放目录 LOG_HOME="/app/ ...
- 小模型工具调用能力激活:以Qwen2.5 0.5B为例的Prompt工程实践
在之前的分析中,我们深入探讨了cline prompt的设计理念(Cline技术分析:prompt如何驱动大模型对本地文件实现自主变更),揭示了其在激发语言模型能力方面的潜力.现在,我们将这些理论付诸 ...
- 拆解 Cursor Pro 自动化工具,看看它是怎么实现的?
深入解析Cursor Pro自动化工具的核心实现 从源码角度剖析关键技术 完整解读:注册.认证.机器码重置的自动化方案 项目概述 大家好,我是松哥.这篇文章将为大家详细解析一个Cursor自动化管 ...
- WPF 解决PasswordBox 属性Password无法绑定到后台的问题
在 WPF 中,你可以使用密码框的 Password 属性来绑定到后台,但是由于安全性考虑,WPF 的密码框不直接支持双向绑定.然而,你仍然可以通过其他方式实现将密码框的内容绑定到后台. 一种常见的方 ...
- Java编程--抽象类和接口的区别
No. 区别 抽象类 接口 1 关键字 abstract class interface 2 组成 构造方法.普通方法.抽象方法.static方法.常量.变量 抽象方法.全局常量 3 子类使用 cla ...
- 操作系统综合题之“采用时间片轮转调度算法(Round-Robin,RR)执行,分时系统中的进程可能出现的状态变化”
一.问题:某分时系统中的进程可能出现下图所示的状态变化,请回答下列问题: 1.根据图示,您认为该系统采用的是什么进程调度策略? 2.把图中所示的每一个状态变化的原因填在下表相应位置. 变化 原因 1 ...
- vault
目录 Vault使用场景 数据加密 访问控制 有时间限制的访问 灾备恢复 基于身份(Identity)的安全性 人类和机器认证 静态和动态secrets的Secrets engines Install ...
- 编译原理:python编译器--从AST到字节码
首先了解下从AST到生成字节码的整个过程: 编译过程 Python编译器把词法分析和语法分析叫做 "解析(Parse)", 并且放在Parser目录下. 从AST到生成 字节码的过 ...
- VMware NSX Manager SSL证书更新
安装 NSX 后,管理器节点和集群具有自签名证书.证书有效期为825天,到期后需要进行证书重新更新.如图所示,本环境中此次将有三个类型的证书即将到期需要替换:1.NSX 联合身份验证 PI(Local ...