2017-4-25-Train:Codeforces Round #311 (Div. 2)

A. Ilya and Diplomas(贪心)

Soon a school Olympiad in Informatics will be held in Berland, n schoolchildren will participate there.

At a meeting of the jury of the Olympiad it was decided that each of the n participants, depending on the results, will get a diploma of the first, second or third degree. Thus, each student will receive exactly one diploma.

They also decided that there must be given at least min1 and at most max1 diplomas of the first degree, at least min2 and at most max2diplomas of the second degree, and at least min3 and at most max3 diplomas of the third degree.

After some discussion it was decided to choose from all the options of distributing diplomas satisfying these limitations the one that maximizes the number of participants who receive diplomas of the first degree. Of all these options they select the one which maximizes the number of the participants who receive diplomas of the second degree. If there are multiple of these options, they select the option that maximizes the number of diplomas of the third degree.

Choosing the best option of distributing certificates was entrusted to Ilya, one of the best programmers of Berland. However, he found more important things to do, so it is your task now to choose the best option of distributing of diplomas, based on the described limitations.

It is guaranteed that the described limitations are such that there is a way to choose such an option of distributing diplomas that all nparticipants of the Olympiad will receive a diploma of some degree.

Input

The first line of the input contains a single integer n (3 ≤ n ≤ 3·106) — the number of schoolchildren who will participate in the Olympiad.

The next line of the input contains two integers min1 and max1 (1 ≤ min1 ≤ max1 ≤ 106) — the minimum and maximum limits on the number of diplomas of the first degree that can be distributed.

The third line of the input contains two integers min2 and max2 (1 ≤ min2 ≤ max2 ≤ 106) — the minimum and maximum limits on the number of diplomas of the second degree that can be distributed.

The next line of the input contains two integers min3 and max3 (1 ≤ min3 ≤ max3 ≤ 106) — the minimum and maximum limits on the number of diplomas of the third degree that can be distributed.

It is guaranteed that min1 + min2 + min3 ≤ n ≤ max1 + max2 + max3.

Output

In the first line of the output print three numbers, showing how many diplomas of the first, second and third degree will be given to students in the optimal variant of distributing diplomas.

The optimal variant of distributing diplomas is the one that maximizes the number of students who receive diplomas of the first degree. Of all the suitable options, the best one is the one which maximizes the number of participants who receive diplomas of the second degree. If there are several of these options, the best one is the one that maximizes the number of diplomas of the third degree.

Examples

input

6
1 5
2 6
3 7

output

1 2 3

input

10
1 2
1 3
1 5

output

2 3 5

input

6
1 3
2 2
2 2

output

2 2 2

Means:

有三种奖,每种奖有最多发多少最少法多少的限制,现在要给n个人颁奖,要求越高的奖发的越多越好,现在问你每种奖发多少

Solve:

贪心,优先一等奖发最多,再二等奖法最多,再三等奖发最多,发一等奖的时候,二三等奖都先发最少的,然后取一等与二三取最小中的最小

Code:

 1 #pragma comment(linker, "/STACK:36777216")
 2
 3 #include <bits/stdc++.h>
 4 using namespace std;
 5 #define LSON            id << 1 , l , mid
 6 #define RSON            id << 1 | 1 , mid + 1 , r
 7 #define ROOT            1 , 1 , n
 8 #define CLR(x , y)      memset(x , y , sizeof(x))
 9 #define LOWBIT(x)       x & (-x)
