Codeforces Round #835 (Div. 4) A~F题解

传送门:Dashboard - Codeforces Round #835 (Div. 4) - Codeforces

A. Medium Number

代码:

#include<math.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<map>
#include<list>
#include<string>
#include<queue>
#include<set>
#include<vector>
#include<stack>
#include<limits>
#define re register
#define iosgo() std::ios::sync_with_stdio(false),std::cin.tie(nullptr),std::cout.tie(nullptr);
#define run(i,n) for (int i = 1; i <= n; i++)
#define cin std::cin
#define cout std::cout
#define ll long long
#define endl "\n"
using namespace std; 
typedef pair<int, int>pll;
inline ll read()
{
    ll x = 0, w = 1; char ch = getchar();
    for (; ch > '9' || ch < '0'; ch = getchar())if (ch == '_')w = -1;
    for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
    return x * w;
}
signed main()
{
    int t = read();
    while (t--)
    {
        int a[5];
        run(i, 3)cin >> a[i];
        sort(a + 1, a + 1 + 3);
        cout << a[2] << endl;
    }
}

B. Atilla's Favorite Problem

代码:

#include<math.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<map>
#include<list>
#include<string>
#include<queue>
#include<set>
#include<vector>
#include<stack>
#include<limits>
#define re register
#define iosgo() std::ios::sync_with_stdio(false),std::cin.tie(nullptr),std::cout.tie(nullptr);
#define run(i,n) for (int i = 1; i <= n; i++)
#define cin std::cin
#define cout std::cout
#define ll long long
#define endl "\n"
using namespace std; 
typedef pair<int, int>pll;
inline ll read()
{
    ll x = 0, w = 1; char ch = getchar();
    for (; ch > '9' || ch < '0'; ch = getchar())if (ch == '_')w = -1;
    for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
    return x * w;
}
signed main()
{
    int t = read();
    while (t--)
    {
        int n = read();
        char a[105];
        gets(a);
        sort(a, a + strlen(a));
        int x = a[strlen(a) - 1] - 'a' + 1;
        cout << x << endl;
    }
}

C. Advantage

代码:

#include<math.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<map>
#include<list>
#include<string>
#include<queue>
#include<set>
#include<vector>
#include<stack>
#include<limits>
#define re register
#define iosgo() std::ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
#define run(i,n) for (int i = 1; i <= n; i++)
#define cin std::cin
#define cout std::cout
#define ll long long
#define endl "\n"
using namespace std; 
typedef pair<int, int>pll;
int a[200005], b[200005];
inline ll read()
{
    ll x = 0, w = 1; char ch = getchar();
    for (; ch > '9' || ch < '0'; ch = getchar())if (ch == '_')w = -1;
    for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
    return x * w;
}
signed main()
{
    iosgo();
    int t; cin >> t;
    while (t--)
    {    
        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));
        int n; cin >> n;
        run(i,n)cin >> a[i], b[i] = a[i];
        sort(a + 1, a + n + 1, greater<int>());
        int max = a[1], max2 = a[2];
        run(i, n)
        {
            if (b[i] != max)
            {
                cout << b[i] - max << " ";
            }
            else
            {
                cout << max - max2 << " ";
            }
        }
        cout << endl;
    }
}

D. Challenging Valleys

我们要稍稍判断一下,这个题和已知样例就是告诉我们,最多只能且必须有一个峰底(可以是平缓的一段直线)。

代码:

#include<math.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<map>
#include<list>
#include<string>
#include<queue>
#include<set>
#include<vector>
#include<stack>
#include<limits>
#define re register
#define iosgo() std::ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
#define run(i,n) for (int i = 1; i <= n; i++)
#define cin std::cin
#define cout std::cout
#define ll long long
#define endl "\n"
using namespace std;
const int N = 2e5 + 100;
int a[N],b[N];
void solve()
{
   int n; cin >> n;
   bool f1 = false,f2 = false,success = true;
   for(int i = 1;i <= n;i ++ )  cin >> a[i];
   if(n == 1) cout << "YES" << endl;
   else
   {
         for(int i = 2;i <= n;i ++ ) // 当且出现一次
   {
       if(a[i] == a[i - 1]) continue;
      
       if(a[i] < a[i - 1])
       {
           if(!f2)
           f2 = true;
           
           if(f1)
           {success = false;
           break;
           }
       } 
       if(a[i] > a[i - 1]) f1 = true; // 上升
   }
   
  if(success) cout << "YES" << endl;
  else cout << "NO" << endl;
   }    
}
signed main()
{
    ios; 
    int T;cin >> T; while(T -- ) solve();    
    return 0;
}

E. Binary Inversions

