https://codeforces.com/contest/1557/problem/A
A. Ezzat and Two Subsequences
题意:给你一个长度为n的数组,这个数组可能会有负数,现在你可以随便调换位置,最后把这个数组分成两个部分a,b,使得两个子数组的平均值最大。比如【3,1,2】可以分成a=【3】,b=【1,2】,f(a)=3,f(b)=(1+2)/2=1.5,f(a)+f(b)=4.5.因此4.5就是本题的答案。
思路O(N):是这样的,我们假设数组a里存的是平均值最大的数,其他数都存到数组b里,这样他们的平均值之和一定是最大的。因此我们先对数组从大到小排序,第一个数一定是a的(因为可能整个数组都是负数),每次通过判断第i个数加入a能否增大a数组的平均值,如果能就加入a里,反之就加入b中,且其后的数都加入b中。
代码:
#include<bits/stdc++.h>
#define ll long long
#define rep(a,b,c) for(int a=b;a<=c;a++)
#define per(a,b,c) for(int a=b;a>=c;a--)
#define hh 0x3f3f3f3f
const int maxn=1e5+5;
using namespace std;
inline int read()
{
ll s=0,f=1;
char c=getchar();
while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){s=(s<<3)+(s<<1)+c-'0';c=getchar();}
return s*f;
}
double a,b,ans,temp,A,B;
int cnt,n,pd,f[maxn];
bool cmp(int a,int b){return a>b;}
void solve()
{
n=read();//快读
a=0,b=0,ans=0,temp,pd=1,A=0,B=0;
for(int i=1;i<=n;i++)f[i]=read();
sort(f+1,f+1+n,cmp);
for(int i=1;i<=n;i++)
{
if(i==1)//第一个数加入a数组中
{
a+=f[i];A++;
continue;
}
else
{
temp=(a+f[i])/(A+1);
//判断这个数加入数组a里能后增大a的平均值
if(temp<=(a/A))pd=0;//如果不能就让i以及i后面的数加入b中
}
if(pd)//加入a数组中
{
a+=f[i];A++;
}
else//加入b数组中
{
b+=f[i];B++;
}
}
ans=(a/A)+(b/B);
printf("%.9llf\n",ans);
}
int main()
{
int t;
t=read();
while(t--)solve();
return 0;
}
https://codeforces.com/contest/1557/problem/B
B. Moamen and k-subarrays
题意:给你一个长度为n的数组,数组中每个数值都不相同,给你一个k,让他把这个数组分成k份,然后给这k组子数组从新组合,问你能不能构成一个递增的数组。如果能就输出Yes,反之就输出No。
思路O(n):如果是一个递增的数组,那么一个数a它右边一定是和他距离最近且比他大的数b。在同一个子数组里,a的坐标是i ,那么b的坐标一定是i+1,否者他们就不是同一个子数组里的。因此,我们可以用坐标标记的方法来解决这道题。举个例子:[3,6,4]:3和6不是相邻的因此不是同一个子数组,同理3和4也不是同一个子数组;[6,7,4]:6和7是同一个子数组,7和4不是同一个子数组。
首先,这个数组可能有负数,我们不能用桶标记的方法来实现,那么我们可以用map(q)这个牛逼的容器来帮助我们记录其对应的位置。然后再对我们输入的数组f[i]从小到大从新排序。如果第i个坐标和第i-1个的坐标相差不是1那么就说明他们不是同一个子数组。ans+1;(ans初始化一定是1因为无论怎么样必须是1个以上的子数组)。
代码:
#include<bits/stdc++.h>
#define ll long long
#define rep(a,b,c) for(int a=b;a<=c;a++)
#define per(a,b,c) for(int a=b;a>=c;a--)
#define hh 0x3f3f3f3f
const int maxn=1e5+5;
using namespace std;
inline int read()
{
ll s=0,f=1;
char c=getchar();
while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){s=(s<<3)+(s<<1)+c-'0';c=getchar();}
return s*f;
}
map<int,int>q;
int f[maxn],n,ans,k;
void solve()
{
ans=1;//开始无论怎么样必须是1个以上的子数组
cin>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>f[i];
q[f[i]]=i;//用map标记f[i]的序号
}
sort(f+1,f+1+n);
for(int i=2;i<=n;i++)
{
if((q[f[i]]-1)!=q[f[i-1]])ans++;//如果是同一个子数组那么他们的序号一定是相邻的,否则就不是同一个子数组
}
if(ans>k)cout<<"No"<<endl;
else cout<<"Yes"<<endl;
}
int main()
{
ios::sync_with_stdio(0);cout.tie(0);cin.tie(0);
int t;
cin>>t;
while(t--)solve();
return 0;
}