Codeforces Round #702 (Div. 3)

比赛链接:https://codeforces.com/contest/1490

  • 这次比赛好像全是循环的题,有点意思。

A题:

  • 让你添加数字,使得相邻的两个数字小于等于两倍
#include <bits/stdc++.h>
using namespace std;

const int N = 50;

int a[N];

int main()
{
    int t;
    cin >> t;
    
    while(t --)
    {
        int n;
        cin >> n;
        for(int i = 0;i < n;i ++) cin >> a[i];
        int cnt = 0;
        for(int i = 0;i < n - 1;i ++)
        {
      //  cout << i << ' ' << a[i] << ' ' << cnt << endl;
           if(a[i + 1] >= a[i]){
               if((a[i + 1] + a[i] - 1) / a[i] <= 2) continue;
               a[i] *= 2;
               cnt ++;
               i --;
           }else{
               if((a[i] + a[i + 1] - 1) / a[i + 1] <= 2) continue;
            //   a[i] /= 2;
               if(a[i] & 1){
                   a[i] = a[i] / 2 + 1; 
               }else{
                   a[i] = a[i] / 2;
               }
               cnt ++;
               i --;
           }
            
        }
        cout << cnt << endl;
    }
    
    return 0;
}

B题:

  • 让余数等于0的等于余数为1,并且等于余数为2的。其实就只需要统计出等于0的数字个数,等于1的数字个数,等于2的数字个数。如果0的多余,那么就转移给1,如果1的多余转移给2,如果2多余,就转移给0
#include <bits/stdc++.h>
using namespace std;
 
const int N = 1e6 + 10;
 
int a[N];
 
int main()
{
    int t;
    cin >> t;
    
    while(t --)
    {
        int n;
        cin >> n;
        a[0] = 0,a[1] = 0,a[2] = 0;
        
        for(int i = 0,x;i < n;i ++){
            cin >> x;
            a[x % 3] ++;
        }
        
        int ave = n / 3;
        int cnt = 0;
        
        for(int i = 0;i < 10;i ++){
            if(a[2] > ave){
                cnt += a[2] - ave;
                a[0] += a[2] - ave;
                a[2] = ave;
            }
            if(a[0] > ave){
                cnt += a[0] - ave;
                a[1] += a[0] - ave;
                a[0] = ave;
            }
            if(a[1] > ave){
                cnt += a[1] - ave;
                a[2] += a[1] - ave;
                a[1] = ave;
            }
        }
        cout << cnt << endl;
    }
    
    return 0;
}

C题:

  • 打表题
#include <bits/stdc++.h>
using namespace std;
 
const int N = 1e6 + 10;
 
typedef long long LL;
 
int main()
{
    int t;
    cin >> t;
    
    unordered_set<LL> st;
    
    for(LL i = 1;i <= 10000;i ++){
        st.insert(i * i * i);
    }
    
    
    while(t --)
    {
        LL n;
        cin >> n;
        bool flag = false;
        for(LL i = 1;i <= 10000;i ++){
            LL b = n - i * i * i;
            if(st.count(b)){
                flag = true;
            }
        }
        
        if(flag){
            cout << "YES" << endl;
        }else{
            cout << "NO" << endl;
        }
        
    }
    
    return 0;
}

D题:

  • 模拟一下建树
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int t,n;
int a[N];
int deep[N];

void build(int l,int r,int dep)
{
    if(l == r){
        return;
    }
    int ml = l;
    for(int i = l;i < r;i ++){
        if(a[i] > a[ml]){
            ml = i;
        }
    }
  //  cout << ml << ' ' << a[ml] << endl;
    deep[a[ml]] = dep + 1;
    build(l,ml,dep + 1);
    build(ml + 1,r,dep + 1);
}


int main()
{
    cin >> t;
    while(t --)
    {
        memset(deep,0,sizeof deep);
        
        cin >> n;
        int maxn = 1;
        for(int i = 1;i <= n;i ++)
        {
            cin >> a[i];
            if(a[i] >= a[maxn]){
                maxn = i;
            }
        }
        deep[maxn] = 0;
        
        build(1,maxn,0);
        build(maxn + 1,n + 1,0);
        
        for(int i = 1;i <= n;i ++){
            cout << deep[a[i]] << ' ';
        }
        
        cout << endl;
    }
    
    return 0;
}