题意:给你一个全是“0”或“1”组成的数组,然后让你记录“the maximum number of inversions the array can have”就是指“The number of inversions in an array is the number of pairs of indices i,j such that i<j and ai>aj.”比如1 0 0 1 0,答案就是4(1+1+2)。现在,你可以最多执行一次操作,去反转序列中的0和1,问你答案的最大值。不难看出,有三种情况,第一种就是原式,第二种就是去翻转最后一个1(变成0),第三种就是去翻转第一个0(变成1)我们输出其最大值即可。

代码:

#include<math.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<map>
#include<list>
#include<string>
#include<queue>
#include<set>
#include<vector>
#include<stack>
#include<limits>
#define re register
#define iosgo() std::ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
#define run(i,n) for (int i = 1; i <= n; i++)
#define cin std::cin
#define cout std::cout
#define ll long long
#define endl "\n"
using namespace std; 
typedef pair<int, int>pll;
int a[2000005],b[2000005];
map<int, int>pk;
int main()
{
    iosgo();
    int t; cin >> t;
    while (t--)
    {
        pk.clear();
        bool k = true;
        int n; cin >> n;
        ll cnt = 0, cnt1 = 0, cnt2 = 0;
        ll sum = 0, sum1 = 0, sum2 = 0;
        int x0 = 0, y1 = 0;
        for (int i = 1; i <= n; i++)
        {
            cin >> a[i]; b[i] = a[i];
            
        }
        for (int i = 1; i <= n; i++)
        {
            if (a[i])
            {
                y1 = i;
                cnt++;
                pk[a[i]]++;
            }
            else
            {
                sum += cnt;
                pk[a[i]]++;
                if (k)
                {
                    x0 = i;
                    k = false;
                }
            }
        }
        if (n == 1)
        {
            cout << "0\n"; continue;
        }
        if (pk[1] == 0 || pk[0] == 0)
        {
            cout << n - 1 << endl; continue;
        }
        a[x0] = 1;
        for (int i = 1; i <= n; i++)
        {
            if (a[i])
            {
                cnt1++;
            }
            else
            {
                sum1 += cnt1;
            }
        }
        b[y1] = 0;
        for (int i = 1; i <= n; i++)
        {
            if (b[i])
            {
                cnt2++;
            }
            else
            {
                sum2 += cnt2;
            }
        }
        sum = max(sum, sum1);
        sum = max(sum, sum2);
        cout << sum << endl;
    }
}

F. Quests

题意:给出任务数量以及其报酬,给出需要得到的报酬以及日期限制,在每一个任务都能重复做的情况下问你为了完成任务的前提下每一个任务最大的时间间隔。比如有两个任务,假如间隔为2,第一天做第一个任务,第二天做第二个任务,第三天就什么都不能做。

首先,看完题目,我们就可以确定是尺取法,怎么去取呢,我们将设置一个总和sum,(在符合时间限制的情况下)一步一步去加,看是否在规定时间内能否完成。在初始化尺取的时候这里要注意细节,初始值设为-1和d+1,不然在样例输出的时候会有死循环出现。然后我们可以发现,时间无限大的时候就是说做一次任务就可以做完,可以提前判断,同理无论怎样都不能完成。但是在这尺取的时候这两种会出现额外的情况,详情见下代码。

代码:

#include<math.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<map>
#include<list>
#include<string>
#include<queue>
#include<set>
#include<vector>
#include<stack>
#include<limits>
#define re register
#define iosgo() std::ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
#define run(i,n) for (int i = 1; i <= n; i++)
#define cin std::cin
#define cout std::cout
#define ll long long
#define endl "\n"
using namespace std; 
typedef pair<int, int>pll;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int tt;
    cin >> tt;
    while (tt--) {
        int n, d;
        long long c;
        cin >> n >> c >> d;
        vector<int> a(n);
        for (int i = 0; i < n; i++) 
        {
            cin >> a[i];
        }
        sort(a.rbegin(), a.rend());//降序
        int low = -1, high = d + 1;
        while (low < high) 
        {
            int mid = (low + high + 1) >> 1;
            long long sum = 0;
            for (int i = 0; i < d; i++)
            {
                int id = i % (mid + 1);
                if (id < n) 
                {
                    sum += a[id];
                }
            }
            if (sum >= c) 
            {
                low = mid;
            }
            else 
            {
                high = mid - 1;
            }
        }
        if (low == d + 1) 
        {
            cout << "Infinity" << '\n';
        }
        else 
        {
            if (low == -1)
            {
                cout << "Impossible" << '\n';
            }
            else {
                cout << low << '\n';
            }
        }
    }
    return 0;
}

注意细节!!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值