10 #define FORN(i , a , n)  for(int i = (a) ; i <= (n) ; ++i)
11 #define FORP(i , n , a)  for(int i = (n) ; i >= (a) ; --i)
12 #define CASE(x)        printf("Case %d: ", x)
13 #define SFD(x)      scanf("%lf" , &x)
14 #define SFC(x)      scanf(" %c" , &x)
15 #define SFS(x)      scanf(" %s" , x)
16 #define SFI(x)      scanf("%d" , &x)
17 #define SFI64(x)    scanf("%I64d" , &x)
18 #define PFF(x)         printf("%f" , x)
19 #define PFD(x)         printf("%lf" , x)
20 #define PFI(x)         printf("%d" , x)
21 #define PFC(x)         printf("%c" , x)
22 #define PFS(x)         printf("%s" , x)
23 #define PFI64(x)       printf("%I64d" , x)
24 #define SPACE          printf(" ")
25 #define PUT            puts("")
26 #define LPUP(i , j , k) for(int i = j ; i <= k ; ++i)
27 #define LPDW(i , j , k) for(int i = j ; i >= k ; --i)
28 #define PB(x)          push_back(x)
29 #define ALL(A)         A.begin(), A.end()
30 #define SZ(A)          int((A).size())
31 #define LBD(A, x)      (lower_bound(ALL(A), x) - A.begin())
32 #define UBD(A, x)      (upper_bound(ALL(A), x) - A.begin())
33 #define LOCAL
34 static const double PI = acos(-1.0);
35 static const double EPS = 1e-8;
36 static const int INF = 0X3fffffff;
37 typedef __int64 LL;
38 typedef double DB;
39 template<class T> inline
40 T read(T &x)
41 {
42     x = 0;
43     int f = 1 ; char ch = getchar();
44     while (ch < ‘0‘ || ch > ‘9‘) {if (ch == ‘-‘) f = -1; ch = getchar();}
45     while (ch >= ‘0‘ && ch <= ‘9‘) {x = x * 10 + ch - ‘0‘; ch = getchar();}
46     x *= f;
47 }
48
49 /************************Little Pea****************************/
50
51 int mx[5];
52 int mi[5];
53 int ans[5];
54 int n;
55 int main()
56 {
57 #ifdef LOCAL
58     //freopen("D:\\系统优化\\Desktop\\littlepea\\in.data" , "r" , stdin);
59 #endif
60     read(n);
61     LPUP(i , 1 , 3)
62     {
63         read(mi[i]);
64         read(mx[i]);
65     }
66     ans[1] = min(mx[1] , n - mi[2] - mi[3]);
67     ans[2] = min(mx[2] , n - ans[1] - mi[3]);
68     ans[3] = n - ans[1] - ans[2];
69     LPUP(i , 1 , 3)
70     {
71         PFI(ans[i]);SPACE;
72     }
73 }

B. Pasha and Tea(贪心 + 思想)

Pasha decided to invite his friends to a tea party. For that occasion, he has a large teapot with the capacity of w milliliters and 2n tea cups, each cup is for one of Pasha‘s friends. The i-th cup can hold at most ai milliliters of water.

It turned out that among Pasha‘s friends there are exactly n boys and exactly n girls and all of them are going to come to the tea party. To please everyone, Pasha decided to pour the water for the tea as follows:

  • Pasha can boil the teapot exactly once by pouring there at most w milliliters of water;
  • Pasha pours the same amount of water to each girl;
  • Pasha pours the same amount of water to each boy;
  • if each girl gets x milliliters of water, then each boy gets 2x milliliters of water.

In the other words, each boy should get two times more water than each girl does.

Pasha is very kind and polite, so he wants to maximize the total amount of the water that he pours to his friends. Your task is to help him and determine the optimum distribution of cups between Pasha‘s friends.

Input

The first line of the input contains two integers, n and w (1 ≤ n ≤ 105, 1 ≤ w ≤ 109) — the number of Pasha‘s friends that are boys (equal to the number of Pasha‘s friends that are girls) and the capacity of Pasha‘s teapot in milliliters.

The second line of the input contains the sequence of integers ai (1 ≤ ai ≤ 109, 1 ≤ i ≤ 2n) — the capacities of Pasha‘s tea cups in milliliters.

Output

Print a single real number — the maximum total amount of water in milliliters that Pasha can pour to his friends without violating the given conditions. Your answer will be considered correct if its absolute or relative error doesn‘t exceed 10 - 6.

Examples

input

2 4
1 1 1 1

output

3

input

3 18
4 4 4 2 2 2

output

18

input

1 5
2 3

