Codeforces Round #346 (Div. 2)C. Tanya and Toys
地址:http://codeforces.com/problemset/problem/659/C
题目:
In Berland recently a new collection of toys went on sale. This collection consists of 109 types of toys, numbered with integers from 1 to 109. A toy from the new collection of the i-th type costs i bourles.
Tania has managed to collect n different types of toys a1, a2, ..., an from the new collection. Today is Tanya's birthday, and her mother decided to spend no more than m bourles on the gift to the daughter. Tanya will choose several different types of toys from the new collection as a gift. Of course, she does not want to get a type of toy which she already has.
Tanya wants to have as many distinct types of toys in her collection as possible as the result. The new collection is too diverse, and Tanya is too little, so she asks you to help her in this.
The first line contains two integers n (1 ≤ n ≤ 100 000) and m (1 ≤ m ≤ 109) — the number of types of toys that Tanya already has and the number of bourles that her mom is willing to spend on buying new toys.
The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the types of toys that Tanya already has.
In the first line print a single integer k — the number of different types of toys that Tanya should choose so that the number of different types of toys in her collection is maximum possible. Of course, the total cost of the selected toys should not exceed m.
In the second line print k distinct space-separated integers t1, t2, ..., tk (1 ≤ ti ≤ 109) — the types of toys that Tanya should choose.
If there are multiple answers, you may print any of them. Values of ti can be printed in any order.
3 7
1 3 4
2
2 5
4 14
4 6 12 8
4
7 2 3 1
In the first sample mom should buy two toys: one toy of the 2-nd type and one toy of the 5-th type. At any other purchase for 7 bourles (assuming that the toys of types 1, 3 and 4 have already been bought), it is impossible to buy two and more toys.
思路:哈希数组+贪心;用哈希数组储存所有玩具,已拥有就设为1,没有就为0.然后从1开始扫描数组,知道钱不够两人。
ps:数组大小取到10的6次方就可以,想想为什么?
因为n最大为100 000,m最大为10 0000 0000;所以大于1000 000的都不用管了
代码:
1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cmath> 5 #include <cstring> 6 #include <queue> 7 #include <stack> 8 #include <map> 9 #include <vector> 10 11 using namespace std; 12 #define N 10000000 13 int have[N]; 14 int main (void) 15 { 16 int n, temp, t; 17 long long cost,m; 18 memset(have, 0, sizeof(have)); 19 cin >> n >> m; 20 for (int i = 1; i <= n; i++) 21 { 22 scanf("%d", &temp); 23 if( temp < N) 24 have[temp] = 1; 25 } 26 cost = 0; 27 n=0; 28 for (int i = 1; cost <= m && i < N; i++) 29 if (!have[i]) 30 { 31 cost += i; 32 if (cost > m) 33 { 34 break; 35 } 36 have[i] = 1; 37 n++; 38 } 39 else 40 have [i] =0; 41 cout << n << endl; 42 cost = 1; 43 for (temp = 1, t = 0; t < n && temp < N; temp++) 44 if (have[temp]) 45 { 46 if(cost) 47 { 48 cost = 0; 49 } 50 else 51 printf(" "); 52 printf("%d", temp); 53 t++; 54 } 55 cout<<endl; 56 return 0; 57 }
作者:weeping
出处:www.cnblogs.com/weeping/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。