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

比赛入口

寒假给自己立个flag:每日一场div3!!div3里都是思维大佬,希望思维也能转快一点…

C Friends and Gifts

做法:题意很好理解,就是每个位置都有一个数字,要使得每个数字和当前标号不相等,已经给出一些数字,0是需要填补的数字,输出一种恰当方案就行…当时写的时候没想到该用什么方法才更好写,WA了以后耻辱睡着…其实只要确保后面那个待补数字和当前待补数字满足要求即可…这个还真没想到TAT

代码

#include<bits/stdc++.h>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int N = 2e5+5;
int a[N], vis[N];
vector<int> v1, v2;
int main() {
  int n; read(n);
  for(int i = 1; i <= n; ++i) {
    read(a[i]); vis[a[i]]++;
    if(!a[i]) v1.push_back(i);
  }
  for(int i = 1; i <= n; ++i) {
    if(!vis[i]) v2.push_back(i);
  }
  int len = v1.size();
  for(int i = 0; i < len - 1; ++i) {
    if(v1[i] == v2[i] || v1[i+1] == v2[i+1])
      swap(v2[i], v2[i+1]);
    a[v1[i]] = v2[i];
  }
  a[v1[len-1]] = v2[len-1];
  for(int i = 1; i <= n; ++i) {
    printf("%d ", a[i]);
  }
  return 0;
}

D Christmas Trees

做法:D题题意是要求构造出m个y,x和y每个数字两两不同,要求满足 j = 1 m min i = 1 n x i y j \sum\limits_{j=1}^{m}\min\limits_{i=1}^{n}|x_i - y_j| 这个公式…刚开始是想到要以每个点向两边扩散,刚开始也是不知道该怎么实现…但后来看到题解…直接用bfs的思路就可以了…

代码

#include<bits/stdc++.h>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
queue<int> q;
map<int, int> mp;
int main() {
  int n, m;
  read(n); read(m);
  int x;
  for(int i = 0; i < n; ++i) {
    read(x);
    q.push(x); mp[x] = 1;
  }
  int cnt = 0, d;
  LL res = 0;
  while(cnt < m) {
    x = q.front(); q.pop();
    d = mp[x];
    if(!mp[x-1]) {
      q.push(x-1);
      mp[x-1] = d + 1;
      cnt++;
      res += d;
    }
    if(!mp[x+1] && cnt < m) {
      q.push(x+1);
      mp[x+1] = d + 1;
      cnt++;
      res += d;
    }
  }
  cout << res << endl;
  for(auto it : mp) {
    if(it.second > 1) cout << it.first << " ";
  }
  cout << endl;
  return 0;
}

E New Year Parties

做法:题意是每个数字可以+1或者-1,要求最大的不同的数字和最小的不同的数字…直接贪心???看到这么短小的代码还是很惊诧…

代码

#include<bits/stdc++.h>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int N = 2e5+5;
LL a[N];
int main() {
  LL n; read(n);
  for(int i = 0; i < n; ++i) {
    read(a[i]);
  }
  sort(a, a+n);
  LL ls = -1e18, mn = 0, mx = 0, k;
  for(int i = 0; i < n; ++i) {
    if(a[i] - ls > 2) {
      mn++; ls = a[i];
    }
  }
  ls = -1e18;
  for(int i = 0; i < n; ++i) {
    k = max(a[i]-1, ls+1);
    if(k <= a[i] + 1) {
      mx++; ls = k;
    }
  }
  printf("%lld %lld", mn, mx);
  return 0;
}

发布了28 篇原创文章 · 获赞 14 · 访问量 2957

猜你喜欢

转载自blog.csdn.net/qq_43408978/article/details/104065321