QOJ.ac

QOJ

總分: 100 不可用

#17239. 均位数

统计

一些公司喜欢对员工的薪资保密,以防止工会领袖了解员工薪资过低的情况,从而避免关于加薪和奖金的繁琐谈判。然而,有时公司也乐意为了统计和营销目的公布某些信息。

某家特定的公司愿意回答以下形式的问题:“员工 $A$、$B$、$C$ 和 $D$ 的中位均值(meandian)薪资是多少?”。四个数的中位均值定义为这两个中位数的算术平均值。更具体地说,序列 $a, b, c, d$ 的中位均值是通过先对序列进行排序,然后计算 $(x+y)/2$ 得到的,其中 $x$ 和 $y$ 是排序后序列中的第二个和第三个元素。你的目标是通过询问若干个此类问题来找出所有员工的精确薪资。请注意,即使询问了所有可能的问题,某些员工的薪资也永远无法被推导出来(例如,薪资最低的员工)。

该公司有 $N$($4 \le N \le 100$)名员工,用整数 $1$ 到 $N$ 标记。每位员工的薪资都是一个小于或等于 $100\,000$ 的正偶数,且没有两位员工的薪资相同。

为你提供了一个实现 Meandian 函数的库。给定四个不同的整数 $A, B, C, D$($1 \le A, B, C, D \le N$),该函数返回员工 $A, B, C, D$ 薪资的中位均值。

编写一个程序,在可以使用该库的情况下,找出所有员工的精确薪资,但那些永远无法确定的薪资除外。你的程序最多允许提问 $1000$ 次。

库函数

你可以使用实现以下三个函数的库:

  • Init – 无参数调用。该函数应在程序开始时恰好调用一次。它不接受任何参数,并返回整数 $N$ —— 公司的员工人数。

    function Init : longint;
    int Init(void);
  • Meandian – 该函数应传入四个参数 $A, B, C, D$ —— 介于 $1$ 和 $N$ 之间(含 $1$ 和 $N$)的互不相同的整数。它返回一个整数,即员工 $A, B, C, D$ 薪资的中位均值。

    function Meandian(a,b,c,d : longint) : longint;
    int Meandian(int,int,int,int);
  • Solution – 该函数应在程序结束时调用。它接受一个表示对应员工薪资的整数数组作为参数。如果无法确定某个特定员工的薪资,则数组中对应的数值应为 $-1$。

    请注意,在所有编程语言中,返回的数组都应该是从 0 开始索引的。这意味着员工 1 的薪资应位于数组的第 0 个位置,员工 2 的薪资应位于第 1 个位置,依此类推。

    procedure Solution(var sol : array of longint);
    void Solution(const int *);

在 Pascal 中使用库

你程序的源代码必须包含语句 uses libmean;

要构建和测试你的解决方案,你可以从竞赛系统的“Tasks”页面下载示例库。将该库放在你编译的目录下。

在 C/C++ 中使用库

你程序的源代码必须包含指令 #include "libmean.h"

要构建和测试你的解决方案,你可以从竞赛系统的“Tasks”页面下载示例库。

为了编译你的解决方案,你需要将其与提供的库进行链接,使用如下命令行:

gcc -o meandian meandian.c libmean.o

g++ -o meandian meandian.cpp libmean.o

测试

提供的示例库允许你通过在标准输入中输入数字 $N$,后跟 $N$ 个偶数来测试你的解决方案。

该库将输出一条消息,指示你的解决方案是否正确。它还将创建一个文本文件 meandian.log,其中包含程序执行的详细信息。

以下是完全不能解决该问题,但可用作在不同编程语言中使用该库的示例代码片段。

Pascal 示例

uses libmean; 
var i, n : integer; 
 arr : array[0..99] of longint; 
 foo, bar, quux : integer; 
begin 
 n := Init; 
 foo := Meandian(1, 2, 3, 4); 
 bar := Meandian(4, 2, 3, 1); 
 quux := Meandian(n, n-1, n-2, n-3); 
 for i := 1 to n do 
 arr[i-1] := 2*i; 
 arr[3] := -1; 
 Solution(arr); 
end.

C/C++ 示例

#include "libmean.h" 
int main(void) 
{ 
 int i, n; 
 int arr[100]; 
 int foo, bar, quux; 
 n = Init(); 
 foo = Meandian(1, 2, 3, 4); 
 bar = Meandian(4, 2, 3, 1); 
 quux = Meandian(n, n-1, n-2, n-3); 
 for (i=1; i<=n; ++i) 
 arr[i-1] = 2*i; 
 arr[3] = -1; 
 Solution(arr); 
 return 0; 
}

样例

以下是如何为与提供的示例库链接的程序输入数据的示例。该库会告诉你你的解决方案是否正确。

输入 1

10 
100 500 200 400 250 300 350 600 550 410

或者逐个上传:

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.