洛谷1583 魔法照片 解题报告

it2026-08-04  16

洛谷1583 魔法照片

本题地址: http://www.luogu.org/problem/show?pid=1583

题目描述

   一共有n(n≤20000)个人(以1--n编号)向佳佳要照片,而佳佳只能把照片给其中的k个人。佳佳按照与他们的关系好坏的程度给每个人赋予了一个初始权值W[i]。然后将初始权值从大到小进行排序,每人就有了一个序号D[i](取值同样是1--n)。按照这个序号对10取模的值将这些人分为10类。也就是说定义每个人的类别序号C[i]的值为(D[i]-1) mod 10 +1,显然类别序号的取值为1--10。第i类的人将会额外得到E[i]的权值。你需要做的就是求出加上额外权值以后,最终的权值最大的k个人,并输出他们的编号。在排序中,如果两人的W[i]相同,编号小的优先。

输入输出格式

输入格式:

   第一行输入用空格隔开的两个整数,分别是n和k。   第二行给出了10个正整数,分别是E[1]到E[10]。   第三行给出了n个正整数,第i个数表示编号为i的人的权值W[i]。

输出格式:

   只需输出一行用空格隔开的k个整数,分别表示最终的W[i]从高到低的人的编号。

输入输出样例

输入样例#1:

10 10 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20

输出样例#1:

10 9 8 7 6 5 4 3 2 1

题解

模拟+排序

多级排序,两次快排即可。

如果w[i]中有若干个量相同,那么这若干个量在第一遍排序后位置不同对结果也会有影响,那么必须把排序以后的序列中的相同数列段再次进行快排。

并且注意:

1.从大到小排列后按序号确定人的类型。

2.k可能等于0(一定注意特判)。

3.在排序中,如果两人的W[i]相同,编号小的优先。

下面附上代码。

代码

var data:array[1..50000,1..2] of longint;      e:array[1..10] of longint;      n,k,i,j:longint;  procedure change(var x,y:longint);  var t:longint;  begin   t:=x; x:=y; y:=t;  end;    procedure qsort(l,r:longint);  var mid,a,b,i:longint;  begin   mid:=data[(l+r) div 2,2]; a:=l; b:=r;   i:=data[(l+r) div 2,1];  repeat  while (data[a,2]>mid) or ((data[a,2]=mid) and (data[a,1]<i)) do inc(a);  while (data[b,2]<mid) or ((data[b,2]=mid) and (data[b,1]>i)) do dec(b);  if a<=b then  begin   change(data[a,2],data[b,2]);   change(data[a,1],data[b,1]);   inc(a); dec(b);  end;  until a>b;  if l<b then qsort(l,b);  if a<r then qsort(a,r);  end;    begin  read(n,k); fillchar(data,sizeof(data),0);  for i:=to 10 do read(e[i]);  for i:=to n do read(data[i,2]);  for i:=to n do data[i,1]:=i;  qsort(1,n);  for i:=to n do   data[i,2]:=data[i,2]+e[((i-1) mod 10)+1];  qsort(1,n);  for i:=to k do   write(data[i,1],' ');  end. 

(本文系笔者原创,未经允许不得转载)

转载于:https://www.cnblogs.com/yzm10/p/4751332.html

最新回复(0)