output

4.5

Note

Pasha also has candies that he is going to give to girls but that is another task...

Means:

给你N个杯子,每个杯子的最大容量已知,共有W的茶,分给n个女孩和n个男孩,要求:

①给每个女孩的量都要相同。

②给每个男孩的量都要相同。

③给每个女孩的量是每个男孩的量的1/2.

求最大倒多少茶

Solve:

注意,并不是他给出的数据中并没有区分男女,所以,我们贪心的取最小的n个为女生的杯子,剩下的为男生的杯子(很显然要这样贪心,因为男生要取女生两倍并且男生的每杯茶的质量要相等),因为男生跟男生要相等,女生跟女生要相等,所以女生最大可以有的质量肯定是杯子最小的那个人的,男生也是同理,又因为男生是女生的两倍,所以如果女生最小杯子的两倍大于男生最小杯子,那么男生最多能倒的就是男生最小杯子的数量,而茶的最多能倒的总质量的公式又是男生最多能倒的x乘以男生的杯子数,加上女生每人能倒的也就是男生单位数量的一半,所以总的就是:1.5*n*x,但是如果总的能倒的超过总w,那么最大的肯定是w

Code:

 1 #pragma comment(linker, "/STACK:36777216")
 2
 3 #include <bits/stdc++.h>
 4 using namespace std;
 5 #define LSON            id << 1 , l , mid
 6 #define RSON            id << 1 | 1 , mid + 1 , r
 7 #define ROOT            1 , 1 , n
 8 #define CLR(x , y)      memset(x , y , sizeof(x))
 9 #define LOWBIT(x)       x & (-x)
10 #define FORN(i , a , n)  for(int i = (a) ; i <= (n) ; ++i)
11 #define FORP(i , n , a)  for(int i = (n) ; i >= (a) ; --i)
12 #define CASE(x)        printf("Case %d: ", x)
13 #define SFD(x)      scanf("%lf" , &x)
14 #define SFC(x)      scanf(" %c" , &x)
15 #define SFS(x)      scanf(" %s" , x)
16 #define SFI(x)      scanf("%d" , &x)
17 #define SFI64(x)    scanf("%I64d" , &x)
18 #define PFF(x)         printf("%f" , x)
19 #define PFD(x)         printf("%lf" , x)
20 #define PFI(x)         printf("%d" , x)
21 #define PFC(x)         printf("%c" , x)
22 #define PFS(x)         printf("%s" , x)
23 #define PFI64(x)       printf("%I64d" , x)
24 #define SPACE          printf(" ")
25 #define PUT            puts("")
26 #define LPUP(i , j , k) for(int i = j ; i <= k ; ++i)
27 #define LPDW(i , j , k) for(int i = j ; i >= k ; --i)
28 #define PB(x)          push_back(x)
29 #define ALL(A)         A.begin(), A.end()
30 #define SZ(A)          int((A).size())
31 #define LBD(A, x)      (lower_bound(ALL(A), x) - A.begin())
32 #define UBD(A, x)      (upper_bound(ALL(A), x) - A.begin())
33 #define LOCAL
34 static const double PI = acos(-1.0);
35 static const double EPS = 1e-6;
36 static const int INF = 0X3fffffff;
37 typedef __int64 LL;
38 typedef double DB;
39 template<class T> inline
40 T read(T &x)
41 {
42     x = 0;
43     int f = 1 ; char ch = getchar();
44     while (ch < ‘0‘ || ch > ‘9‘) {if (ch == ‘-‘) f = -1; ch = getchar();}
45     while (ch >= ‘0‘ && ch <= ‘9‘) {x = x * 10 + ch - ‘0‘; ch = getchar();}
46     x *= f;
47 }
48
49 /************************Little Pea****************************/
50 static const int MAXN = 2e5 + 10;
51 int n , w;
52 int data[MAXN];
53 int main()
54 {
55 #ifdef LOCAL
56     //freopen("D:\\系统优化\\Desktop\\littlepea\\in.data" , "r" , stdin);
57 #endif
58     read(n);read(w);
59     int ac = n << 1;
60     LPUP(i , 1 , ac)
61     {
62         read(data[i]);
63     }
64     sort(data + 1 , data + 1 + ac);
65     int mi = data[1] << 1 <= data[n + 1] ? data[1] << 1 : data[n + 1];
66     if((DB)w - 1.5 * (DB)n * (DB)mi > EPS)
67     {
68         PFF(1.5 * (DB)n * (DB)mi);
69     }
70     else
71         PFI(w);
72 }

