CodeForces 998B Cutting(贪心)

时间:2019-10-20
本文章向大家介绍CodeForces 998B Cutting(贪心),主要包括CodeForces 998B Cutting(贪心)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

https://codeforces.com/problemset/problem/998/B

 简单贪心题

代码如下:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <sstream>
13 const int INF=0x3f3f3f3f;
14 typedef long long LL;
15 const int mod=1e9+7;
16 //const double PI=acos(-1);
17 #define Bug cout<<"---------------------"<<endl
18 const int maxn=1e5+10;
19 using namespace std;
20 
21 int a[105];
22 int b[105];//如果可分,存放差值 
23 
24 int  main()
25 {
26     int n,m;
27     scanf("%d %d",&n,&m);
28     for(int i=0;i<n;i++)
29     {
30         scanf("%d",&a[i]);
31     }
32     int x=0;//前面奇数的个数 
33     int y=0;//前面偶数的个数 
34     int cnt=0;//b数组下标计数器 
35     for(int i=2;i!=n;i+=2)
36     {
37         a[i-2]%2==1? x++ : y++;
38         a[i-1]%2==1? x++ : y++;
39         if(x==y)//该数后面的间隔可分 
40             b[cnt++]=max(a[i],a[i-1])-min(a[i],a[i-1]);
41     }
42     sort(b,b+cnt);
43     int num=0;
44     for(int i=0;i<cnt;i++)
45     {
46         if(m>=b[i])
47         {
48             num++;
49             m-=b[i];
50         }
51         else
52             break;
53     }
54     printf("%d\n",num);
55     return 0;
56 }

原文地址:https://www.cnblogs.com/jiamian/p/11708201.html