Codeforces_831

A.线性判断。

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

int n,a[105] = {0};

int main()
{
    ios::sync_with_stdio(0);
    cin >> n;
    for(int i = 1;i <= n;i++)   cin >> a[i];
    int t = 1;
    while(a[t] > a[t-1])    t++;
    while(a[t] == a[t-1])   t++;
    while(a[t] < a[t-1])    t++;
    if(t > n)    cout << "YES" << endl;
    else    cout << "NO" << endl;
    return 0;
}
View Code

B.map转换一下。

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

map<char,char> mp;
string s,s1,s2;
int main()
{
    ios::sync_with_stdio(0);
    cin >> s1 >> s2 >> s;
    for(int i = 0;i < 26;i++)
    {
        mp[s1[i]] = s2[i];
        mp[s1[i]+'A'-'a'] = s2[i]+'A'-'a';
    }
    for(int i = 0;i < s.length();i++)
    {
        if(mp.count(s[i]))  cout << mp[s[i]];
        else    cout << s[i];
    }
    cout << endl;
    return 0;
}
View Code

C.先求评委前缀和,若要某方案符合,则必须所有n个点在k个评委的评分间隔内,枚举每个评委,对应一个点,判断是否符合要求即可,注意重复的情况。

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

int n,k,a[2005],b[2005];
map<int,int> mp,ok;

int main()
{
    ios::sync_with_stdio(0);
    cin >> k >> n;
    for(int i = 1;i <= k;i++)   cin >> a[i];
    for(int i = 2;i <= k;i++)   a[i] += a[i-1];
    for(int i = 1;i <= k;i++)   mp[a[i]] = 1;
    for(int i = 1;i <= n;i++)   cin >> b[i];
    for(int i = n;i >= 1;i--)   b[i] -= b[1];
    int ans = 0;
    for(int i = 1;i <= k;i++)
    {
        int flag = 1;
        for(int j = 1;j <= n;j++)
        {
            if(!mp.count(a[i]+b[j]))
            {
                flag = 0;
                break;
            }
        }
        if(flag && !ok.count(a[i]))
        {
            ans++;
            ok[a[i]] = 1;
        }
    }
    cout << ans << endl;
    return 0;
}
View Code

D.先对人和钥匙排序,最优的结果肯定是人在钥匙中连续的某一段,暴力枚举每一段就可以了。

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

int n,k,p,a[2005],b[2005];


int main()
{
    ios::sync_with_stdio(0);
    cin >> n >> k >> p;
    for(int i = 1;i <= n;i++)   cin >> a[i];
    for(int i = 1;i <= k;i++)   cin >> b[i];
    sort(a+1,a+1+n);
    sort(b+1,b+1+k);
    int ans = INT_MAX;
    for(int i = 1;i+n-1 <= k;i++)
    {
        int maxx = 0;
        for(int j = 1;j <= n;j++)
        {
            int t = i+j-1;
            maxx = max(maxx,abs(a[j]-b[t])+abs(b[t]-p));
        }
        ans = min(ans,maxx);
    }
    cout << ans << endl;
    return 0;
}
View Code

E.给每个数字开个set,储存每个数字下标,原数组排序一下,从小到大开始操作,用树状数组计数。

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

int n,a[100005],tree[100005] = {0};
set<int> s[100005];

inline int lowbit(int x)
{
    return x&-x;
}

void update(int pos,int x)
{
    while(pos <= n)
    {
        tree[pos] += x;
        pos += lowbit(pos);
    }
}

int getsum(int pos)
{
    int sum = 0;
    while(pos > 0)
    {
        sum += tree[pos];
        pos -= lowbit(pos);
    }
    return sum;
}

int main()
{
    ios::sync_with_stdio(0);
    cin >> n;
    for(int i = 1;i <= n;i++)
    {
         cin >> a[i];
         s[a[i]].insert(i);
         update(i,1);
    }
    sort(a+1,a+1+n);
    int now = 0;
    long long ans = 0;
    for(int i = 1;i <= n;i++)
    {
        int t = a[i];
        auto it = s[t].lower_bound(now);
        if(it == s[t].end())
        {
            ans += getsum(n)-getsum(now);
            it = s[t].lower_bound(0);
            now = 0;
        }
        ans += getsum(*it)-getsum(now);
        now = *it;
        update(*it,-1);
        s[t].erase(it);
    }
    cout << ans << endl;
    return 0;
}
View Code

F.要求最大的d,使得 

 

枚举每一个可能的 中d的值,对每一个可能的值计算d再判断。

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

long long n,k,a[105],b[8000000];

int main()
{
    ios::sync_with_stdio(0);
    cin >> n >> k;
    int cnt = 0;
    long long sum = k;
    for(int i = 1;i <= n;i++)
    {
        cin >> a[i];
        sum += a[i];
        for(int j = 1;j*j <= a[i];j++)
        {
            b[++cnt] = j;
            b[++cnt] = (a[i]+j-1)/j;
        }
    }
    sort(b+1,b+1+cnt);
    cnt = unique(b,b+1+cnt)-b-1;
    long long ans = 0;
    for(int i = 1;i <= cnt;i++)
    {
        long long now = 0;
        for(int j = 1;j <= n;j++)   now += (a[j]+b[i]-1)/b[i];
        if(sum/now >= b[i])   ans = max(ans,sum/now);
    }
    cout << ans << endl;
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/zhurb/p/7224599.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值