C. Arthur and Table(贪心 + 枚举 + 预处理 + 前缀和 QWQ)

Arthur has bought a beautiful big table into his new flat. When he came home, Arthur noticed that the new table is unstable.

In total the table Arthur bought has n legs, the length of the i-th leg is li.

Arthur decided to make the table stable and remove some legs. For each of them Arthur determined number di — the amount of energy that he spends to remove the i-th leg.

A table with k legs is assumed to be stable if there are more than half legs of the maximum length. For example, to make a table with 5legs stable, you need to make sure it has at least three (out of these five) legs of the maximum length. Also, a table with one leg is always stable and a table with two legs is stable if and only if they have the same lengths.

Your task is to help Arthur and count the minimum number of energy units Arthur should spend on making the table stable.

Input

The first line of the input contains integer n (1 ≤ n ≤ 105) — the initial number of legs in the table Arthur bought.

The second line of the input contains a sequence of n integers li (1 ≤ li ≤ 105), where li is equal to the length of the i-th leg of the table.

The third line of the input contains a sequence of n integers di (1 ≤ di ≤ 200), where di is the number of energy units that Arthur spends on removing the i-th leg off the table.

Output

Print a single integer — the minimum number of energy units that Arthur needs to spend in order to make the table stable.

Examples

input

2
1 5
3 2

output

2

input

3
2 4 4
1 1 1

output

0

input

6
2 2 1 1 3 3
4 3 5 5 2 1

output

8

Means:

神题。。。就是给你一张桌子,有n条腿,你可以锯掉多条腿,使得剩下的长度最长的腿的数量大于总数量的一半

Solve:

只要枚举桌子腿的最高高度,那么我们另最高高度为H,显然大于H的直接锯掉,把大于H的花费加到计数器,然后另小于H的有y个,H有x个,那么有等式,(最高的大于一半嘛),y-x>0所以必须去掉max(y-x+1 , 0)个,然后我们可以预处理出每个大于当前高度的花费,然后每次贪心锯小的的时候,贪心的枚举花费

