首页 > 其他分享 >Educational Codeforces Round 152 A~D

Educational Codeforces Round 152 A~D

时间:2023-07-28 18:57:55浏览次数:42  
标签:Educational 152 const cout int cin Codeforces solve define

A

#include <bits/stdc++.h>
#define endl '\n'
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

typedef pair<int, int> PII;
const int N = 2e5 + 10;
const int MOD = 1e9 + 7;
int T;
void solve()
{
  int b, c, h;
  cin >> b >> c >> h;
  if (c + h < b - 1)
  {
    cout << 2 * (c + h) + 1 << endl;
  }
  if (c + h >= b - 1)
  {
    cout << 2 * (b - 1) + 1 << endl;
  }
}
signed main()
{
  ios;
  int T = 1;
  cin >> T;
  while (T--)
  {
    solve();
  }
}

B

#include <bits/stdc++.h>
#define endl '\n'
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
#define int long long
typedef pair<int, int> PII;
#define int long long
const int N = 3e5 + 10;
const int MOD = 1e9 + 7;
int T;
int n, k;
#define int long long
typedef pair<int, int> PII;

bool cmp(PII a, PII b)
{
  if (a.first != b.first)
    return a.first > b.first;
  else
    return a.second < b.second;
}
void solve()
{
  cin >> n >> k;
  vector<PII> a;
  for (int i = 1; i <= n; i++)
  {
    int x;
    cin >> x;
    if (x > k)
    {
      if (x % k == 0)
        a.push_back({k, i});

      else
        a.push_back({x % k, i});
    }
    else
      a.push_back({x, i});
  }
  sort(a.begin(), a.end(), cmp);
  for (auto x : a)
  {
    cout << x.second << ' ';
  }
  // for (int i = 1; i <= n; i++)
  // {
  //   cout << a[i].second << ' ';
  // }
  cout << endl;
}
signed main()
{
  ios;
  int T = 1;
  cin >> T;
  while (T--)
  {
    solve();
  }
}

C

#include <bits/stdc++.h>
#define endl '\n'
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

typedef pair<int, int> PII;
const int N = 2e5 + 10;
const int MOD = 1e9 + 7;
int T;

int n, m;
char str[N];
int l[N], r[N];

void solve()
{
  cin >> n >> m >> str + 1;

  int last = 0;
  for (int i = 1; i <= n; i++)
  {
    if (str[i] == '0')
      last = i;
    l[i] = last;
  }

  last = n + 1;
  for (int i = n; i; i--)
  {
    if (str[i] == '1')
      last = i;
    r[i] = last;
  }

  set<PII> S;
  while (m--)
  {
    int x, y;
    cin >> x >> y;
    if (r[x] > y || l[y] < x) //这是每个询问右端点前面最近的0都在区间左边(区间里全为1)或者最近离左端点后面的1在区间右边(区间全为0)这种情况不会改变区间,也就是原来状态
      S.insert({0, 0});
    else
    {
      x = r[x], y = l[y];
      if (x > y)
        S.insert({0, 0}); //当为类似000111情况时,排序也没有变化序列,仍为原序列
      else
        S.insert({x, y}); //每一个区间的排序都和l[y]和r[x]的组合 一 一对应
    }
  }

  cout << S.size() << endl; // set的size就是我们排序的所有的
}

int main()
{
  ios;
  T = 1;
  cin >> T;
  while (T--)
    solve();
  return 0;
}

D

 先将2的向两遍扩展再将1向一边扩展(尽量向有1的地方扩展)每一次扩展完成需要一代价,剩下的只能花费代价

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;

int a[N], vis[N]; // vis数组判断是否被扩展过

inline void solve()
{
  int n, ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
  {
    if (a[i] == 0)
      continue;
    int j = i, flag = 0;
    while (j <= n && a[j] != 0)
    {
      if (a[j] == 2)
        flag = 1;
      j++;
    }
    if (flag)
    {
      vis[i - 1] = vis[j] = 1; //当有数为2时,我们将第一个2的左边和类似21121这类不含0的连续串的右边都扩展;
    }
    else
    {
      if (i - 1 >= 1 && !vis[i - 1]) //当该数为1时,若前面有未扩展过的1,我们向前面的1扩展,否则我们向后面扩展
        vis[i - 1] = 1;
      else
        vis[j] = 1;
    }
    ans++;
    i = j - 1;
  }
  for (int i = 1; i <= n; i++)
    if (a[i] == 0 && !vis[i])
      ans++;
  cout << ans << endl;
}

