Codeforces_841_C Leha and Function(贪心+构造|规律)

来源:互联网 发布:最优化方法第二版答案 编辑:程序博客网 时间:2024/05/16 16:06

题目链接

题意

F(n,k): 一个1~n的集合M,从M中任意拿出k个元素,构成大小为k的子集, F(n,k)是所有子集的最小值的贡献的平均值

For example, let’s find F(4, 2). All possible 2-element subsets of {1,2,3,4} are: {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4}. Their minimal values are 1, 1, 1, 2, 2, 3. So the average (expected) value is F(4,2)=(1+1+1+2+2+3)/6=10/6=1.66666666

现在给出两个数组,A[]和B[],让我们对A[]数组重新排序,使得** 最大(抱歉不会打数学公式,请看原题)

解决

  1. 理论知识待补充,这篇博客有给证明
  2. 观察样例,我们发现优先让A[]数组中最大的去匹配B[]数组中最小的
  3. 构造一个结构体,保存数字和位置
  4. 排序后输出

希望我的代码能被大家读懂^_^

#include<bits/stdc++.h>using namespace std;const int maxn=2e5+5;struct node{    int num,id;    bool operator<(node n2)const    {        return num<n2.num;    }}a[maxn],b[maxn];int ans[maxn];int main(){    int n;    scanf("%d",&n);    for(int i=1;i<=n;i++)    {        scanf("%d",&a[i].num);        a[i].id=i;    }    for(int i=1;i<=n;i++)    {        scanf("%d",&b[i].num);        b[i].id=i;    }    sort(a+1,a+1+n);    sort(b+1,b+1+n);    for(int i=1;i<=n;i++)    {        ans[b[i].id]=a[n+1-i].num;    }    for(int i=1;i<=n;i++)        printf("%d%c",ans[i],i==n?'\n':' ');}
原创粉丝点击