补题: Codeforces Round #611 (Div. 3)

为了督促自己补题!以后都开博客记录!

本次比赛:Codeforces Round #611 (Div. 3)
比赛地址:点我
时间:2020/1/22

前面好多坑没有填

Div3系列从C题开始做

C:大概意思是给出一个数组,数组为1-n的一个排列,会把一些位置上的元素置为0,补充完整,保证下标和该下标的元素不一样。
思路:把下标和元素处理出来,若下标和元素不一样则就是这个元素,若相等则保存进另一个数组A。若A只有一个则随便找一个交换即可,若有多个则向右移动一个,比如 1 2 3 改成 2 3 1;
D:大概意思是,给出一个数组A,要你找出另一个数组B,使得B中每一个数到A中最近的元素的距离之和最小。bfs即可
E:大概意思:每一个数可以加一,不变,减一,求出最少可能是多少个数,最多可能是多少个数。
思路:贪心,见代码;

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double pi = acos(-1);
namespace {
  template <typename T> inline void read(T &x) {
    x = 0; T f = 1;char s = getchar();
    for(; !isdigit(s); s = getchar()) if(s == '-') f = -1;
    for(;  isdigit(s); s = getchar()) x = (x << 3) + (x << 1) + (s ^ 48);
    x *= f;
  }
}
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define lowbit(x) x & (-x)
#define pii pair<int,int>
#define fi first
#define se second
const int N = 2e5+5;
int a[N], b[N];
int n;
void Maxsolve() {
  memset(b, 0, sizeof b);
  int ans = 0;
  for(int i = 1; i <= n; i++) {
    if(!b[a[i]-1]) {
      ans++;
      b[a[i]-1]++;
    }else if(!b[a[i]]) {
      ans++;
      b[a[i]]++;
    }else if(!b[a[i]+1]) {
      ans++;
      b[a[i]+1]++;
    }
  }
  cout << ans << endl;
}
void Minsolve() {
  int ans = 0;
  for(int i = 1; i <= n; i++) {
    if(!b[a[i]-1] && !b[a[i]] && !b[a[i]+1]) {
      ans++;
      b[a[i]+1]++;
    }
  }
  cout << ans << " ";
}
int main() {
  read(n);
  for(int i = 1; i <= n; i++) {
    read(a[i]);
  }
  sort(a + 1, a + 1 + n);
  Minsolve();
  Maxsolve();
}

F:给出一颗树,每个点的下标为i, 每个点的权值为这个点的子树权值和,一个点的权值为2^i。按权值的大小给出每条边权值更大的点,还原这棵树。输出根和每条边。边可以任意顺序输出
思路:首先第一个点一定是根,未出现的点一定是叶子节点,显然,叶子节点最大的应该和数组中越后的配对。配对完之后它的入度减一以便后续配对;
代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double pi = acos(-1);
namespace {
  template <typename T> inline void read(T &x) {
    x = 0; T f = 1;char s = getchar();
    for(; !isdigit(s); s = getchar()) if(s == '-') f = -1;
    for(;  isdigit(s); s = getchar()) x = (x << 3) + (x << 1) + (s ^ 48);
    x *= f;
  }
}
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define lowbit(x) x & (-x)
#define pii pair<int,int>
#define fi first
#define se second
const int N = 2e5+5;
int d[N], a[N];
priority_queue<int> q;
int main() {
  int n;
  read(n);
  for(int i = 1; i < n; i++) {
    read(a[i]);
    d[a[i]]++;
  }
  for(int i = 1; i <= n; i++) {
    if(!d[i]) {
      q.push(-i);
    }
  }
  printf("%d\n", a[1]);
  for(int i = n-1; i >= 1; i--) {
    int v = q.top(); q.pop();
    printf("%d %d\n", a[i], -v);
    d[a[i]]--;
    if(!d[a[i]]) q.push(-a[i]);
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值