7.15总结

· · 闲话

T1 P3918 [国家集训队] 特技飞行

贪心题

思路:因为某次动作的价值为 ( 距上次该动作的时间 ) × c_i ,所以我们要尽量把刺激度最大的放在 1 和 n 这里。

跟着思路写就行了

Code:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=1e3+10;
int n,k,a[N],l,r,ans,b[N];
bool cmp(int x,int y){return x>y;}
int main(){
    ios::sync_with_stdio(0);
    cin>>n>>k;
    for(int i=1;i<=k;i++)
        cin>>a[i];
    sort(a+1,a+k+1,cmp);
    l=1,r=n;
    while(l<=r&&l<=k){
        ans+=(r-l)*a[l];
        l++;
        r--;
    }
    cout<<ans;
    return 0;
}

T2 P1109 学生分组

小于 l 就加到 l ,大于 r 就减到 r

同时 ans+=(l-a_i||a_i-r)

Code:

#include <bits/stdc++.h>
#define ll long long
#define endl '\n' 
using namespace std;
int n,a[105],l,r,sum,x,y;
int main(){
    ios::sync_with_stdio(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        sum+=a[i];
    }
    cin>>l>>r;
    if(sum<l*n||sum>r*n){
        cout<<-1;
        return 0;
    }
    for(int i=1;i<=n;i++){
        if(a[i]<l)x+=l-a[i];
        if(a[i]>r)y+=a[i]-r;
    }
    cout<<max(x,y);
    return 0;
}

T3 P1843 奶牛晒衣服

牛成精了?

显然把烘干机用在湿度最大的衣服上最好,但是直接暴力会超时。。

可以用二分枚举可能的答案,输出最大的就行了

check要写好哦

Code:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,a,b,w[500010],maxx;
bool cmp(int x,int y){
    return x>y;
}
bool check(int x){
    int s=x,res=0;
    for(int i=1;i<=n;i++){
        if(w[i]-a*x<1)continue;
        else{
            res=ceil((w[i]-a*x)*1.0/b);
            if(res>s)return 0;
            s-=res;
        }
    }
    return 1;
}
signed main(){
    cin>>n>>a>>b;
    for(int i=1;i<=n;i++){
        cin>>w[i];
        maxx=max(maxx,w[i]);
    }
    sort(w+1,w+n+1,cmp);
    int l=0,r=maxx+1;
    while(l+1<r){
        int mid=(l+r)/2;
        if(check(mid))r=mid;
        else l=mid;
    }
    cout<<r;
    return 0;
}
\Huge Bye\sim