首页 > 其他分享 >Codeforces Round 980 (Div. 2)

Codeforces Round 980 (Div. 2)

时间:2024-10-26 13:59:01浏览次数:1  
标签:std kN int 980 cin Codeforces sum Div LL

目录

写在前面

比赛地址:https://codeforces.com/contest/2030

赛时被 B 硬控 1h,后面两题一眼秒了一共写了 20min 呃呃。

还好是小号。

A 签到

讨论一下很容易算出来最优决策。

//
/*
By:Luckyblock
*/
#include <bits/stdc++.h>
#define LL long long
//=============================================================
//=============================================================
//=============================================================
int main() {
  //freopen("1.txt", "r", stdin);
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  int T; std::cin >> T;
  while (T --) {
    int a, b; std::cin >> a >> b;
    if (a >= b) {
      std::cout << a << "\n";
      continue;
    }

    int x = b - a;
    if (x > a) {
      std::cout << 0 << "\n";
    } else {
      std::cout << a - x << "\n";
    }
  }
  return 0;
}

B 贪心,模拟

显然要确定某个按钮对应的饮料数量,当且仅当按下这个按钮后没有获得新的饮料。

则可知最优的操作,是每次把当前未确定的按钮全按一遍。

于是考虑将 \(a_i\) 排序后模拟上述过程即可,在此过程中计算每轮按的数量以及该轮获得的饮料数量。

//
/*
By:Luckyblock
*/
#include <bits/stdc++.h>
#define LL long long
const int kN = 2e5 + 10;
//=============================================================
LL n, k, a[kN];
//=============================================================
//=============================================================
int main() {
  // freopen("1.txt", "r", stdin);
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  int T; std::cin >> T;
  while (T --) {
    std::cin >> n >> k;
    std::map<int, int> cnt;
    for (int i = 1; i <= n; ++ i) std::cin >> a[i], ++ cnt[a[i]];

    LL ans = 0, cnt0 = 0, round = 0;
    for (auto [x, y]: cnt) {
      if (k <= 0) break;
      int t = std::min((LL) ceil(1.0 * k / (x - round)), n - cnt0);
      ans += t * (x - round), k -= t * (x - round);
      cnt0 += y, round = x;
      if (k <= 0) break;
      ans += y;
    }
    std::cout << ans + k << "\n";
  }
  return 0;
}

C 贪心,结论,思维

发现一组两个数内部的逆序对不受影响,只需要考虑每组之间的就行。

考虑将每组看成二维平面上的点,发现逆序对最小时应该让这些点满足 \(x+y\) 递增即可。

正确性显然,讨论一下两个点之间的两维关系即可。

//
/*
By:Luckyblock
*/
#include <bits/stdc++.h>
#define LL long long
#define pii std::pair<int,int>
#define mp std::make_pair
const int kN = 2e5 + 10;
//=============================================================
int n;
struct Data {
  int sum, x, y;
} a[kN];
//=============================================================
bool cmp(Data fir_, Data sec_) {
  return fir_.sum < sec_.sum;
}
//=============================================================
int main() {
  //freopen("1.txt", "r", stdin);
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  int T; std::cin >> T;
  while (T --) {
    std::cin >> n;
    for (int i = 1; i <= n; ++ i) {
      int x, y; std::cin >> x >> y;
      a[i] = (Data) {x + y, x, y};
    }
    std::sort(a + 1, a + n + 1, cmp);
    for (int i = 1; i <= n; ++ i) std::cout << a[i].x << " " << a[i].y << " ";
    std::cout << "\n";
  }
  return 0;
}

D 图论转化,最短路

发现这个人的策略一定是不断地重复往前做一段题,然后往后跳。在此过程中的价值之和,即到达的最远的位置 \(i\) 的价值的前缀和 \(\operatorname{sum}_i\),减去选择跳的位置的权值之和。

考虑枚举每一个到达的最远的位置 \(i\),并最小化跳的位置的权值之和,考虑建边 \(i\rightarrow b_i, i\operatorname{i - 1}\),发现这等价于最小化到达每个位置 \(i\) 的最短路 \(\operatorname{dis}_i\)。

于是建图后跑最短路,对于能到达的点取 \(\operatorname{sum}_i -\operatorname{dis}_i\) 的最大值即可。

