当前位置:   article > 正文

Codeforces Round #703 (Div. 2)

codeforces round #703

题目链接
Here

A题

因为是任意次数加,所以可以 0 , 1 , 2 , 3 … 0,1,2,3\dots 0123
具体看代码

  • 代码
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <unordered_map>

using namespace std;
typedef long long ll;
#define PII pair<int, int>
#define x first
#define y second
#define PB push_back

const int N = 110, M = 100;
ll a[N];
void solve()
{
     int n;
     cin>>n;
     for(int i=1;i<=n;i++)cin>>a[i];
     for(int i=1;i<n;i++){
         if(a[i]!=0&&a[i]>=i-1){
             ll x=a[i];
             a[i]=i-1;
             a[i+1]+=(x-i+1);
         }
     }
     bool flag=1;
     for(int i=1;i<n;i++){
         if(a[i]>=a[i+1]){
             flag=0;
             break;
         }
     }
     if(!flag)cout<<"NO"<<endl;
     else cout<<"YES"<<endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        solve();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

B题

  • 题意
  • 在这里插入图片描述

二维平面上能找到多少个点,使得所有点距离 ∣ x i − x j ∣ + ∣ y i − y j ∣ |x_i-x_j|+|y_i-y_j| xixj+yiyj这些点距离最短。

  • 思路
    在这里插入图片描述

  • 代码

#include <bits/stdc++.h>
using namespace std;
 
long long solve(vector<int> x) {
    sort(x.begin(), x.end());
    return x[x.size() / 2] - x[(x.size() - 1) / 2] + 1;
}
 
void solve() {
    int n;
    cin >> n;
    vector<int> x(n), y(n);
    for (int i = 0; i < n; ++i)
        cin >> x[i] >> y[i];
    cout << solve(x) * solve(y) << '\n';
}
 
int main() {
    int t;
    cin >> t;
    while (t--) solve();
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

C1、C2 题

  • 题意
    交互式问题,每次询问一个区间,会返回第二小的位置。问你能够在 40   20 40~20 40 20次查询中找到n的位置。
easy version

二分区间,具体看代码吧。

  • 代码
#include <bits/stdc++.h>
using namespace std;
 
int ask(int l, int r) 
{
    cout << "? " << l << ' ' << r << endl;
    int ans;
    cin >> ans;
    return ans ;
}
 
int main() 
{
    int n;
    cin >> n;
    int l = 1, r = n;
    while (r - l > 1) {//保证区间长度大于等于2.
        int m = (l + r+1) / 2;
        int smax = ask(l, r );
        if (smax < m) {//如果第二小值在左边。
            if (ask(l, m ) == smax) {//第二小值和最大值在一边。
                r = m;
            } else {
                l = m;
            }
        } else {//第二小值在左边。
            if (ask(m, r ) == smax) {//第二小值和最大值在一边。
                l = m;
            } else {
                r = m;
            }
        }
    }
    int x=ask(l,r);
    cout<<"! ";
    if(x==r)
    cout  << l << endl;
    else cout<<r<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
hard version
  • 思路
    在这里插入图片描述
  • 代码
#include <bits/stdc++.h>
using namespace std;

int ask(int l, int r) {
    if (l >= r) return -1;
    cout << "? " << l + 1 << ' ' << r + 1 << endl;
    int ans;
    cin >> ans;
    return ans - 1;
}

int main() {
    int n;
    cin >> n;
    int smax = ask(0, n - 1);
    if (smax == 0 || ask(0, smax) != smax) {
        int l = smax, r = n - 1;
        while (r - l > 1) {
            int m = (l + r) / 2;
            if (ask(smax, m) == smax) {
                r = m;
            } else {
                l = m;
            }
        }
        cout << "! " << r + 1 << endl;
    } else {
        int l = 0, r = smax;
        while (r - l > 1) {
            int m = (l + r) / 2;
            if (ask(m, smax) == smax) {
                l = m;
            } else {
                r = m;
            }
        }
        cout << "! " << l + 1 << endl;
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

D题

  • 题意
    在这里插入图片描述
  • 思路
    首先可以二分check,check:大于x的标记为1,小于x的标记为-1;然后查找k长度区间sum是否大于0,大于0就可以增加x,否则减小x
  • 代码
#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (auto &i : a) cin >> i;
    int l = 1, r = n + 1;
    while (r - l > 1) {
        int m = (l + r) / 2;
        vector<int> b(n);
        for (int i = 0; i < n; ++i)
            if (a[i] >= m) {
                b[i] = 1;
            } else {
                b[i] = -1;
            }
        for (int i = 1; i < n; ++i) b[i] += b[i - 1];
        int mx = b[k - 1];
        int mn = 0;
        for (int i = k; i < n; ++i) {
            mn = min(mn, b[i - k]);
            mx = max(mx, b[i] - mn);//这一步就是查找是否有k长度的区间满足sum>0
        }
        if (mx > 0) {
            l = m;
        } else {
            r = m;
        }
    }
    cout << l << '\n';
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号