Codeforces Round #727 (Div. 2)

A. Contest Start

思路:

t 为比赛持续时间 x为每个选手的开始间隔时间

首先先考虑分情况讨论

(1)如果说 x > t 那么就是没有任何冲突的 ans=0

(2)如果说 x ==  t  那么除了最后一个人没有冲突 其余人都有冲突 ans=n-1

(3)如果说 x < t 那么肯定是有冲突的 每个人冲突的个数就是\left \lfloor t/x \right \rfloor 

在第三种情况上再进行讨论 前面一部分人的数量确实是\left \lfloor t/x \right \rfloor 但是到后面了就会逐渐减1,这是显然的 因为在后面的几个人里面后面没有继续加入比赛了 那么当前的人的不满意程度肯定会减少

那么这个分界线是什么呢

分界线应该是\left \lfloor t/x \right \rfloor

如果 n  <=  \left \lfloor t/x \right \rfloor    ans =(n∗(n−1) )​/ 2

n > \left \lfloor t/x \right \rfloor ans = ( n - \left \lfloor t/x \right \rfloor ) * \left \lfloor t/x \right \rfloor + (\left \lfloor t/x \right \rfloor *( \left \lfloor t/x \right \rfloor - 1 ) ) / 2  

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll n,x,t;
        cin>>n>>x>>t;
        ll k=t/x;
        if(x==t)
            cout<<n-1<<endl;
        else if(x>t)
            cout<<0<<endl;
        if(x<t)
        {
            if(n<=k)
                cout<<n*(n-1)/2<<endl;
            else
                cout<<(n-k)*k+k*(k-1)/2<<endl;
        }
    }
    return 0;
}

B. Love Song

思路:简单题 做一遍前缀和就好了

#include <bits/stdc++.h>
using namespace std;
const int N=100100;
char g[N];
int s[N];
int main()
{
    int n,k;
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        cin>>g[i];
    }
    for(int i=1;i<=n;i++)
    {
        s[i]=s[i-1]+g[i]-'a'+1;
    }
    while(k--)
    {
        int l,r;
        cin>>l>>r;
        cout<<s[r]-s[l-1]<<endl;
    }
    return 0;
}

C. Stable Groups

思路:排序后去维护没加人进来的总组数 然后同时维护两组之间的差 然后再将差排序 从最小的开始加人 就可以了

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=200100;
int a[N];
int n,k,x;
int d[N];
signed main()
{
    cin>>n>>k>>x;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    sort(a+1,a+1+n);
    int cnt=0;
    for(int i=1,j=i+1;i<n;)
    {
        while(a[j]-a[i]<=x&&j<n)
        {
            j++;
            i++;
        }
        d[cnt++]=a[j]-a[i];
        i=j;
        j=i+1;
    }
    sort(d,d+cnt);
    /*for(int i=0;i<cnt;i++)
        cout<<d[i]<<endl;*/
    int res=cnt;
    for(int i=0;i<cnt;i++)
    {
        if(k>=(d[i]/x)+(d[i]%x!=0)-1)
        {
            k-=(d[i]/x+(d[i]%x!=0)-1);
            res--;
        }
        else break;
    }
    cout<<res+1<<endl;
    return 0;
}

D. PriceFixed

#include <algorithm>
#include <queue>
#include <iostream>
using namespace std;
typedef long long LL;
struct node{
    LL a, b;
    bool operator < (const node& o) const{
        return b < o.b;
    }
}p[200100];
LL c[200100];
int main()
{
    int n; cin >> n;
    for(int i = 1; i <= n; i++) scanf("%lld %lld",&p[i].a,&p[i].b);
    sort(p+1, p+1+n);
    int r = n, l = 1;
    LL sum = 0, cost = 0;
    while(l <= r)
    {
        // for(int i = 1; i <= n; i++) cout << c[i] << " "; cout << cost << ":" << sum << endl;
        if(sum >= p[l].b)
        {
            // printf("%lld %lld %lld\n",sum, p[l].a, c[l]);
            cost += p[l].a - c[l];
            sum += p[l].a - c[l];
            c[l] = p[l].a;
            l++;
            continue;
        }
        else
        {
            LL sub = min(p[r].a-c[r], p[l].b-sum);
            if(sub == 0) { r--; continue; }
            c[r] += sub;
            cost += 2*sub;
            sum += sub;
            if(p[r].a - c[r] == 0) r--; 
            continue;
        }
    }
    // for(int i = 1; i <= n; i++) cout << c[i] << " "; cout << endl;
    cout << cost << endl;
    // system("pause");
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值