E题

  • 二分题,大于等于的就可以把比他小的吃掉,而且还能获得他拥有的物品
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int t,n;
typedef long long LL;
LL sum[N];
int a[N];
int b[N];
bool check(int x)
{
    long long ans = sum[x];
    for(int i = x + 1;i <= n;i ++){
        if(ans >= a[i]){
            ans += a[i];
        }else{
            return false;
        }
    }
    return true;
}

int main()
{
    cin >> t;
    while(t --)
    {
        cin >> n;
        for(int i = 1;i <= n;i ++){
            cin >> a[i];
            b[i] = a[i];
        }
        
        sort(a + 1,a + n + 1);
        
        for(int i = 1;i <= n;i ++){
            sum[i] = sum[i - 1] + a[i];
            // b[i] = a[i];
        }
        
        int l = 1,r = n;
        
        while(l < r)
        {
            int mid = l + r >> 1;
            if(check(mid)){
                r = mid;
            }else{
                l = mid + 1;
            }
        }
        
        cout << n - l + 1 << endl;
        
        for(int i = 1;i <= n;i ++){
            if(b[i] >= sum[l] - sum[l - 1])
                cout << i << ' ';
        }
        cout << endl;
    }
    
    return 0;
}

F题

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 1e6 + 10;
int a[N];
int sum[N];

int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t -- )
    {
        int n;
        cin >> n;
        map<int,int>mp;
        
        for(int i = 0,x;i < n;i ++){
            cin >> x;
            mp[x] ++;
            a[i] = 0;
        }
        a[n] = 0;
        for(auto &x : mp){
            a[x.second] ++; 
        }
        
        vector<pair<int,int>> v;
        
        
        int ans = n;
        for(int i = 1;i <= n;i ++){
            if(a[i] == 0) continue;
            v.push_back({i,a[i]});
        }
        
        sum[0] = v[0].second;
        
        for(int i = 1;i < v.size();i ++){
            sum[i] = sum[i - 1] + v[i].second;
        }
        
        int sum1 = 0;
        for(int i = 0;i < v.size();i ++){
            
            int t1 = (sum[v.size() - 1] - sum[i]) * v[i].first;
            int t2 = n - sum1 - v[i].second * v[i].first - t1;
            
            ans = min(ans,t2 + sum1);
            // cout << ans << endl;
            sum1 += v[i].second * v[i].first;
        }
        cout << ans << endl;   
    }
    return 0;
}

/*
1 1
2 2
3 1

*/

G题

  • 二分 + 前缀和.前缀和是为了计算出
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL N = 1e6 + 10;
LL a[N];
LL x[N];
LL sum[N];
LL b[N];
LL t,n,m;
vector<pair<LL,LL>> v;
LL mmax;
long long dfs(LL i)
{
    int cur = x[i];
    if(sum[n] <= 0 && mmax < x[i]){
        return -1;
    }
    LL f1,f2 = 0;
    if(mmax >= x[i]){
        f1 = 0;
        f2 = x[i];
    }else{
        f1 = (x[i]-mmax-1)/sum[n]+1; // 这个地方是个重点,要下取整,而且同时避免是倍数的时候
        f2 = x[i] - sum[n] * f1;
    }
    
    
    LL l = 0,r = n - 1;
    while(l < r){
        LL mid = l + r >> 1;
        if(v[mid].second < f2){
            l = mid + 1;
        }else{
            r = mid;
        }
    }
    
    return f1 * n + v[l].first - 1;
    
}

int main()
{
    
    cin >> t;
    while(t --)
    {
        cin >> n >> m;
        v.clear();
        for(LL i = 1;i <= n;i ++)
            cin >> a[i];
        for(LL i = 1;i <= m;i ++)
            cin >> x[i];
        
        map<LL,LL>mp;
        mmax = -1e10;
        
        for(LL i = 1;i <= n;i ++){
            sum[i] = sum[i - 1] + a[i];
            mmax = max(sum[i],mmax);
            v.push_back({i,mmax});
            
        }

        sort(v.begin(),v.end(),[](pair<LL,LL>f1,pair<LL,LL>f2){
            if(f1.second == f2.second){
                return f1.first < f2.first;
            }else{
                return f1.second < f2.second;
            }
        });
        
        for(LL i = 1;i <= m;i ++){
            cout << dfs(i) << ' ';
        }
        cout << endl;
    }
}
 
知足常乐!
原文地址:https://www.cnblogs.com/yjsh/p/14412169.html