codeforces 1042C Array Product【构造】
题目:戳这里
题意:n个数,两种操作,第一种是a[i]*a[j],删掉a[i],第一种是直接删除a[i](只能用一次)剩下的数序列号不变。操作n-1次,使最后剩下的那个数最大化。
解题思路:
正数之间全用操作1得到的结果最大。
负数的个数如果是偶数,全用操作1最后得到的也最大。如果是奇数,那最大的那个负数(贪心的思想)就要进行特殊操作,具体怎么操作要看后面有没有0,如果有0就用操作1去乘,没有就用操作2直接给这个数删了。
有0的话就把所有的0乘最后一个0,然后把最后一个0删了。
附ac代码:
1 #include <bits/stdc++.h>
2 using namespace std;
3 typedef long long ll;
4 const int maxn = 5e5 + 10;
5 const int inf = 5e5 + 10;
6 struct nod
7 {
8 ll a;
9 int id;
10 }nu[maxn];
11 bool cmp(nod x, nod y)
12 {
13 return x.a < y.a;
14 }
15 int main() {
16 int n;
17 scanf("%d", &n);
18 for(int i = 1; i <= n; ++i)
19 {
20 scanf("%lld", &nu[i].a);
21 nu[i].id = i;
22 }
23 sort(nu + 1, nu + 1 + n, cmp);
24 int cntm = 0;
25 int lastz = 0;
26 int cntop = 0;
27 int firstp = 0;
28 int firstz = 0;
29
30 for(int i = 1; i <= n; ++i)
31 {
32 if(nu[i].a < 0) ++cntm;
33 if(nu[i].a == 0)
34 {
35 if(!firstz) firstz = i;
36 lastz = i;
37 }
38 if(nu[i].a > 0)
39 {
40 firstp = i;
41 break;
42 }
43 }
44 if(cntm & 1)
45 {
46 if(lastz) firstz = cntm;
47 else printf("2 %d\n", nu[cntm].id);
48 --cntm;
49 }
50 for(int i = 1; i < cntm; ++i)
51 {
52 printf("1 %d %d\n", nu[i].id, nu[cntm].id);
53 }
54 for(int i = firstz; i < lastz; ++i)
55 {
56 printf("1 %d %d\n", nu[i].id, nu[i + 1].id);
57 }
58 if(lastz && (cntm || firstp))//这里wa了好多发
59 {
60 printf("2 %d\n", nu[lastz].id);
61
62 }
63 if(firstp)
64 {
65 if(cntm)
66 {
67 printf("1 %d %d\n", nu[cntm].id, nu[n].id);
68 }
69 for(int i = firstp; i < n; ++i)
70 {
71 printf("1 %d %d\n", nu[i].id, nu[n].id);
72 }
73 }
74 return 0;
75 }
codeforces 1042C Array Product【构造】的更多相关文章
- codeforces 1042c// Array Product// Codeforces Round #510(Div. 2)
题意:给出一个数组,2种操作:.1:x*y然后x消失,2:除掉x(2操作最多只能进行一次).问最大的结果的一种操作方式.逻辑题,看能不能想全面. 1先数好0,正,负的数量,zero,pos,neg.如 ...
- Array Product(模拟)
Array Product http://codeforces.com/problemset/problem/1042/C You are given an array aa consisting o ...
- Array Product CodeForces - 1042C (细节)
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> ...
- Makes And The Product CodeForces - 817B (思维+构造)
B. Makes And The Product time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Codeforces F. Maxim and Array(构造贪心)
题目描述: Maxim and Array time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #510 #C Array Product
http://codeforces.com/contest/1042/problem/C 给你一个有n个元素序列,有两个操作:1,选取a[i]和a[j],删除a[i],将$a[i]*a[j]$赋值给a ...
- Codeforces Round #643 (Div. 2) D. Game With Array (思维,构造)
题意:给你两个正整数\(N\)和\(S\),构造一个长度为\(N\)并且所有元素和为\(S\)的正整数数组,问是否能找到一个\(K (0\le K \le S)\)使得这个数组的任意_子数组_的和都不 ...
- Codeforces.487C.Prefix Product Sequence(构造)
题目链接 \(Description\) 对于一个序列\(a_i\),定义其前缀积序列为\(a_1\ \mathbb{mod}\ n,\ (a_1a_2)\ \mathbb{mod}\ n,...,( ...
- Codeforces Round #510 (Div. 2) C. Array Product
题目 题意: 给你n个数,有两种操作,操作1是把第i个位置的数删去, 操作2 是把 a[ j ]= a[ i ]* a[ j ],把a[ i ]删去 .n-1个操作以后,只剩1个数,要使这个数最大 . ...
随机推荐
- 1.2V转3.3V芯片电路图,超简电路
镍氢可充电电池1.2V转成3.3V的电路和电子产品很多,在实际适用中,即使是两节镍氢电池串联供电也是会有供电电压下降和不稳定的影响,这是因为电池电量减少,而导致电池的电压也是会随着降低. 一般情况下, ...
- DSL是什么?Elasticsearch的Query DSL又是什么?
1.DSL简介 DSL 其实是 Domain Specific Language 的缩写,中文翻译为领域特定语言.而与 DSL 相对的就是 GPL,这里的 GPL 并不是我们知道的开源许可证(备注:G ...
- OpenDaylight — YANG
1. 介绍 YANG 是一种用于为 NETCONF 协议建模数据的语言. YANG 将数据的层次结构建模为一棵树. 2. 节点类型 2.1 leaf 它只有一个特定类型的值,并且没有子节点. YANG ...
- docker容器的基本命令
#安装docker yum -y install docker systemctl start docker.service systemctl status docker systemctl e ...
- Linux下双网卡双ip-双外网网关-电信联通双线主机设置
1.实现:通过运营商提供的智能DNS,把电信用户访问时,数据进电信的网卡,出来时也从电信的网关出来,访问联通时,从联通网卡时,联通网卡出.这样速度就会快,实现双线主机的功能. 2.网卡信息:电信IP( ...
- libuv工作队列
目录 1.说明 2.API 2.1.uv_queue_work 2.2.uv_cancel 3.代码示例 1.说明 libuv 提供了一个线程池,可用于运行用户代码,libuv 中的工作队列中的任务会 ...
- 在这个示例中,使用 watch 选项允许我们执行异步操作 (访问一个 API),限制我们执行该操作的频率,并在我们得到最终结果前,设置中间状态。这些都是计算属性无法做到的。
在这个示例中,使用 watch 选项允许我们执行异步操作 (访问一个 API),限制我们执行该操作的频率,并在我们得到最终结果前,设置中间状态.这些都是计算属性无法做到的.
- java8 新特性---列表
- windows提权常用系统漏洞与补丁编号速查对照表
#Security Bulletin #KB #Description #Operating System CVE-2020-0787 [Windows Background Intelligent ...
- 利用powershell隐藏执行后门
运行后门时,有些后门不能中断.双击运行这种后门会产生一个黑框. 一条命令就能使其在后台执行 命令解释: start-process启动一个进程 -windowstyle窗口样式 hidden隐藏