Make Product Equal One CodeForces - 1206B

https://vjudge.net/contest/356807#problem/G

题意

  给定一组数 每次操作可以选定一个数进行+1或者-1,

  至少进行多少次可以让所有的数成绩为1

思路

  1. 负数全部自增变为 -1;正数自减变为 1;0不变;

  2.  -1为偶数个时,0全部变为1;

    -1为奇数个时:

      有0就变一个0为 -1,回到前一种情况

      没有0,变一个 -1为 1,回到前一种情况

  可以多得到最优的cost

代码

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<bitset>
 6 #include<cassert>
 7 #include<cctype>
 8 #include<cmath>
 9 #include<cstdlib>
10 #include<ctime>
11 #include<deque>
12 #include<iomanip>
13 #include<list>
14 #include<map>
15 #include<queue>
16 #include<set>
17 #include<stack>
18 #include<vector>
19 #include <vector>
20 #include <iterator>
21 #include <utility>
22 #include <sstream>
23 #include <limits>
24 #include <numeric>
25 #include <functional>
26 using namespace std;
27 #define gc getchar()
28 #define mem(a) memset(a,0,sizeof(a))
29 //#define sort(a,n,int) sort(a,a+n,less<int>())
30 
31 #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
32 
33 typedef long long ll;
34 typedef unsigned long long ull;
35 typedef long double ld;
36 typedef pair<int,int> pii;
37 typedef char ch;
38 typedef double db;
39 
40 const double PI=acos(-1.0);
41 const double eps=1e-6;
42 const ll mod=1e9+7;
43 const int inf=0x3f3f3f3f;
44 const int maxn=1e5+10;
45 const int maxm=100+10;
46 
47 
48 bool compare(int a, int b)
49 {
50     return a < b;//升序
51 }
52 
53 ll num[100000] = {0};
54 int main()
55 {
56     int n;
57     ll cost_p = 0, cost_n = 0, cost_0 = 0;
58     ll counter = 0;
59     ll cost = 0;
60     cin >> n;
61     for(int i = 1; i<=n;i++)
62     {
63         cin >> num[i];
64     }
65     for(int i = 1;i<=n;i++)
66     {
67         if(num[i] > 0)
68         {
69             cost_p += num[i]-1;
70         }
71         else if(num[i] < 0)
72         {
73             cost_n += -num[i]-1;
74             counter++;
75         }
76         else if(num[i] == 0)
77         {
78             cost_0 += 1;
79         }
80     }
81     if(counter % 2 == 0 || cost_0)
82     {
83         cost = cost_n + cost_p + cost_0;
84     }
85     else
86     {
87         cost = cost_n + cost_p + 2;
88     }
89     cout << cost;
90     return 0;
91 }

 

posted @ 2020-02-16 16:24  YukiRinLL  阅读(64)  评论(0编辑  收藏  举报