【14.94%】【codeforces 611E】New Year and Three Musketeers
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Do you know the story about the three musketeers? Anyway, you must help them now.
Richelimakieu is a cardinal in the city of Bearis. He found three brave warriors and called them the three musketeers. Athos has strength a, Borthos strength b, and Caramis has strength c.
The year 2015 is almost over and there are still n criminals to be defeated. The i-th criminal has strength ti. It’s hard to defeat strong criminals — maybe musketeers will have to fight together to achieve it.
Richelimakieu will coordinate musketeers’ actions. In each hour each musketeer can either do nothing or be assigned to one criminal. Two or three musketeers can be assigned to the same criminal and then their strengths are summed up. A criminal can be defeated in exactly one hour (also if two or three musketeers fight him). Richelimakieu can’t allow the situation where a criminal has strength bigger than the sum of strengths of musketeers fighting him — a criminal would win then!
In other words, there are three ways to defeat a criminal.
A musketeer of the strength x in one hour can defeat a criminal of the strength not greater than x. So, for example Athos in one hour can defeat criminal i only if ti ≤ a.
Two musketeers can fight together and in one hour defeat a criminal of the strength not greater than the sum of strengths of these two musketeers. So, for example Athos and Caramis in one hour can defeat criminal i only if ti ≤ a + c. Note that the third remaining musketeer can either do nothing or fight some other criminal.
Similarly, all three musketeers can fight together and in one hour defeat a criminal of the strength not greater than the sum of musketeers’ strengths, i.e. ti ≤ a + b + c.
Richelimakieu doesn’t want musketeers to fight during the New Year’s Eve. Thus, he must coordinate their actions in order to minimize the number of hours till all criminals will be defeated.
Find the minimum number of hours to defeat all criminals. If musketeers can’t defeat them all then print “-1” (without the quotes) instead.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of criminals.
The second line contains three integers a, b and c (1 ≤ a, b, c ≤ 108) — strengths of musketeers.
The third line contains n integers t1, t2, …, tn (1 ≤ ti ≤ 108) — strengths of criminals.
Output
Print one line with the answer.
If it’s impossible to defeat all criminals, print “-1” (without the quotes). Otherwise, print the minimum number of hours the three musketeers will spend on defeating all criminals.
Examples
input
5
10 20 30
1 1 1 1 50
output
2
input
5
10 20 30
1 1 1 1 51
output
3
input
7
30 20 10
34 19 50 33 88 15 20
output
-1
input
6
10 5 10
10 9 5 25 20 5
output
3
Note
In the first sample Athos has strength 10, Borthos 20, and Caramis 30. They can defeat all criminals in two hours:
Borthos and Caramis should together fight a criminal with strength 50. In the same hour Athos can fight one of four criminals with strength 1.
There are three criminals left, each with strength 1. Each musketeer can fight one criminal in the second hour.
In the second sample all three musketeers must together fight a criminal with strength 51. It takes one hour. In the second hour they can fight separately, each with one criminal. In the third hour one criminal is left and any of musketeers can fight him.
【题解】
贪心;
用平衡树来找小于等于某个能力值的抵抗力最大的敌人,并删掉它;
用代码来讲;(以一小时为单位,查看一小时,这3个人能做些什么);
//a[1]<a[2]<a[3]
//work(a)会删去平衡树中小于等于a且最大的数字;
int mx = get_max(root);
if (mx<=a[1])//如果抵抗力最大的敌人都比能力值最小的那个人小。则三个人每次都能打3个敌人
{
ans--;
ans +=(n+2)/3;
break;
}
else
if (mx<=a[2])//如果mx<a[1]<=a[2];
{//则分别去打就好;
work(a[1]);
work(a[2]);
work(a[3]);
}
else
if (mx <=a[3])//如果a[1]<a[2]<mx<=a[3]
{//如果a[1]和a[2]每个人都能打一个敌人(两个),则让他们俩分别打;如果两人总共只能打一只或不能打,尝试a[1]+a[2]合起来打一只会更好;
qianqu = 0;
ask_before(root,a[1]);
bool flag = 1;
if (qianqu!=0)
{
int temp1 = qianqu;
de_lete(root,qianqu);
qianqu = 0;
ask_before(root,a[2]);
if (qianqu!=0)
{
de_lete(root,qianqu);
n-=2;
work(a[3]);
}
else
{
insert(root,temp1);
flag = 0;
}
}
else
flag = 0;
if (!flag)
{
work(a[1]+a[2]);
work(a[3]);
}
}
else
if (mx<=a[1]+a[2])//小于最小的两个,就a[1]+a[2]合体然后a[3]分别打;
{
work(a[1]+a[2]);
work(a[3]);
}
else
if (mx <= a[1]+a[3])//同理
{
work(a[1]+a[3]);
work(a[2]);
}
else
if (mx <= a[2]+a[3])
{
work(a[2]+a[3]);
work(a[1]);
}
else//想等就全部合起来打;
work(a[1]+a[2]+a[3]);
完整代码↓↓↓
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <string>
#define lson L,m,rt<<1
#define rson m+1,R,rt<<1|1
#define LL long long
using namespace std;
const int MAXN = 6000990;
const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0);
int n,root=0,l[MAXN],r[MAXN],key[MAXN],size[MAXN],totn = 0,cf[MAXN],a[4],qianqu;
int t[MAXN];
int get_max(int t)
{
int temp = r[t];
while (temp)
{
t = r[t];
temp = r[t];
}
return key[t];
}
void right_rotation(int &t)
{
int k = l[t];
l[t] = r[k];
r[k] = t;
size[k] = size[t];
size[t] = size[l[t]]+size[r[t]]+1;
t = k;
}
void left_rotation(int &t)
{
int k = r[t];
r[t] = l[k];
l[k] = t;
size[k] = size[t];
size[t] = size[r[t]]+size[l[t]]+1;
t = k;
}
void maintain(int &t,bool flag)
{
if (flag)
{
if (size[l[l[t]]] > size[r[t]])
right_rotation(t);
else
if (size[r[l[t]]] > size[r[t]])
{
left_rotation(l[t]);
right_rotation(t);
}
else
return;//如果都不是的话就结束递归
}
else
{
if (size[r[r[t]]] > size[l[t]])
{
left_rotation(t);
}
else
if (size[l[r[t]]] > size[l[t]])
{
right_rotation(r[t]);
left_rotation(t);
}
else
return;
}
maintain(l[t],true);
maintain(r[t],false);
maintain(t,true);
maintain(t,false);
}
void insert(int &t,int data)
{
if (t==0)
{
t=++totn;
l[t] = r[t] = 0;
size[t] = 1;
key[t] = data;
}
else
{
size[t]++;
if (data<key[t])
insert(l[t],data);
else
insert(r[t],data);
maintain(t,data<key[t]);
}
}
void ask_before(int t, int x)
{//询问前趋是啥
if (!t)
return;
if (key[t] <= x)
{
qianqu = max(qianqu, key[t]);
ask_before(r[t], x);
}
else
if (x < key[t])
ask_before(l[t], x);
}
void de_lete(int &t,int data) //这是删除平衡树中某个节点的过程;
{
size[t]--;//因为这个节点肯定在以t为根的树下面。所以删除后,它的大小会递减。
if (key[t] == data)//如果找到了要删除的元素。
{
if (l[t] ==0 && r[t] == 0)//如果它没有左子树或右子树
t = 0;//那么就直接把这个节点置空
else
if (l[t] == 0 && r[t]!=0)//如果左子树为空,右子树不为空
t = r[t];//就把这个节点去掉,用右子树来接在下面。
else
if (l[t] != 0 && r[t] == 0)//如果左子树不为空,右子树为空
t = l[t];//则直接把这个节点去掉,把左子树接在下面。
else
if (l[t]!=0 && r[t]!=0)
{
int temp = r[t];
while (l[temp]) temp = l[temp];
key[t] = key[temp];
de_lete(r[t],key[temp]);
}
}
else
if (data<key[t])
de_lete(l[t],data);
else
de_lete(r[t],data);
}
void work(int x)
{
if (!root)
return;
qianqu = 0;
ask_before(root,x);
if (qianqu!=0)
{
de_lete(root,qianqu);
n--;
}
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
//printf("%d\n",6*sizeof(size)/1024/1024);
// return 0;
scanf("%d",&n);
for (int i = 1;i <= 3;i++)
scanf("%d",&a[i]);
sort(a+1,a+1+3);
for (int i = 1;i <= n;i++)
scanf("%d",&t[i]),insert(root,t[i]);
if (get_max(root)>a[1]+a[2]+a[3])
{
puts("-1");
return 0;
}
int ans = 0;
while (root && n)
{
ans++;
int mx = get_max(root);
if (mx<=a[1])
{
ans--;
ans +=(n+2)/3;
break;
}
else
if (mx<=a[2])
{
work(a[1]);
work(a[2]);
work(a[3]);
}
else
if (mx <=a[3])
{
qianqu = 0;
ask_before(root,a[1]);
bool flag = 1;
if (qianqu!=0)
{
int temp1 = qianqu;
de_lete(root,qianqu);
qianqu = 0;
ask_before(root,a[2]);
if (qianqu!=0)
{
de_lete(root,qianqu);
n-=2;
work(a[3]);
}
else
{
insert(root,temp1);
flag = 0;
}
}
else
flag = 0;
if (!flag)
{
work(a[1]+a[2]);
work(a[3]);
}
}
else
if (mx<=a[1]+a[2])
{
work(a[1]+a[2]);
work(a[3]);
}
else
if (mx <= a[1]+a[3])
{
work(a[1]+a[3]);
work(a[2]);
}
else
if (mx <= a[2]+a[3])
{
work(a[2]+a[3]);
work(a[1]);
}
else
work(a[1]+a[2]+a[3]);
}
printf("%d\n",ans);
return 0;
}
【14.94%】【codeforces 611E】New Year and Three Musketeers的更多相关文章
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 【77.78%】【codeforces 625C】K-special Tables
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【codeforces 754D】Fedor and coupons
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 760A】Petr and a calendar
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 750E】New Year and Old Subsequence
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【22.73%】【codeforces 606D】Lazy Student
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 762B】USB vs. PS/2
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
随机推荐
- [C/C++]_[0基础]_[static_cast,reinterpret_cast,dynimic_cast的使用场景和差别]
场景: 1. C++的对象差别于C的原因是他们能够有继承关系, 方法有重载, 覆盖关系等, 他们的对象内存数据结构因此也比較复杂. 2. 非常多情况下我们须要一个父类来存储子类的指针对象进行通用方法的 ...
- 关于mybatis中,批量增删改查以及參数传递的问题
1.參数传递的问题 大多数情况下,我们都是利用map作为參数,而且大部分情况下都是仅仅有一个參数. 可是,我们也能够利用@param注解,来传入多个參数,此时,mybatis会自己主动将參数封装成ma ...
- 讨论:怎样加快android的开机时间
如题,近期项目须要,须要将android的开机时间大幅缩短,查了下网上资料,作用有限,望有处理过相关问题的兄弟姐妹參与讨论,给予不吝赐教,期待ing
- POJ 2362 Square DFS
传送门:http://poj.org/problem?id=2362 题目大意: 给一些不同长度的棍棒,问是否可能组成正方形. 学习了写得很好的dfs 赶紧去玩博饼了.....晚上三个地方有约.... ...
- 【Codeforces Round #437 (Div. 2) C】 Ordering Pizza
[链接]h在这里写链接 [题意] 给你参赛者的数量以及一个整数S表示每块披萨的片数. 每个参数者有3个参数,si,ai,bi; 表示第i个参赛者它要吃的披萨的片数,以及吃一片第 ...
- ASP.NET配置文件里经常使用到的节点信息
web.config文件是一个XML文件,是以<confirguration>为根结点展开的. 上一面从宏观上解说了一下有关配置的文件的内容,以下是一些有关于配置文件经常使用的操作. ...
- win32程序如何改变字体大小颜色
//设定文字大小和颜色 LOGFONT logfont; //改变输出字体 ZeroMemory(&logfont, sizeof(LOGFONT)); logfont.lfCharSet = ...
- NASM Syntax
NASM has a simplified syntax designed to let the user code with minimum overhead. In its simplest fo ...
- AE内置Command控件使用
樱木 原文 AE内置Command控件使用 直接使用AE内置的Command控件来完成功能 1.拉框放大 /// <summary> /// 放大 /// </summary> ...
- 通过双重for循环来找到JSON中不反复的数据
//通过双重for循环来找到JSON中不反复的数据 var count = 0; for ( i=0; i<json.length; i++) { for ( j=0; j<i; j++) ...