A. Subset Mex

题意:把一个集合(可能有重复元素)分成两部分,使得每部分的缺少的最小非负整数的和最大。

其中在原序列里面就缺少的那个最小非负整数肯定是躲不掉的。就先把这个数之前的所有数放在一个集合,另外的放另一个集合。这样贪心是最优的。

view code
#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} }; ll a[maxn];
ll Map[150]; int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n = read();
mem(Map,0);
rep(i,1,n) a[i] = read(), Map[a[i]]++;
ll obj = 0;
rep(i,0,n) if(!Map[i]) {obj = i; break;}
rep(i,0,obj) Map[i]--;
rep(i,1,n) if(Map[a[i]]==0) a[i] = -1;
mem(Map,0);
rep(i,1,n) Map[a[i]]++;
ll obj1 = 0;
rep(i,0,n) if(!Map[i]) {obj1 = i; break; }
cout<<obj+obj1<<'\n';
}
return 0;
}

B. Maximum Product

题意:给一个序列,找出一个五元组,使得其积最大。

思路:贪心。分类讨论一下

全负

四负一正

三负二正

二负三正

一负四正

全正

的情况。如果结果是负数的就挑各自绝对值最小的,反之就挑绝对值最大的几个。

view code
#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 = 2e5+200;
const ll inf=0x3f3f3f3f3f3f3f3f;
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} }; ll a[maxn];
ll dp[maxn];
vector<ll> neg;
vector<ll> pos; int main()
{
int kase;
cin>>kase;
while(kase--)
{
neg.clear(), pos.clear();
ll n = read();
rep(i,1,n) a[i] = read();
rep(i,1,n)
{
if(a[i]<0) neg.pb(a[i]);
else pos.pb(a[i]);
}
ll ans = -inf;
if(neg.size()+pos.size()>=5)
{
sort(neg.begin(), neg.end());
sort(pos.begin(),pos.end()); if(neg.size()>=4&&pos.size()>=1)
ans = max((ll)neg[0]*neg[1]*neg[2]*neg[3]*pos[pos.size()-1], ans); if(neg.size()>=3&&pos.size()>=2)
ans = max((ll)neg[neg.size()-1]*neg[neg.size()-2]*neg[neg.size()-3]*pos[0]*pos[1],ans); if(neg.size()>=2&&pos.size()>=3)
ans = max((ll)neg[0]*neg[1]*pos[pos.size()-1]*pos[pos.size()-2]*pos[pos.size()-3],ans); if(neg.size()>=1&&pos.size()>=4)
ans = max((ll)neg[neg.size()-1]*pos[0]*pos[1]*pos[2]*pos[3],ans); if(pos.size()>=5)
ans = max((ll)pos[pos.size()-1]*pos[pos.size()-2]*pos[pos.size()-3]*pos[pos.size()-4]*pos[pos.size()-5],ans); if(neg.size()>=5)
ans = max((ll)neg[neg.size()-1]*neg[neg.size()-2]*neg[neg.size()-3]*neg[neg.size()-4]*neg[neg.size()-5], ans);
}
cout<<ans<<'\n';
}
return 0;
}

C. Link Cut Centroids

题意:给一颗树,定义一个特殊点为删除它和连接的边后剩下子树最大节点数最小。现在让你在保持树结构的前提下,改一条边,使得这个特殊点唯一。

思路:其实这个特殊点就是重心。

重心怎么求呢?

随便找个结点当根,先统计所有结点的子树包含结点,然后用一个变量存每个结点子树数量的最大值。其中最大值最小的那个即是重心。

重心有个性质就是如果不唯一,只能最多存在两个,且相邻。

那么这个题求完重心后就看看唯一与否,是的话直接随便找条和它相连的边删掉再连回去。不是的话就把另一个重心的另外一个相连点连到当前这个上。

