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 }
Make Product Equal One CodeForces - 1206B的更多相关文章
- Queries about less or equal elements CodeForces - 600B(二分)
You are given two arrays of integers a and b. For each element of the second arraybj you should find ...
- 题解 CF1206B 【Make Product Equal One】
感谢 @一个低调的人 (UID=48417) 题目: CodeForces链接 Luogu链接 思路: 这是一个一眼题 我们不妨把所有的数都看做是\(1\)(取相应的花费,如:\(6\) 的花费就是\ ...
- Many Equal Substrings CodeForces - 1029A (kmp next数组应用)
题目大意 题目看样例也能猜到就是输出最短的循环串. 吐槽 明明是div3第一题为啥子还会用到kmp的知识? 解法 这个题仔细看发现是求最长可去除的后缀,也就是说去除跟下一个相同的字符串还能连接起来.这 ...
- Product Oriented Recurrence(Codeforces Round #566 (Div. 2)E+矩阵快速幂+欧拉降幂)
传送门 题目 \[ \begin{aligned} &f_n=c^{2*n-6}f_{n-1}f_{n-2}f_{n-3}&\\ \end{aligned} \] 思路 我们通过迭代发 ...
- Codeforces Round #580 (Div. 2)
这次比上次多A了一道,但做得太慢,rating还是降了. Problem A Choose Two Numbers 题意:给出两个集合A,B,从A,B中分别选出元素a,b使得a+b既不属于集合A,又不 ...
- mongodb3.2系统性学习——4、find()操作
find 操作语法展示: find()操作实例 : //连接数据库 dbService = connect("localhost:27017"); //选择插入集合 db = db ...
- geeksforgeeks@ Equal to product (Binary Search)
http://www.practice.geeksforgeeks.org/problem-page.php?pid=667 Equal to product Given an array of in ...
- Educational Codeforces Round 7 C. Not Equal on a Segment 并查集
C. Not Equal on a Segment 题目连接: http://www.codeforces.com/contest/622/problem/C Description You are ...
- Educational Codeforces Round 2 B. Queries about less or equal elements 水题
B. Queries about less or equal elements Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforc ...
- Codeforces gym101612 E.Equal Numbers(贪心)
传送:http://codeforces.com/gym/101612 题意:给出一个大小为n的序列a[i],每次选其中一个数乘以一个正整数,问进行k步操作后最少剩下多少种数字,输出0≤k≤n,所有的 ...
随机推荐
- MyBatis常见面试题:Dao接口的工作原理是什么?Dao接口里的方法,参数不同时,方法能重载吗?
MyBatis常见面试题:通常一个Xml映射文件,都会写一个Dao接口与之对应,请问,这个Dao接口的工作原理是什么?Dao接口里的方法,参数不同时,方法能重载吗? Dao接口即Mapper接 ...
- 基于TCP实现简单的聊天室
原文出处:<Go 语言编程之旅>第四章4.1节 基于TCP的聊天室 1.服务端 新用户到来,生成一个User的实例,代表该用户. type User struct{ ID int // 用 ...
- gitlabci之gitlab runner部署配置
gitlab runner部署 部署方案可以采用gitlab runner operator部署,也可以直接使用gitlab helm charts部署. runner配置说明 对于k8s gitla ...
- WDA SEARCH step by step
之前写了不少的东西,其实大多数都是给自己看的,我的习惯是把资料放到网上,用的时候直接看博客. 之前硬盘轻轻摔了一下,几年的资料没了,然后就再也不用硬盘了. 昨天有人突然问我关于WDA的问题,毕竟奇怪, ...
- 几分钟了解下java虚拟机--01
JDK, JRE, JVM的关系 ⚙ 解释器: 逐行转换字节码为机器码 即时编译器(JIT):将热点代码(经常执行的代码段)编译成高效的本地机器码,并缓存起来以供后续直接执行 Just-In-Time ...
- adobe破解工具
Adobe GenP激活器是个适用Adobe产品激活工具,激活所有Adobe软件 下载 搜素GenP 或 点击这里去官网 下载. 参考 https://blog.csdn.net/Cappuccino ...
- rollupjs
掉落神坛的webpack webpack诞生之初的根本原因就是处理前端js模块化的工具. 如果浏览器本身慢慢的已经支持了模块化. 那么webpack存在的意义就不大了. webpack的其它瑕疵 在w ...
- HDU7458 旅行 题解
前言 感觉是非常优秀的题目,写一篇题解记录一下. HDU-7458 旅行(on Vjudge) 题面 题目描述 有一棵 \(n\) 个结点的无根树,每个结点都有对应的类型 \(c_i\) 和权重 \( ...
- 前端开发系列130-进阶篇之TS、Class and ES5
本文讨论Typescript中的Class同ES5构造函数的对应关系,涉及TypeScript的诸多语法.构造函数.面向对象以及原型对象等相关知识点细节,本文只简单对比并不进行深入展开. TypeSc ...
- jz2440 环境搭建
2.搭建三者互通 1.搭建TFTP服务 这两点搞定基本可以飞奔了 记录一下 配置 板子的ip ifconfig eth3 IP地址 不用重启network服务因为也没有这个服务 当然虚拟机里面的一样 ...