Film Critics ( 2020-2021 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2020) )
The premier of the anticipated action film No Thyme to Fry is right around the corner, and it is time to give early screenings to film critics so that they can review it. A small cinema has been selected to show these early screenings.
There are n n n critics numbered from 1 1 1 to n n n scheduled to watch the movie early, and each of them will watch it separately. After watching it, they will immediately give it a score from 0 0 0 to m m m. Susan, the cinema owner, has carefully looked at every critic’s social media and already knows that the i i ith critic thinks the movie is worth a score of a i a_i ai. However, the i i ith critic will not simply give the movie a score of a i a_i ai like you would expect, because they also take into account the scores that the other critics gave. Here is how they behave:
-
The first critic to arrive will be so happy that they are the first to review the movie that they will give it a score of m m m regardless of their initial opinion.
-
Every subsequent critic will look at the average score given by the previous critics. If this number is smaller than or equal to the initial opinion a i a_i ai then the critic will give it a score of m m m, otherwise they will give it a 0 0 0.
Susan thinks the critics’ behaviour is ridiculous. She has watched the movie, and it is clearly worth a score of exactly k / n k/n k/n and nothing else! But Susan is the owner of the cinema, so she gets to decide in what order to invite the critics. Your task is to find a permutation of 1 , 2 , … , n 1,2, \dots, n 1,2,…,n so that if the critics arrive in this order the average score will be exactly k / n k/n k/n.
Input
The first line of input contains three integers n n n, m m m and k k k ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \leq n \leq 2 \cdot 10^5 1≤n≤2⋅105, 1 ≤ m ≤ 1 0 4 1 \leq m \leq 10^4 1≤m≤104, 0 ≤ k ≤ n ⋅ m 0 \leq k \leq n \cdot m 0≤k≤n⋅m). The second line contains the n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an ( 0 ≤ a i ≤ m 0 \le a_i \le m 0≤ai≤m for each i i i), the n n n critic scores as described above.
Output
If the critics can be ordered in such a way that the resulting average score is exactly k / n k/n k/n, then output n n n integers p 1 , … , p n p_1, \ldots, p_n p1,…,pn ( 1 ≤ p i ≤ n 1 \le p_i \le n 1≤pi≤n), where p i p_i pi indicates that the i i ith critic to visit the cinema is the critic numbered p i p_i pi. This list of integers should be a permutation such that the average score given by the critics is k / n k/n k/n. If there are multiple solutions any one will be accepted.
Otherwise, if there is no such way to order the critics, output “impossible”.
Examples
Input
Copy
5 10 30
10 5 3 1 3
Output
3 5 2 1 4
Input
5 5 20
5 3 3 3 3
Output
impossible
题目描述:
电影《No Thyme to Fry》的首映即将到来,电影评论员们将提前观看该电影并给出评分。电影院的老板 Susan 已经知道每个评论员给电影的初始评分,但是每个评论员在给出评分时并不会仅仅按照自己的初始评分给出,而是会受到其他评论员评分的影响。评论员们的行为规则如下:
- 第一位到达的评论员会非常高兴,因为是第一个观看电影的评论员,因此他们会给电影打满分
m
。 - 后来的每一位评论员都会查看前面评论员们给出的平均分。如果平均分小于或等于他们自己的初始评分
a_i
,则他们会给电影打满分m
;否则,他们会给电影打0
分。
电影院的老板 Susan 认为评论员的这种行为非常荒谬,实际上电影的评分应该是 k/n
(即总评分的平均值)。Susan 可以决定评论员的观看顺序,问题是:是否存在一种评论员观看顺序,使得最后的平均评分正好是 k/n
?如果存在,则输出评论员的观看顺序;否则,输出 “impossible”。
题解
这个问题要求我们安排电影评论家观看电影的顺序,以便电影的最终评分平均值等于 k/n
。每个评论家会给出一个分数,评分要么是 m
(积极评分),要么是 0
(消极评分)。每个评论家根据当前评分的平均值和自己对电影的初步评分决定是否给出 m
还是 0
。如果当前的平均评分大于评论家的初步评分,他们会给出 0
;否则,他们会给出 m
。
初步观察
- 每个评论的分数要么是积极的(评分
m
),要么是消极的(评分0
)。 - 如果有
p
个积极评论,那么最终的平均分是p * m / n
。也就是说,最终的平均分k/n
必须是m
的整数倍,否则就不可能达到目标。 - 我们需要恰好有
p = k/m
个积极评论。
关键思路
- 我们可以假设,最优秀的
p
个评论家会给出积极的评价(即评分m
),其余的评论家会给出消极的评价(即评分0
)。 - 给出积极评分的评论家会按他们的初步评分从小到大排序(即评分较低的先给出积极评分),而给出消极评分的评论家会按他们的初步评分从大到小排序(即评分较高的先给出消极评分)。
- 我们的目标是按照这种顺序排列评论家,确保最终的平均评分为
k/n
。
解决方案
- 排序:首先,我们按照评论家的评分进行排序。将评分较高的
p
个评论家排在前面,其他评论家排在后面。 - 构造答案:我们从这两组评论家中选择评论家依次安排他们的顺序,确保当前的平均评分符合要求。
- 迭代过程:每次迭代,我们有两个候选评论家:一个是当前剩余的
p
个最高评分评论家的最低评分,另一个是剩余的n-p
个最低评分评论家的最高评分。我们根据当前的平均评分选择合适的评论家。 - 不可能的情况:如果某个分组已经为空,但我们仍需要从中选评论家,或者无法选择符合条件的评论家,则说明排序是不可能的。
时间复杂度
- 排序的时间复杂度是
O(n log n)
,然后我们根据当前的平均评分进行一次O(n)
的迭代。 - 总的时间复杂度是
O(n log n)
。
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define close ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
const int mod=1e9+7;
const int N=2e5+10;
const int inf=1e18+7;
signed main()
{
int n,m,k;
cin>>n>>m>>k;
vector<pair<int,int>>v(n+1);
vector<pair<int,int>>ans(n+1,{-1,0});
int id=0;
for(int i=1;i<=n;++i)
{
cin>>v[i].first;
v[i].second=i;
}
if(k%m!=0 or k==0)
{
cout<<"impossible";
return 0;
}
sort(v.begin()+1,v.begin()+n+1);
int flag=1;
int now=0;
for(int i=n-k/m+2;i<=n;++i)
{
now+=m;
int id_;
if(v[i].first==0)
{
id_=n;
}
else
{
id_=(now-1)/v[i].first+2;
}
if(id_>n)
{
flag=0;
break;
}
if(id_>id)
{
id=id_;
ans[id]=v[i];
}
else
{
id++;
if(id>n)
{
flag=0;
break;
}
ans[id]=v[i];
}
}
if(flag==0)
{
cout<<"impossible"<<endl;
return 0;
}
id=1;
for(int i=n-k/m+1;i>=1;--i)
{
if(ans[id].first!=-1)
{
id++;
i++;
continue;
}
ans[id]=v[i];
id++;
}
int av=0;
for(int i=1;i<=n;++i)
{
//cout<<av<<' '<<ans[i]*(i-1)<<endl;
if(av<=ans[i].first*(i-1))
{
av+=m;
}
}
if(av!=k)
{
flag=0;
}
if(flag==0)
{
cout<<"impossible"<<endl;
return 0;
}
for(int i=1;i<=n;++i)
{
cout<<ans[i].second<<' ';
}
}