Codeforces Problem 717C Potions Homework(排序)

来源:互联网 发布:java飞机大战子弹 编辑:程序博客网 时间:2024/05/23 21:32

此文章可以使用目录功能哟↑(点击上方[+])

比赛链接→Bubble Cup 9 - Finals [Online Mirror]

 Codeforces Problem 717C Potions Homework

Accept: 0    Submit: 0
Time Limit: 1 second    Memory Limit : 256 megabytes

 Problem Description

Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class.

Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where ai represents both the laziness level of the i-th student and the difficulty of his task.

The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007.

 Input

The first line of input contains integer n (1 ≤ n ≤ 100 000) — the number of tasks. The next n lines contain exactly one integer number ai (1 ≤ ai ≤ 100 000) — both the difficulty of the initial task and the laziness of the i-th students.

 Output

Print the minimum total time to finish all tasks modulo 10 007.

 Sample Input

2
1
3

 Sample Output

6

 Hint

In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.

 Problem Idea

解题思路:

【题意】

给你n个数,第i个数表示第i位学生的懒散度和第i个任务的难度

一位学生完成一个任务的时间=该学生的懒散度×该任务的难度

问在最优的任务分配下,完成n个任务的最少时间是多少

要求每位学生只能完成一个任务,每个任务只能由一个学生完成

【类型】
排序

【分析】

相信大家对数的敏锐性

遇到这样的题,第一感觉是

拿最小的数乘最大的数,加上第二小的数乘第二大的数,以此类推……

即如下式子(ai表示已经排好序的数组a中的第i个数):


那这样子究竟是不是最小的呢?

博主想过直接证明的方法,但是有点笨,没有想出来,要是有人有好的想法,希望能够共享

所以,博主在此处只能提供反证法来稍微证明一下

假设如下式子并不是和最小的式子:


那么必定存在一个和最小的式子:


且Min'<Min

但是


与假设不符,故命题成立

即式子成立

【时间复杂度&&优化】
O(nlogn)

题目链接→Codeforces Problem 717C Potions Homework

 Source Code

/*Sherlock and Watson and Adler*/#pragma comment(linker, "/STACK:1024000000,1024000000")#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<stack>#include<math.h>#include<vector>#include<map>#include<set>#include<bitset>#include<cmath>#include<complex>#include<string>#include<algorithm>#include<iostream>#define eps 1e-9#define LL long long#define PI acos(-1.0)#define bitnum(a) __builtin_popcount(a)using namespace std;const int N = 100005;const int M = 100005;const int inf = 1000000007;const int mod = 10007;int s[N];int main(){    int n,i,ans=0;    scanf("%d",&n);    for(i=0;i<n;i++)        scanf("%d",&s[i]);    sort(s,s+n);    for(i=0;i<n;i++)        ans=(ans+s[i]%mod*(s[n-1-i]%mod))%mod;    printf("%d\n",ans);    return 0;}
菜鸟成长记

0 0
原创粉丝点击