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;
}