[CF sets of questions] CF-1201

CF-1201

Portal

# = * A 500 B 1000 C 1500 D 2000 E1 2000 E2 1000
1 (2217) 1672 482 00:09 400 01:40 790 01:25 Have been added

A

Looking after the largest deposit into

Beginning the variable wrong, delayed a few minutes

#include <bits/stdc++.h>
using namespace std;
int n,m;
char s[1010];
int c[1010][5],a[1010];
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        scanf("%s",s+1);
        for(int j=1;j<=m;j++){
            c[j][s[j]-'A']++;
        }
    }
    for(int i=1;i<=m;i++)cin>>a[i];
    int res = 0;
    for(int i=1;i<=m;i++){
        int mx = 0;
        for(int j=0;j<5;j++){
            mx = max(mx,c[i][j]);
        }
        res += mx * a[i];
    }
    cout<<res<<endl;
    return 0;
}

B

First of all the numbers and get an even number, followed by half the number of all must be guaranteed to be less than the sum of

int n;
int a[100010];
int main() 
{
    cin>>n;
    ll sum = 0;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        sum += a[i];
    }
    if(sum & 1){
        puts("NO");return 0;
    }
    for(int i=1;i<=n;i++){
        if(2ll * a[i] > sum){
            puts("NO");return 0;
        }
    }
    puts("YES");
    return 0;
}

C

Be greedy think about, so that every time you want to increase the median, we must make the same number and the median size figures behind the increase in the median together. For example, 2222223, followed by three 2 must be added together, will ensure an increase in the median. So this can be increased to every length and height down and sweep it again

const int N = 200010;
ll n,k;
ll a[N];//注意开 ll 
int main() 
{
    cin>>n>>k;
    for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
    if(n == 1){
        printf("%lld\n",a[1] + k);
        return 0;
    }
    sort(a+1,a+1+n);
    int p = n/2 + 1;
    vector<pair<int,int> >v;
    for(int i=p+1;i<=n;i++){
        if(a[i] != a[i-1]){
            v.push_back({i-p,a[i] - a[i-1]});
        }
    }
    v.push_back({p,k});
    ll res = 0;
    for(auto x : v){
        ll l = x.first;
        ll h = x.second;
        if(k < l)break;
        if(l * h <= k){
            k -= l * h;
            res += h;
        }else{
            res += k / l;
            break;
        }
    }
    cout<<res + a[p]<<endl;
    return 0;
}

D

\ (L [i] \) the number of columns representing the i-th row of the left-most treasure, \ (R & lt [i] \) represents the number of the rightmost column of the i-th row treasures. The line as a stage, when this line scanning is finished, certainly in the leftmost or rightmost, then pick a recent secure way to the top of a walk (it is conceivable that this is the best, if in this line to walk go through a few steps, then it is not the nearest road, but may be wasted). May be an array \ (dp [i] [0 ] \) represents the leftmost minimum cost, the same way at the end of the i-th row \ (dp [i] [1 ] \) minimum represents the last rightmost spending, while moving down the floor, half looking for the nearest door to violence transfer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+10;
ll p[N],dp[N][2],a[N][2];//a[i][0]表示i行最左边
ll n,m,q,k;
// [j][v] -> [i][u]
ll calc(ll j,ll v,ll i,ll u){
    ll res = 1ll << 60;
    int k = lower_bound(p+1,p+q+1,a[j][v]) - p;//找右边最近的
    if(k <= q){
        res = abs(a[j][v]-p[k]) + abs(a[i][u^1] - p[k]) + abs(a[i][u] - a[i][u ^ 1]);
    }
    k = lower_bound(p+1,p+q+1,a[j][v]) - p - 1;//找左边最近的
    if(k){
        res = min(res,abs(a[j][v]-p[k]) + abs(a[i][u^1] - p[k]) + abs(a[i][u] - a[i][u ^ 1]));
    }
    return res;
}
int main(){
    scanf("%lld%lld%lld%lld",&n,&m,&k,&q);
    for(int i=1;i<=n;i++){
        a[i][0] = 1e9;a[i][1] = -1e9;
        dp[i][0] = dp[i][1] = 1ll<<60;
    }
    for(int i=1;i<=k;i++){
        ll x,y;scanf("%lld%lld",&x,&y);
        a[x][0] = min(a[x][0],y);
        a[x][1] = max(a[x][1],y);
    }
    a[1][0] = 1,a[1][1] = max(a[1][1],1ll);//第一行特殊处理
    for(int i=1;i<=q;i++)
        scanf("%lld",&p[i]);
    sort(p+1,p+1+q);
    dp[1][0] = abs(a[1][1] - 1) + abs(a[1][1] - a[1][0]);
    dp[1][1] = abs(a[1][1] - 1);
    int j = 1;
    for(int i=2;i<=n;i++){
        if(a[i][0] == 1e9)continue;//这一行没宝藏
        for(int u=0;u<2;u++){
            for(int v=0;v<2;v++){
                dp[i][u] = min(dp[i][u],dp[j][v] + i - j + calc(j,v,i,u));
            }
        }
        j = i;
    }
    cout << min(dp[j][0],dp[j][1])<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/1625--H/p/11356173.html