贪心—有时是对的 ---qzezoj 1532: ZCC Loves Codefires
275307894a · · 个人记录
贪心—有时是对的
---qzezoj 1532: ZCC Loves Codefires
题目:
题目描述
Though ZCC has many Fans, ZCC himself is a crazy Fan of a coder, called “Memset137”.
It was on Codefires(CF), an online competitive programming site, that ZCC knew Memset137, and immediately became his fan.
But why?
Because Memset137 can solve all problem in rounds, without unsuccessful submissions; his estimation of time to solve certain problem is so accurate, that he can surely get an Accepted the second he has predicted. He soon became IGM, the best title of Codefires. Besides, he is famous for his coding speed and the achievement in the field of Data Structures.
After become IGM, Memset137 has a new goal: He wants his score in CF rounds to be as large as possible.
What is score? In Codefires, every problem has 2 attributes, let’s call them Ki and Bi(Ki, Bi>0). if Memset137 solves the problem at Ti-th second, he gained Bi-Ki*Ti score. It’s guaranteed Bi-Ki*Ti is always positive during the round time.
Now that Memset137 can solve every problem, in this problem, Bi is of no concern. Please write a program to calculate the minimal score he will lose.(that is, the sum of Ki*Ti).
输入
The first line contains an integer N(1≤N≤10^5), the number of problem in the round.
The second line contains N integers Ei(1≤Ei≤10^4), the time(second) to solve the i-th problem.
The last line contains N integers Ki(1≤Ki≤10^4), as was described.
输出
One integer L, the minimal score he will lose.
样例输入
3
10 10 20
1 2 3
样例输出
150
提示
Memset137 takes the first 10 seconds to solve problem B, then solves problem C at the end of the 30th second. Memset137 gets AK at the end of the 40th second.
L = 10 * 2 + (10+20) * 3 + (10+20+10) * 1 = 150.
贪心一般和排序逃不开关系。这一道题目是普及组原题的弱化版。
首先我们列出答案的式子:ans=∑_(i=1)^n▒〖a[i]*∑_(j=1)^i▒〖b[j]〗〗
乘法分配律,得ans=∑_(i=1)^n▒〖a[i]*b[i]+a[i]*∑_(j=1)^(i-1)▒〖b[j]〗〗
分配到每一个si,得s[i]=a[i]*b[i]+a[i]*∑_(j=1)^(i-1)▒〖b[j]〗
若要比较两个a[x]与a[y]的优先级关系,得a[x]*b[x]+a[x]*∑_(j=1)^(x-1)▒b[j] +a[y]*b[y]+a[y]*∑_(j=1)^x▒b[j] <a[y]*b[y]+a[y]*∑_(j=1)^(y-1)▒b[j] +a[x]*b[x]+a[x]*∑_(j=1)^y▒b[j]
两边同时消去,得a[x]*∑_(j=1)^(x-1)▒b[j] +a[y]*∑_(j=1)^x▒b[j] <+a[y]*∑_(j=1)^(y-1)▒b[j] +a[x]*∑_(j=1)^y▒b[j]
再由于y=x+1,进行乘法分配律拆解,得a[x]*∑_(j=1)^x▒b[j] +a[y]*b[x]+a[y]*∑_(j=1)^x▒b[j] <+a[y]*∑_(j=1)^x▒b[j] +a[x]*b[y]+a[x]*∑_(j=1)^x▒b[j]
两边消去,得a[y]*b[x]<a[x]*b[y]。
所以一个排序函数就可以得到答案了。
代码实现:
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
struct yyy{
int x,y;
}s[100039];
inline bool cmp(yyy x,yyy y){
return x.x*y.y<y.x*x.y;
}
long long ans,tot;
int main(){
register int i;
scanf("%d",&n);
for(i=1;i<=n;i++) scanf("%d",&s[i].x);
for(i=1;i<=n;i++) scanf("%d",&s[i].y);
sort(s+1,s+n+1,cmp);
for(i=1;i<=n;i++){
ans+=s[i].x;
tot+=ans*s[i].y;
}
printf("%lld",tot);
return 0;
}
不过瘾?再来看一道。
问题 M: 【未分类】[ZJOI2008]泡泡堂BNB
时间限制: 1 Sec 内存限制: 256 MB
提交: 2 解决: 2
[提交] [状态] [讨论版] [命题人:admin]
题目描述
第XXXX届NOI期间,为了加强各省选手之间的交流,组委会决定组织一场省际电子竞技大赛,每一个省的代表
队由n名选手组成,比赛的项目是老少咸宜的网络游戏泡泡堂。每一场比赛前,对阵双方的教练向组委会提交一份
参赛选手的名单,决定了选手上场的顺序,一经确定,不得修改。比赛中,双方的一号选手,二号选手……,n号
选手捉对厮杀,共进行n场比赛。每胜一场比赛得2分,平一场得1分,输一场不得分。最终将双方的单场得分相加
得出总分,总分高的队伍晋级(总分相同抽签决定)。作为浙江队的领队,你已经在事先将各省所有选手的泡泡堂水
平了解的一清二楚,并将其用一个实力值来衡量。为简化问题,我们假定选手在游戏中完全不受任何外界因素干扰
,即实力强的选手一定可以战胜实力弱的选手,而两个实力相同的选手一定会战平。由于完全不知道对手会使用何
种策略来确定出场顺序,所以所有的队伍都采取了这样一种策略,就是完全随机决定出场顺序。当然你不想这样不
明不白的进行比赛。你想事先了解一下在最好与最坏的情况下,浙江队最终分别能得到多少分。
输入
输入的第一行为一个整数n,表示每支代表队的人数。接下来n行,每行一个整数,描述了n位浙江队的选手的
实力值。接下来n行,每行一个整数,描述了你的对手的n位选手的实力值。 20%的数据中,1<=n<=10; 40%的数
据中,1<=n<=100; 60%的数据中,1<=n<=1000; 100%的数据中,1<=n<=100000,且所有选手的实力值在0到100
00000之间。
输出
包括两个用空格隔开的整数,分别表示浙江队在最好与最坏的情况下分别能得多少分。不要在行末输出多余的
空白字符。
样例输入
2
1
3
2
4
样例输出
2 0
提示
样例说明
我们分别称4位选手为A,B,C,D。则可能出现以下4种对战方式,最好情况下可得2分,最坏情况下得0分。
一 二 三 四
浙江 ??? 结果 浙江 ??? 结果 浙江 ??? 结果 浙江 ??? 结果
一号选手 A C 负 A D 负 B C 胜 B D 负
二号选手 B D 负 B C 胜 A D 负 A C 负
总得分 0 2 2 0
[提交][状态]
welcome to QZEZ 2020
he一看就是田忌赛马呀,只不过做两遍罢了。那么我们直接看代码。。。。。。(笑)
首先如何让得分最大化。
回想一下田忌赛马,预备赛马的应该是一个有序组。所以我们先排一个序,满足i<j<k,a[i]>=a[j]>=a[k].
然后应该考虑最强的两匹马。分类讨论。
命题一:若我方最强的马可以干掉对方最强的马,则直接干。
命题二:若我方最弱的马可以干掉对方最弱的马,则直接干
命题三:若命题一命题二都不成立,那么直接让我方最弱的马送给对方最强的马。
但在命题三中,我方最弱的马可能与对方最弱的马打平。所以要特判。
我方最低分则以对手最高分减去总分就可以了。
代码实现:
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m,a[100039],b[100039],ra,rb,la,lb,ans,flag;
int main() {
register int i;
scanf("%d",&n);
for(i=1; i<=n; i++) scanf("%d",&a[i]);
for(i=1; i<=n; i++) scanf("%d",&b[i]);
sort(a+1,a+n+1);
sort(b+1,b+n+1);
ra=rb=n;
la=lb=1;
for(i=1; i<=n; i++) {
if(a[ra]>b[rb])ra--,rb--,ans+=2;
else if(a[la]>b[lb])lb++,la++,ans+=2;
else {
if(a[la]==b[rb]) la++,rb--,ans++;
else la++,rb--;
}
}
printf("%d ",ans);
ra=rb=n;
la=lb=1;
ans=0;
for(i=1; i<=n; i++) {
if(a[ra]<b[rb])ra--,rb--,ans+=2;
else if(a[la]<b[lb])lb++,la++,ans+=2;
else {
if(a[ra]==b[lb]) lb++,ra--,ans++;
else lb++,ra--;
}
}
printf("%d",2*n-ans);
}