Code:

  1 #pragma comment(linker, "/STACK:36777216")
  2
  3 #include <bits/stdc++.h>
  4 using namespace std;
  5 #define LSON            id << 1 , l , mid
  6 #define RSON            id << 1 | 1 , mid + 1 , r
  7 #define ROOT            1 , 1 , n
  8 #define CLR(x , y)      memset(x , y , sizeof(x))
  9 #define LOWBIT(x)       x & (-x)
 10 #define FORN(i , a , n)  for(int i = (a) ; i <= (n) ; ++i)
 11 #define FORP(i , n , a)  for(int i = (n) ; i >= (a) ; --i)
 12 #define CASE(x)        printf("Case %d: ", x)
 13 #define SFD(x)      scanf("%lf" , &x)
 14 #define SFC(x)      scanf(" %c" , &x)
 15 #define SFS(x)      scanf(" %s" , x)
 16 #define SFI(x)      scanf("%d" , &x)
 17 #define SFI64(x)    scanf("%I64d" , &x)
 18 #define PFF(x)         printf("%f" , x)
 19 #define PFD(x)         printf("%lf" , x)
 20 #define PFI(x)         printf("%d" , x)
 21 #define PFC(x)         printf("%c" , x)
 22 #define PFS(x)         printf("%s" , x)
 23 #define PFI64(x)       printf("%I64d" , x)
 24 #define SPACE          printf(" ")
 25 #define PUT            puts("")
 26 #define LPUP(i , j , k) for(int i = j ; i <= k ; ++i)
 27 #define LPDW(i , j , k) for(int i = j ; i >= k ; --i)
 28 #define PB(x)          push_back(x)
 29 #define ALL(A)         A.begin(), A.end()
 30 #define SZ(A)          int((A).size())
 31 #define LBD(A, x)      (lower_bound(ALL(A), x) - A.begin())
 32 #define UBD(A, x)      (upper_bound(ALL(A), x) - A.begin())
 33 #define LOCAL
 34 static const double PI = acos(-1.0);
 35 static const double EPS = 1e-6;
 36 static const int INF = 0X3fffffff;
 37 typedef __int64 LL;
 38 typedef double DB;
 39 template<class T> inline
 40 T read(T &x)
 41 {
 42     x = 0;
 43     int f = 1 ; char ch = getchar();
 44     while (ch < ‘0‘ || ch > ‘9‘) {if (ch == ‘-‘) f = -1; ch = getchar();}
 45     while (ch >= ‘0‘ && ch <= ‘9‘) {x = x * 10 + ch - ‘0‘; ch = getchar();}
 46     x *= f;
 47 }
 48
 49 /************************Little Pea****************************/
 50 static const int MAXN = 1e5 + 10;
 51 static const int OO = 0x3fffffff;
 52 LL big[MAXN];
 53 LL num[MAXN];
 54 bool vis[MAXN];
 55 int n;
 56 LL sum;
 57 LL ans;
 58 LL high[MAXN];
 59 LL cost[MAXN];
 60 LL have[MAXN];
 61 struct Node
 62 {
 63     LL l , c;
 64 }data[MAXN];
 65 bool cmp(Node a , Node b)
 66 {
 67     return a.l < b.l;
 68 }
 69 int main()
 70 {
 71 #ifdef LOCAL
 72     //freopen("D:\\系统优化\\Desktop\\littlepea\\in.data" , "r" , stdin);
 73 #endif
 74     read(n);
 75     ans = OO;
 76     LPUP(i , 0 , n - 1)
 77     {
 78         read(data[i].l);
 79         ++num[data[i].l];
 80     }
 81     LPUP(i , 0 , n - 1)
 82     {
 83         read(data[i].c);
 84     }
 85     sort(data , data + n , cmp);
 86     LPDW(i , n - 1 , 1)///预处理高度花费
 87     {
 88         sum += data[i].c;
 89         if(data[i].l != data[i - 1].l)
 90             big[data[i - 1].l] = sum;
 91     }
 92     /*LPUP(i , 0 , n - 1)
 93     {
 94         PFI(big[data[i].l]);PUT;
 95     }*/
 96     ans = big[data[0].l];
 97     ++cost[data[0].c];
 98     LPUP(i , 1 , n - 1)
 99     {
100         if(data[i].l != data[i - 1].l)
101         {
102             sum = big[data[i].l];
103             LL tp = i - num[data[i].l] + 1;
104             if(tp > 0)
105             {
106                 LPUP(j , 1 , 200)
107                 {
108                     if(cost[j])
109                     {
110                         if(cost[j] >= tp)
111                         {
112                             sum += tp * j;
113                             tp = 0;
114                             break;
115                         }
116                         else
117                         {
118                             sum += cost[j] *j;
119                             tp -= cost[j];
120                         }
121                     }
122                 }
123             }
124             ans = min(sum , ans);
125         }
126         ++cost[data[i].c];
127     }
128     PFI64(ans);
129 }

时间: 2024-11-07 15:06:37

2017-4-25-Train:Codeforces Round #311 (Div. 2)的相关文章

Codeforces Round #311 (Div. 2) D. Vitaly and Cycle(二分图染色,奇环)

给定n个点,m条边. 求最少最要加几条边使图中存在奇环,且输出此时加边的方法种数 根据题意,只可能为 0:已经存在奇环,dfs搜到已经染色的且颜色相同 1:判断每个连通块里的 染色黑白色的个数 2 :某个点的 度 > 1 3:0条边 1 #include<cstdio> 2 #include<iostream> 3 #include<queue> 4 #include<vector> 5 #include<stack> 6 #include

