首页 > 代码库 > Codeforces Round #415 (Div. 2)

Codeforces Round #415 (Div. 2)

Straight <<A>>

暴力模拟一下。

技术分享
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

int main()
{
    int n,k;
    cin>>n>>k;
    double sum = 0;
    for(int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        sum += x;
    }
    double av = sum/n;
    int i = 0;
    for(;;)
    {
        if(round(av)==k)
        {
            cout<<i<<endl;
            break;
        }
        i++;
        sum += k;
        av = sum/(i+n);
    }
    return 0;
}
Code

Summer sell-off

按 min(ki*2,li)-ki 来从大到小来排序。

技术分享
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 1e5+5;
typedef long long ll;
struct block
{
    ll k,l;
    ll d;
    ll diff;
};
block e[maxn];
bool cmp(block A,block B)
{
    return A.diff>B.diff;
}
int main()
{
    int n,f;
    cin>>n>>f;
    for(int i=0;i<n;i++)
    {
        cin>>e[i].k>>e[i].l;
        e[i].d = e[i].k*2;
        e[i].diff = min(e[i].d,e[i].l)-e[i].k;
    }
    sort(e,e+n,cmp);
    ll sum = 0;
    int count1 = 0;
    for(int i=0;i<n;i++)
    {
        if(e[i].diff>0&&count1<f)
        {
            sum += min(e[i].d,e[i].l);
            count1++;
        }
        else
        {
            sum += min(e[i].k,e[i].l);
        }
    }
    cout<<sum<<endl;
    return 0;
}
Code

Do you want a date?

这个题稍微想了会。 想把所有数从大到小排一下。考虑每一个元素对于整体的贡献率,对于xi,它的贡献率是2^(n-i-1)-(2^i)。然后就是快速幂了。

技术分享
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 3e5+5;
typedef long long ll;
const ll mod = 1e9+7;
ll ans[maxn];
ll quick_pow(ll a,ll n)
{
    ll res = 1;
    while(n)
    {
        if(n%2) res = res*a%mod;
        n /= 2;
        a = a*a%mod;
    }
    return res;
}
ll getsum(ll i,ll n,ll v)
{
    ll a = quick_pow(2,n-i-1)%mod;
    ll b = quick_pow(2,i)%mod;
    ll sum = 0;
    if(n-i-1>=i)
    {
        sum = ((a+mod)-b)%mod;
    }
    else
    {
        sum = (a-b-mod)%mod;
    }
    return (sum*v)%mod;
}
bool cmp(ll A,ll B)
{
    return A>B;
}
int main()
{
    int n;
    cin>>n;
    ll sum = 0;
    for(int i=0;i<n;i++)
    {
        cin>>ans[i];
    }
    sort(ans,ans+n,cmp);
    for(int i=0;i<n;i++)
    {
        sum = (sum + mod + getsum(i,n,ans[i]))%mod;
    }
    cout<<sum<<endl;
    return 0;
}
Code

Glad to see you!

Find a car

Codeforces Round #415 (Div. 2)