10月27日NOIP2018提高组省一冲奖班模测训练3
T1 Anna的派对
题目描述
Anan想举办一个派对。
Anan的朋友总共有 n 人。第i个人如果参加派对会得到 ci 的快乐值,除他自己外每多一个人参加他会减少 di 的快乐值。
Anan想让这个派对的总快乐值尽可能大,在这个基础上,能来的人越多越好。
Anan想知道最佳的邀请方案的快乐值与参加人数。
输入格式
第一行一个正整数n 。 第二行n个整数表示ci。 第三行n个整数表示di。
输出格式
第一行一个整数最大快乐值。 第二行一个整数表示最佳方案的参加人数。
输入样例
6
10 10 10 10 10 9
2 2 2 2 2 3
输出样例
18
3
数据范围
对于 50% 的数据, n≤20
对于 100% 的数据, n≤1000
思路
贪心 + 暴力
震惊, 这题竟然是直接暴枚参加人数, 看到数据范围时惊呆了, 怀疑自己是不是有什么坑没有发现, 然而就是暴枚
复杂度只有O(n^2logn), 愉快AC
代码
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 1e3 + 5;
int n, tot, ans, sum;
int c[MAXN], d[MAXN], h[MAXN];
inline int read ();
inline bool cmp (int lhs, int rhs);
int main ()
{
n = read ();
for (int i = 1; i <= n; ++ i){
c[i] = read ();
}
for (int i = 1; i <= n; ++ i){
d[i] = read ();
}
for (int i = 1; i <= n; ++ i){ //枚举来的人数
sum = 0;
for (int j = 1; j <= n; ++ j){ //计算每个人的快乐值
h[j] = c[j] - d[j] * (i - 1);
}
sort (h + 1, h + 1 + n, cmp); //排序, 从大到小贪心选
for (int j = 1; j <= i; ++ j){
sum += h[j];
}
if (ans < sum){
ans = sum;
tot = i;
}
if (ans == sum){
tot = i;
}
}
printf ("%d\n%d", ans, tot);
return 0;
}
inline int read ()
{
char ch = getchar ();
while (!isdigit (ch)) ch = getchar ();
int x = 0;
while (isdigit (ch)){
x = x * 10 + ch - '0';
ch = getchar ();
}
return x;
}
inline bool cmp (int lhs, int rhs)
{
return lhs > rhs;
}