求个网站直接能看的,关键词录入榜,2021没封的网站有人分享吗,免费seo工具贪心6--整数区间 一、心得 二、题目和分析 给n个区间#xff0c;形式为[a, b]#xff0c;a和b均为整数#xff0c;且a b。求一个最小的整数点的集合#xff0c;使得每个区间至少2个不同的元素(整数点)属于这个集合。求这个集合的元素个数。输入第1行#xff1a;1个整… 贪心6--整数区间 一、心得 二、题目和分析 给n个区间形式为[a, b]a和b均为整数且a b。求一个最小的整数点的集合使得每个区间至少2个不同的元素(整数点)属于这个集合。求这个集合的元素个数。输入第1行1个整数n(1 n 10000)接下来n行每行2个整数表示区间的左右端点a, b(0 a b 10000)输出第1行1个整数表示集合的元素的个数样例输入43 62 40 24 7样例输出4 三、代码和结果 1 #include iostream2 #include algorithm3 using namespace std;4 struct act{5 int begin;6 int end;7 };8 9 int mycmp(const act a,const act b){
10 return a.endb.end;
11 }
12
13 //贪心选最快结束就好
14 int main(){
15 //freopen(in.txt,r,stdin);
16 int n;
17 cinn;
18 act a[1005];
19 for(int i1;in;i){
20 cina[i].begina[i].end;
21 }
22 sort(a1,an1,mycmp);
23 cout排序后的序列endl;
24 for(int i1;in;i){
25 couta[i].begin a[i].endendl;
26 }
27 int total1;
28 int xa[1].end;
29 int ans[1005];
30 ans[total]x;
31 for(int i2;in;i){
32 if(a[i].beginx){
33 total;
34 xa[i].end;
35 ans[total]x;
36 }
37
38 }
39 coutanstotalendl;
40 for(int i1;itotal;i){
41 coutans[i] ;
42 }
43 coutendl;
44
45 return 0;
46 } 转载于:https://www.cnblogs.com/Renyi-Fan/p/7135615.html