//
/*
By:Luckyblock
*/
#include <bits/stdc++.h>
#define LL long long
#define pr std::pair
#define mp std::make_pair
const int kN = 4e5 + 10;
const LL kInf = 1e18;
//=============================================================
int n, a[kN], b[kN]; 
LL sum[kN], dis[kN];
bool vis[kN];
std::vector<pr <int, int> > edge[kN];
//=============================================================
void Dijkstra() {
  std::priority_queue<pr <LL, int> > q;
  for (int i = 1; i <= n; ++ i) {
    dis[i] = kInf; 
    vis[i] = 0;
  }
  dis[1] = 0;
  q.push(mp(0, 1));

  while (!q.empty()) {
    int u = q.top().second; q.pop();
    if (vis[u]) continue;
    vis[u] = 1;
    for (auto [v, w]: edge[u]) {
      if (dis[v] > dis[u] + w) {
        dis[v] = dis[u] + w;
        q.push(mp(-dis[v], v));
      }
    }
  }
}
//=============================================================
int main() {
  //freopen("1.txt", "r", stdin);
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  int T; std::cin >> T;
  while (T --) {
    std::cin >> n;
    for (int i = 1; i <= n; ++ i) {
      std::cin >> a[i], sum[i] = sum[i - 1] + a[i];
      edge[i].clear();
    }
    for (int i = 1; i <= n; ++ i) {
      std::cin >> b[i];
      if (b[i] > i) edge[i].push_back(mp(b[i], a[i]));
      if (i > 1) edge[i].push_back(mp(i - 1, 0));
    }
    Dijkstra();
    LL ans = 0;
    for (int i = 1; i <= n; ++ i) {
      if (dis[i] < kInf) ans = std::max(ans, sum[i] - dis[i]);
    }
    std::cout << ans << "\n";
  }
  return 0;
}

写在最后

我是傻逼。

标签:std,kN,int,980,cin,Codeforces,sum,Div,LL
From: https://www.cnblogs.com/luckyblock/p/18504014

相关文章

  • Codeforces Round 981 (Div. 3) 10.24 (ABCDE)题解
    CodeforcesRound981(Div.3)2024.10.24题解A.SakurakoandKosuke题意:\(Sakurako\)与\(Kosuke\)正在玩游戏,一个点在原点\(x=0\)的起始位置。对于第\(i\)次操作,点会移动\(2\asti-1\)步。两人轮流操作,Sakurako先手,每次将点往负方向移动;Kosuke每次将点往正方向移动......
  • Codeforces Round 979 (Div. 2)
    目录写在前面A签到B构造C博弈D模拟E组合数学写在最后写在前面比赛地址:https://codeforces.com/contest/2030。赛时E看错题了变成神题了而且居然还口胡了一个自然根号的做法还写出来了然而样例没过最后才发现读错题了妈的。掉分!A签到\(b,c\)即前缀最小值和最大值,显......
  • Codeforces Round 981 (Div. 3)A-D题解
    CodeforcesRound981(Div.3)A.SakurakoandKosukeSakurakoandKosukedecidedtoplaysomegameswithadotonacoordinateline.Thedotiscurrentlylocatedinposition\(x=0\).Theywillbetakingturns,andSakurakowillbetheonetostart.Ont......
  • Codeforces Round 981 (Div. 3) G
    G.SakurakoandChefir因为没有找到类似的题解,顺便记录下来题目给定一棵树,树上有\(n\)个顶点,以顶点\(1\)为根。樱子带着她的猫Chefir穿过这棵树,樱子走神了,Chefir跑开了。为了帮助樱子,浩介记录了他的\(q\)次猜测。在\(i\)次猜测中,他假设Chefir在顶点\(v_i\)迷路,......
  • 倒计时功能实现:认识了css选择器 div[id^=“countdown-“]
    html倒计时<!DOCTYPEhtml><htmllang="zh-CN"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>倒计时功能实现</ti......
  • 【CodeForces训练记录VP】Codeforces Round 933 (Div. 3)
    https://codeforces.com/contest/1941训练情况50min后罚坐反思C题刚开始思路错了,以为是删字符串最后面,然后漏考虑掉两字符串部分拼接的情况A题直接模拟,求\(a_i+b_j\lek\)的对数。#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;voidsolve......
  • Codeforces Round 972 (Div. 2)
    CodeforcesRound972(Div.2)总结A#include<bits/stdc++.h>usingnamespacestd;intn;chara[]={'a','e','i','o','u'};voidsolve(){cin>>n;intx=n/5,y=n%5;for(inti=0;i<5;......
  • CodeForces - 788C - 完全背包
    题目表示(x1*a[1]+x2*a[2]+...+xk*a[k])/((x1+x2+...+xk)*1000)=n/1000,可以推出(x1*a[1]+x2*a[2]+...+xk*a[k])=n*(x1+x2+...+xk),进而得出(x1*(a[1]-n)+x2*(a[2]-n)+...+xk*(a[k]-n))=0,这样处理之后就和我之前......
  • Codeforces Round 980 (Div. 2)
    CodeforcesRound980(Div.2)总结A简单小学算数题。如果\(b\lea\),直接输出\(a\)。否则,列方程\(a-x=b-2x\),\(x=b-a\),输出\(a-x\),即\(2a-b\)。#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<c......
  • [算法题解] Codeforces round 979 --D. QED's Favorite Permutation
    题目链接:https://codeforces.com/contest/2030/problem/D题目:题解:FavotitePermutation即每个数满足i=p[i];此题中,p数组每次询问后保持不变,s根据询问而改变。因此每次需要进行交换的数的位置是相同的,只需要检验s变更后,操作能否满足交换需求与否。故创建需数组need,预处理nee......