Codeforces 1203E Boxers (greedy)

Topic link: https: //codeforces.com/problemset/problem/1203/E

The meaning of problems: Given n numbers, each transform may be performed once the +1 or -1 (may change), the number n Q by appropriate after conversion in the digital part of the number of mutually differing .

It is still weak chicken Bu Ti ing. This question is on the line as long as the greedy, greedy thing to note is not to hit the mark in the number of storage After each value directly to the existing value. why? In order to facilitate later moved: taking into account the existence of some special cases, such as

Number: 1234567

Number: 2,110,112

For this example, if we changed the original 2,3 3,4 +1 overall, it can be put on the empty patch 4, while a 1 plus 1, the number of this column there are seven different from each other the number, but if you start to lay the mark, it means that we can not make such a move. And this move is greedy implementation.

Specific implementation is another array to build a vis marking, from small to large or from large to small can be traversed, just remember which side to where to start greedy side by, for example, from the words 1-150000, each value of the first see less than 1 it has no vis, vis a word has come to mark the location of their value. Finally, mark complete statistical output on the line again.

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<set>
 4 #include<stdio.h>
 5 #include<string.h>
 6 #include<math.h>
 7 #include<vector>
 8 #include<stdlib.h>
 9 #include<queue>
10 #include<algorithm>
11 #include<map>
12 #include<stack>
13 using namespace std;
14 int n;
15 int a[150010];
view [int16 150010];
17 int main()
18 {
19     scanf("%d",&n);
20     int k;
21     int s=0;
22     for(int i=0;i<n;i++)
23     {
24         scanf("%d",&k);
25         a[k]++;
26     }
27     for(int i=1;i<=150000;i++)
28     {
29         if(a[i])
30         {
31             for(int j=i-1;j<=i+1;j++)
32             {
33                 if(a[i]&&j>0&&!vis[j])
34                 {
35                     vis[j]=1;
36                     a[i]--;
37                 }
38             }
39         }
40     }
41     for(int i=1;i<=150001;i++)
42     {
43         if(vis[i]>0)
44         {
45             s++;
46         }
47     }
48     printf("%d\n",s);
49     return 0;
50 }

 

Guess you like

Origin www.cnblogs.com/forever3329/p/11354454.html