view code
#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} }; ll Map[maxn];
vector<vector<ll> > D(maxn);
ll siz[maxn];
ll son[maxn];
bool vis[maxn];
ll n; ll dfs(ll x)
{
ll cur = 0;
ll ma = 0;
vis[x] = 1;
for(int i=0 ;i<D[x].size(); i++)
{
ll y = D[x][i];
if(vis[y]) continue;
cur += dfs(y);
ma = max(ma,siz[y]);
}
son[x] = ma = max(ma, n-cur-1);
siz[x] = cur+1;
return cur+1;
} int main()
{
int kase;
cin>>kase;
while(kase--)
{
n = read();
rep(i,0,n+1) Map[i] = 0, D[i].clear(), vis[i] = 0, son[i] = 0, siz[i] = 0;
rep(i,1,n-1)
{
ll x = read(), y = read();
D[x].pb(y);
D[y].pb(x);
}
ll m = dfs(1); ll obj = 1;
ll mi = inf;
rep(i,1,n)
{
Map[son[i]]++;
if(son[i]<mi) mi = son[i], obj = i;
}
if(Map[mi]==1)
{
ll v = D[obj][0];
cout<<obj<<' '<<v<<'\n';
cout<<obj<<' '<<v<<'\n';
}
else
{
ll v = 1;
for(int i=0; i<D[obj].size(); i++)
if(son[D[obj][i]]==mi) {v=D[obj][i]; break;}
ll vv = 1;
for(int i=0; i<D[v].size(); i++) if(D[v][i]!=obj) {vv=D[v][i]; break;}
cout<<vv<<' '<<v<<'\n';
cout<<vv<<' '<<obj<<'\n';
}
}
return 0;
}

Codeforces Round #670 (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. Codeforces Round #540 (Div. 3) 部分题解

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

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

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

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

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

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

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

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

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

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

  10. Codeforces Round #545 (Div. 1) 简要题解

    这里没有翻译 Codeforces Round #545 (Div. 1) T1 对于每行每列分别离散化,求出大于这个位置的数字的个数即可. # include <bits/stdc++.h&g ...

随机推荐

  1. Python简单数据分析

    1.分析思路 以贵族价格表为例 a.使用Python连接MySQL数据库 b.从noble_right表查询贵族名称,开通价格 c.将这两组值作为XY轴绘制直方图 2.编写代码: # -*- codi ...

  2. 安装yml 与 wget

    一.备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-B ...

  3. eolinker校验规则之 Json结构定位:返回结果校验的方法和案例(父参、子参内容校验)

    如下图,订单编号的参数在data父字段内 Eolinker返参校验的写法就需要有些变化 先写Data父参,添加子字段,再写子参 预期结果不支持全局变量 可通过添加绑定,绑定前一个接口返回参数,进行匹配

  4. 科研新体验:刘同学深度试用ADTF软件反馈揭晓!

    一.前言 作为一名高校的科研工作者,在高校的科研工作中,经常需要处理各种复杂的数据流,尤其是视频采集和处理的工作,对数据的实时性和精度要求非常高,我首次试用ADTF时,主要负责开发一个集成FFmpeg ...

  5. Java 中的 CMS 和 G1 垃圾收集器如何维持并发的正确性?

    Java 中的 CMS 和 G1 垃圾收集器如何维持并发的正确性? CMS(Concurrent Mark-Sweep)和 G1(Garbage-First)垃圾收集器是两种低延迟的垃圾回收器,它们通 ...

  6. thinkphphp 计算分页 和分页总数 和sql计算分页 php

    利用page计算分页 $p=input('p')?input('p'):1; $limit=6; $res=db('points_log')->where(['p_uid'=>$uid,' ...

  7. RPC实战与核心原理之时钟轮

    时钟轮在RPC中的应用 回顾 在分布式环境下,RPC 框架自身以及服务提供方的业务逻辑实现,都应该对异常进行合理地封装,让使用方可以根据异常快速地定位问题:而在依赖关系复杂且涉及多个部门合作的分布式系 ...

  8. WPF竖向排列的按钮

    <Button Width="100" Height="150"> <Button.Content> <TextBlock Tex ...

  9. C# Environment.CurrentDirectory和AppDomain.CurrentDomain.BaseDirectory的区别

    Environment.CurrentDirectory 和 AppDomain.CurrentDomain.BaseDirectory 都是C#中用于获取当前应用程序的目录路径的方法,但是它们的用途 ...

  10. codeup之等腰梯形

    Description 请输入高度h,输入一个高为h,上底边长为h 的等腰梯形(例如h=4,图形如下). **** ****** ******** ********** Input 输入第一行表示样例 ...