2017ACM/ICPC广西邀请赛 Duizi and Shunzi

题意:就是一个集合分开,有两种区分 对子:两个相同数字,顺子:连续三个不同数字,问最多分多少个

解法:贪心,如果当前数字不构成顺子就取对子 /2,如果可以取顺子,那么先取顺子再取对子

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <vector>
 4 #include <string.h>
 5 #include <map>
 6 using namespace std;
 7 struct Node{
 8     int x,y;
 9 }node[123];
10 map<int,int>Mp;
11 int n;
12 map<int,int>::iterator it;
13 int main(){
14     while(~scanf("%d",&n)){
15         Mp.clear();
16         int num;
17         int sum=0;
18         for(int i=0;i<n;i++){
19             scanf("%d",&num);
20             Mp[num]++;
21         }
22         int cnt=0;
23         for(int i=1;i<=n;i++){
24             int ans=i-1;
25             int cos=i-2;
26             if(Mp[ans]&&Mp[cos]&&Mp[i]){
27                 while(Mp[ans]&&Mp[cos]&&Mp[i]){
28                     //cout<<ans<<" "<<cos<<" "<<i<<endl;
29                     Mp[ans]--;
30                     Mp[cos]--;
31                     Mp[i]--;
32                     cnt++;
33                 }
34                     cnt+=Mp[i]/2;
35                     Mp[i]%=2;
36             }else{
37                     cnt+=Mp[i]/2;
38                     Mp[i]%=2;
39             }
40         }
41         printf("%d\n",cnt);
42     }
43     return 0;
44 }

 

转载于:https://www.cnblogs.com/yinghualuowu/p/7458777.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值