Codeforces Round #311 (Div. 2)

D 分4种情况讨论 1 不需要加边 , 就是说原本就有一个奇数环,我们只要跑一次二分图就好了 2 加一条边 , 也就是说存在大于等于3个点的联通块 我们对于这个联通块也跑一遍二分图, 可以知道图中所有的 同颜色染色中的点任意相连,都是一个奇数环,那么对于每个联通分量都有相应的数可以计算 3 加两条边,也就是说没有大于两个点的联通块存在,并且有两个点的联通块存在,答案也是可以计算出来的 e*(n-2) 4 加三条边 那么就可以知道每个联通块只有一个点,答案是 n*(n-1)*(n-2)/6; #i

Codeforces Round #311 (Div. 2) E. Ann and Half-Palindrome (DP+字典树)

题目地址:传送门 先用dp求出所有的符合要求的半回文串,标记出来.然后构造字典树.然后再dfs一遍求出所有节点的子树和,最后搜一遍就能找出第k个来了. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map>

Codeforces Round #311 (Div. 2)--D(图

题意:给出一个图,问最少加多少条边能连出一个奇圈,还要求输出连边的方法种数. 思路:比赛的时候看了一下是图相关的就没多想,也没时间了,其实挺简单的,如果已经有奇圈,那么直接输出0 1,如果最多有两个点相连,那么就是(n-2)×m,就是每条边和另外的任意点连两条边,要么就是没有边,这个就是直接输出结果.. 稍微麻烦一点的是最后一种,联通块有多个点且没有奇圈,这时候需要把联通快黑白染色,同色的点相连就是一个奇圈,dfs的时候统计每个联通块中两种=颜色的个数即可. #include<bits/stdc

Codeforces Round #311 (Div. 2) A,B,C,D,E

A. Ilya and Diplomas 思路:水题了, 随随便便枚举一下,分情况讨论一下就OK了. code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <algorithm> #include <iostream> #include <cstring> #include <cmath> #define inf 1000000

Codeforces Round #311 (Div. 2)A Ilya and Diplomas

[比赛链接]click here~~ [题目大意] n个人,获取一到三等文凭,每门文凭有大小范围,求最后文凭各颁发多少 [解题思路]直接枚举了, 看完题,赶紧写了一发代码,发现居然错过注册时间,系统提示不能提交代码,真是醉了~~,以后还是得提前注册: A题,比较简单: 代码: #include <iostream> #include <bits/stdc++.h> using namespace std; int main() { int n,m; int a1,a2; int b

Codeforces Round #311 (Div. 2) B. Pasha and Tea

[题目链接]click here~~ [题目大意]给你n个boy,n个girl ,然后W表示茶壶的最大容量,然后n个茶杯,每个都有不同的容量,要求boy的茶杯里的茶水是girl的两倍,且boy和boy容量一样,girl和girl 容量一样,问如何倒茶,最大化总的茶量 [解题思路]这道题本来很简单,第一次读题没读明白,以为每个茶杯的茶水都倒满了,然后一想不就是拿最大的计算吗.一交,直接WA,然后仔细读题,发现,每个茶杯的茶水可以选择的倒(坑~啊),那么既然boy和boy,girl和girl的茶水一

Codeforces Round #224 (Div. 2) B 数学推理

挺有意思的一道题目,一开始发现了循环节,做了一下,发现许多小地方要补,比较繁琐,做了几个小时的无用功吧,但是循环节肯定可以只是我写搓了,后来又推了公式,发现可以的 首先当b<x的时候,c--,a--那么对于 a,c来说他们之间的差并没有减小,所以真正起到作用的是b>=x的时候,这个时候只有c--,但是答案要求的 是多少次,在b<x的时候 是要经过一定次数的  w-(x - b)来重新使得b>=x,所以第二部分对答案有影响,但是 设方程的话 就不需要多设一个未知数,因为 第一部分肯定

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/