2019杭电多校第三场 6606-Distribution of books(树状数组)

Problem Description
传送门
zz6d likes reading very much, so he bought a lot of books. One day, zz6d brought n books to a classroom in school. The books of zz6d is so popular that K students in the classroom want to borrow his books to read. Every book of zz6d has a number i (1<=i<=n). Every student in the classroom wants to get a continuous number books. Every book has a pleasure value, which can be 0 or even negative (causing discomfort). Now zz6d needs to distribute these books to K students. The pleasure value of each student is defined as the sum of the pleasure values of all the books he obtains.Zz6d didn’t want his classmates to be too happy, so he wanted to minimize the maximum pleasure of the K classmates. zz6d can hide some last numbered books and not distribute them,which means he can just split the first x books into k parts and ignore the rest books, every part is consecutive and no two parts intersect with each other.However,every classmate must get at least one book.Now he wonders how small can the maximum pleasure of the K classmates be.

1<=T<=10
1<=n<=2*105
1<=k<=n
-109<=ai<=109

Input
Input contains multiple test cases.
The first line of the input is a single integer T which is the number of test cases. T test cases follow.
For each test case, the first line contains two integer n,k: the number of books and the number of his classmates. The second line contains n integers a1 ,a2,…, an−1,an. (aimeans the pleasure value of book i.)∑n<=2*105.

Output
For each case, print the smallest maximum pleasure of the K classmates, and one line one case.

Sample Input
2
4 2
3 -2 4 -2
5 4
-1 -1 -1 -1 6

Sample Output
2
-1
Hint
In the first example,classmate 1 get book 1,2, classmate 2 get book 3,4.the maximum pleasure is max((3-2),(4-2))=2;

In the second example,he can ignore book 5 and spilt the first 4 books into 4 parts,give them to his classmates.

思路
二分答案,问题就转化为求一个下降子序列的问题;
构造一个前缀和数组,求以第一个结点开头且后一个结点值减前一个结点小于等于二分的结果,这个序列长度大于k则二分结果成立;
用树状数组来维护降序的以每个点结尾的序列长度;
更新时需要将当前点的值减m,然后在降序的排列里找到位置,再利用前缀数组获取最值更新。

参考代码

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

const ll NINF=-(1ll<<61);
const int MAX_N=2e5+5;

struct node{
    ll s;
    int id;
};

int n,k;
ll a[MAX_N+2];
node data[MAX_N+2];
int ii[MAX_N+2];
ll b[MAX_N+2];
int bit[MAX_N+2];

bool cmp(node A,node B){
    return A.s>B.s;
}
bool cmp2(node A,node B){
    return A.id<B.id;
}

void add(int i,int x){
    while(i<=n){
        bit[i]=max(bit[i],x);
        i+=i&-i;
    }
}
ll sum(int i){
    int s=-1;
    while(i>0){
        s=max(s,bit[i]);
        i-=i&-i;
    }
    return s;
}

int find(ll x){
    int lb=0,ub=n+1;
    while(ub-lb>1){
        int mid=(lb+ub)/2;
        if(b[mid]>=x)lb=mid;
        else ub=mid;
    }
    return lb;
}

bool can(ll m){
    fill(bit,bit+n+1,-1);
    add(ii[1],0);
    for(int i=2;i<=n;i++){
        int j=find(data[i].s-m);
        int ans=sum(j);
        if(ans!=-1){
            if(ans>=k-1)return true;
            add(ii[i],ans+1);
        }
    }
    return false;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&k);
        data[1].s=0,data[1].id=1;
        for(int i=2;i<=n+1;i++){
            scanf("%lld",a+i);
            data[i].s=data[i-1].s+a[i];
            data[i].id=i;
        }
        n++;
        sort(data+1,data+n+1,cmp);
        for(int i=1;i<=n;i++){
            ii[data[i].id]=i;
            b[i]=data[i].s;
        }
        sort(data+1,data+n+1,cmp2);
        ll lb=-2e14-1,ub=1e9+1;
        while(ub-lb>1){
            ll mid=(lb+ub)/2;
            if(can(mid))ub=mid;
            else lb=mid;
        }
        printf("%lld\n",ub);
    }
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Out-of-distribution是指在模型训练时未曾出现过的数据分布,也称为“未知数据”。在模型面对未知数据时,其预测结果可能会出现误差或不确定性。因此,对于模型的鲁棒性和泛化能力的提升,需要对out-of-distribution数据进行有效的识别和处理。 ### 回答2: out-of-distribution(OoD)是指模型在测试阶段遇到了其训练数据集之外的样本或类别。当模型只使用特定的数据集进行训练时,它可能无法处理那些与训练数据不同的输入。这些新的样本可能是在颜色、形状、大小等方面与训练数据有所不同,也可能属于未在训练数据中出现过的类别。 遇到OoD样本的问题是模型的泛化能力不足。模型在训练数据中表示和学习的特征可能过于特定,无法推广到训练数据集之外的样本。这可能导致模型的预测不准确或不可靠。 为了解决OoD问题,有几种方法可以采取。一种常见的方法是收集更多来自OoD分布的样本,并将其添加到训练数据中,以使模型能够更好地学习如何处理这些新样本。另一种方法是使用一些先验知识或规则,对OoD样本进行检测和筛选,以避免对其进行错误预测。 同时,一些研究者提出了一些用于检测OoD样本的新颖性评估方法。这些方法通过利用模型在训练样本和OoD样本上的输出差异来判断一个样本是否属于OoD类别。这种方法可以帮助我们识别OoD样本,并采取相应的措施,以提高模型的泛化性能。 综上所述,解决out-of-distribution问题是训练一个具有较强泛化能力的模型的重要步骤。只有当模型能够有效处理新的样本和未见过的类别时,才能提高模型的可靠性和适用性。 ### 回答3: "out-of-distribution"是指数据集中没有包含的数据样本或样本类别。在机器学习和深度学习中,数据集通常用于训练和测试模型的性能。然而,在现实世界中,我们会遇到无法准确分类的新数据,这些数据就属于"out-of-distribution"。这可能是因为这些数据具有与训练数据不同的特征,或者因为数据集的覆盖范围有限。 "out-of-distribution"的出现可能会对模型的性能和鲁棒性产生负面影响。由于模型没有前面没有见过这些类型的数据,它可能会对其进行错误的分类或给出不确定的预测结果。这种情况在实际应用中特别重要,因为我们希望模型能够在各种不同的情况下表现得可靠和准确。 为了解决"out-of-distribution"问题,一种常见的方法是通过收集更多具有代表性的训练数据来增加数据集的覆盖范围。这样模型可以更好地学习不同类型的数据特征,并提高对"out-of-distribution"数据的泛化能力。另外,使用先进的模型架构和优化算法也可以增强模型的鲁棒性。 除了增加训练数据和改进模型架构外,还可以使用一些检测方法来识别"out-of-distribution"的样本。这些方法可以根据模型的置信度、预测熵或数据分布等特征来判断样本是否属于训练集之外的数据。这些方法可以帮助我们发现并处理那些可能造成模型失效的"out-of-distribution"数据。 总之,"out-of-distribution"是指在训练数据之外的数据样本或样本类别。对于机器学习和深度学习任务,了解和解决"out-of-distribution"问题是提高模型性能和鲁棒性的关键。通过增加训练数据、改进模型架构和使用检测方法,我们可以减少"out-of-distribution"带来的负面影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值