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 题解的更多相关文章

  1. Codeforces Round #312 (Div. 2) ABC题解

    [比赛链接]click here~~ A. Lala Land and Apple Trees: [题意]: AMR住在拉拉土地. 拉拉土地是一个很漂亮的国家,位于坐标线.拉拉土地是与著名的苹果树越来 ...

  2. # Codeforces Round #529(Div.3)个人题解

    Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...

  3. Codeforces Round #557 (Div. 1) 简要题解

    Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...

  4. 【cf比赛记录】Codeforces Round #601 (Div. 2)

    Codeforces Round #601 (Div. 2) ---- 比赛传送门 周二晚因为身体不适鸽了,补题补题 A // http://codeforces.com/contest/1255/p ...

  5. Codeforces Round #540 (Div. 3) 部分题解

    Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...

  6. Codeforces Round #538 (Div. 2) (A-E题解)

    Codeforces Round #538 (Div. 2) 题目链接:https://codeforces.com/contest/1114 A. Got Any Grapes? 题意: 有三个人, ...

  7. Codeforces Round #531 (Div. 3) ABCDEF题解

    Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...

  8. Codeforces Round #527 (Div. 3) ABCDEF题解

    Codeforces Round #527 (Div. 3) 题解 题目总链接:https://codeforces.com/contest/1092 A. Uniform String 题意: 输入 ...

  9. Codeforces Round #499 (Div. 1)部分题解(B,C,D)

    Codeforces Round #499 (Div. 1) 这场本来想和同学一起打\(\rm virtual\ contest\)的,结果有事耽搁了,之后又陆陆续续写了些,就综合起来发一篇题解. B ...

  10. 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 ...

随机推荐

  1. 配置Thymeleaf模板引擎

    1).thymeleaf-starter: 关闭缓存 2).静态资源都放在static文件夹下就可以按照路径直接访问 3).页面放在templates下,直接访问 springboot ,访问项目的时 ...

  2. 【Linux】基于Exynos4412的U-Boot引导程序移植

    [Linux]基于Exynos4412的U-Boot引导程序移植 零.准备 首先我们得去下载好U-Boot的源码,因为用的芯片是2012年出的Exynos4412,因此我们选择这个时间节点附近的U-B ...

  3. Sql Server数据库远程连接访问设置

    步骤一:设置sql server数据库 1.以新建一个新用户名test作为远程连接登录名.在本地登录sql server数据库,安全性->右键用户名 2.点击根目录右键,选择属性 选择安全性 选 ...

  4. 智能简历解析器实战教程:基于Spacy+Flask构建自动化人才筛选系统

    一.项目背景与技术选型 在人力资源领域,每天需要处理数百份简历的HR团队面临巨大挑战:人工筛选效率低下.关键信息遗漏风险高.跨文档对比分析困难.本教程将构建一个端到端的智能简历解析系统,通过NLP技术 ...

  5. SAP HANA使用命令行快速导出导入

    楔子 今天折腾了接近一下午,就为了使用SAP HANA自带的命令行工具来导出数据备份. SAP HANA(后续简称Hana)是内存数据库,性能这一方面上还真没怕过谁. 由于SAP HANA提供了Han ...

  6. MySQL 中使用索引一定有效吗?如何排查索引效果?

    MySQL 中使用索引一定有效吗?如何排查索引效果? 虽然索引是提升 MySQL 查询性能的常见手段,但并不是所有情况下索引都会有效.索引的使用取决于查询条件.数据分布.索引设计等多个因素.如果索引未 ...

  7. vue属性/子属性监听watch的几种方法

    特殊字符法 特殊字符+deep法 直接deep法 常规法 直接用如下代码示例吧: data(){ return { goBackHeader:'添加排班', scheduleForm:{ schedu ...

  8. 【记录】Python|Python3程序测试速度的整个流程、方法对比和选取方式

    参考:Python3.7中time模块的time().perf_counter()和process_time()的区别 其他的博客太!长!了!我实在看不下去了,每次都不记得什么场景用什么函数. 让我来 ...

  9. P10833 [COTS 2023] 下 Niz题解

    题意: 给定长度为 \(N\) 的序列 \(a\),求满足以下条件的 \((l,r)\) 对数: \(1\le l\le r\le N\): \(a_l,a_{l+1},\cdots,a_{r-1}, ...

  10. vue3 基础-data-methods-computed-watch

    本篇来简单了解 vue 的数据, 方法, 计算属性和监听器等相关内容. data ( ) vue 里面的 data ( ) 函数返回一些能供模板 template 直接使用的数据, 以变量的方式进行 ...