7.18 总结

· · 个人记录

T1 P1094 [NOIP 2007 普及组] 纪念品分组

思路

包贪心题的。。。

首先我们要对于这个数组从小到大排序,能更好的选择组合。

然后我们用双指针法,算出答案 cnt。

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=3e4+10;
int a[maxn],cnt;
int n,w;
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>w>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    sort(a+1,a+1+n);
    int l=1,r=n;
    while(l<r){
        if(a[l]+a[r]<=w){
            cnt++;
            l++;
            r--;
        }
        else{
            cnt++;
            r--;
        }
        if(l==r){
            cnt++;
            break;
        }
    }
    cout<<cnt;
    return 0;
}

T2 U519235 划船计划

题意

我们需从n个人中选出尽可能多的两人团队,每个团队的两个人的重量和必须相同,且每个人只能属于一个团队。我们需要找到所有可能的团队组合中,能够组成最多团队数的方案。

思路

一样是贪心。

但是呢我们的 mango 把时限调成了 200ms,需要用一些科技手段 (doge

这道题我们的 w 并不知道(两人的重量),所以没办法,这个我们必须要去枚举得到。但是也有妙招,可以在输入的时候找最大值然后乘二就可以。

改了时限之后,这道题就需要用桶来记录每个重量出现的次数了。最后用上一道题一样的双指针法,对于这道题改进一些细节就可以通过。

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=1e4+10;
int a[maxn],cnt,tong[maxn];
int n,w;
int ans(int w){
    int cnt=0;
    for(int i=1;i<=100;i++){
        int x=i;
        int y=w-i;
        if(y<0 || x>y) continue;
        if(x==y) cnt+=tong[i]/2;
        else cnt+=min(tong[x],tong[y]);
    }
    return cnt;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int T;
    cin>>T;
    while(T--){
        int maxx=-1e18;
        int maxxx=-1e18;
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            maxxx=max(maxxx,a[i]);
            tong[a[i]]++;
        }
        int cnt=0;
        for(int w=2;w<=maxxx*2;w++){
            maxx=max(maxx,ans(w));
        } 
        for(int i=1;i<=n;i++){
            tong[a[i]]=0;
        }
        cout<<maxx<<'\n'; 
    }
    return 0;
}

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

思路

我们首先按刺激度从大到小排序。

这道题为了让为了最大化每个动作的价值,我们需要尽可能让相同动作之间的间隔时间最大。所以,我们采用两端向中间逼近的策略:

  1. 将刺激程度最高的动作安排在最早和最晚的时间点。

  2. 将刺激程度次高的动作安排在次早和次晚的时间点,以此类推。

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=1e3+10;
int n,k,a[maxn];
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>k;
    for(int i=1;i<=k;i++) cin>>a[i];
    sort(a+1,a+1+k,greater<int>());
    int l=1;
    int r=n;
    int idx=1;
    int ans=0;
    while(l<=r){
        ans=ans+a[idx]*(r-l);
        idx++;
        if(idx>k) break;
        r--;
        l++;
    }
    cout<<ans;
    return 0;
}

T4 P10483 小猫爬山

思路

不知道怎么说。。。

这道题用 dfs 暴力,我们在不超过重量限制下尝试没种情况,在最后我们也可以试着加上一辆缆车,但是要记得回溯!

在 dfs 最前面,我们还需要一个最优性剪枝就可以 AC 了。

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=20;
int ans,n,c[maxn],a[maxn],w;
void dfs(int sum,int step){
    if(sum>=ans) return;
    if(step>n){
        ans=min(ans,sum);
        return;
    }
    for(int i=1;i<=sum;i++){
        if(c[i]+a[step]<=w){
            c[i]+=a[step];
            dfs(sum,step+1);
            c[i]-=a[step];
        }
    }
    c[sum+1]=a[step];
    dfs(sum+1,step+1);
    c[sum+1]=0;
    return;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>w;
    for(int i=1;i<=n;i++) cin>>a[i];
    ans=n;
    dfs(0,1);
    cout<<ans;
    return 0;
}