signed main()
{
  ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  int T = 1;
  // cin >> T;
  while (T--)
    solve();
}

 

标签:Educational,152,const,cout,int,cin,Codeforces,solve,define
From: https://www.cnblogs.com/xumaolin/p/17587267.html

相关文章

  • Codeforces Round 888 (Div. 3) 题解
    考场上\(7\)题做出来\(4\)题,最后几分钟才把D题调出来,但还是吃了不少罚时A.EscalatorConversations\(O(n)\)枚举即可,对于每个人计算需要的间隔台阶数是否在\((0,m)\)以内以及相差高度是否是\(k\)的倍数B.ParitySort显然,偶数和奇数是不可能产生交换操作的,而偶数......
  • Educational Codeforces Round 1
    EducationalCodeforcesRound1A.TrickySumintfac[N],p2[N];voidinit(){fac[0]=1;p2[0]=1;for(inti=1;i<=33;i++){fac[i]=fac[i-1]*2;p2[i]=p2[i-1]+fac[i];}}voidsolve(){intn=read();intsum=(1+n)*n/2;co......
  • CodeForces 1268E Happy Cactus
    洛谷传送门AtCoder传送门考虑一些简单的情况,比如树。设\(f_u\)为当前\(u\)能通过边权递增的路径到达的点数(包括它自己)。为了让两个点对在边权递增路径的边权最小的那条边被统计,我们倒序枚举边。当枚举到\((u,v)\)时,我们有\(f_u=f_v=f_u+f_v\)。这是因为\(u\)......
  • Codeforces Round 888 (Div. 3)记录
    A.EscalatorConversations#include<cstdio>#include<algorithm>#include<cmath>#include<vector>#include<string.h>#include<set>#include<string>#include<map>#include<iostream>#include<queue>......
  • Codeforces Round 618 (Div. 2)
    CodeforcesRound618(Div.2)https://codeforces.com/contest/1300A.Non-zero要求和,积都不为0,则先把全部0操作一次,然后再check和是否为0,是的话再对任意数操作一次即可。#include<bits/stdc++.h>usingnamespacestd;constintN=105;intn,x,s,ans;voidsolve......
  • Codeforces Round 888 (Div. 3) A-F
    A.EscalatorConversations题意:有一个扶梯,有n个人要站扶梯,这个扶梯有m个位置,第i个位置的高度为i*k,Vlad高H,第i个人高h[i],当且仅当两个人所处的位置高度加上自身身高刚好相同时才能谈话,问能和Vlad谈话的有多少人。Solution直接计算即可voidsolve(){ intn,m,k,H;cin>>n>>m>>......
  • Codeforces Round 888 (Div. 3) - D
    目录D.PrefixPermutationSumsCodeforcesRound888(Div.3)赛后摘记D.PrefixPermutationSums题意判断给定的长为n-1数组,是否为某个1~n的序列的前缀和数组漏了一个数形成的数组思路就是判断能否变回去,毫无感情的判断机器法一:统计给定前缀和数组的差分数组得......
  • Codeforces Round 888 (Div. 3)
    A.EscalatorConversations#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongvoidsolve(){intn,m,k,H;cin>>n>>m>>k>>H;vector<int>h(n);intres=0;for(inth,......
  • Codeforces Round 888 (Div. 3)F(异或小技巧)
    题意:给你一个数组长度为n的a数组,要求a数组的值为非负整数,再给你一个k,a的值全小于2的k次方,找到一个小于a的k次方的值x,再从a中找到两个值,让他们(ai⊕x)&(aj⊕x)最小结论:n个数的最小异或对的答案就是排序后最小的相邻异或和思路:(ai⊕x)&(aj⊕x)的最高位为1,可以把它先转换成二进制......
  • 《安富莱嵌入式周报》第318期:无线电扫描仪,高精度功耗分析仪,单片机JavaScript引擎,平头
    周报汇总地址:http://www.armbbs.cn/forum.php?mod=forumdisplay&fid=12&filter=typeid&typeid=104 【实战技能视频】基于硬件垂直消隐的多缓冲技术在LVGL,emWin,GUIX和TouchGFX应用https://www.armbbs.cn/forum.php?mod=viewthread&tid=120114视频版:https://www.bilibili.......