题目:戳这里

题意: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【构造】的更多相关文章

  1. codeforces 1042c// Array Product// Codeforces Round #510(Div. 2)

    题意:给出一个数组,2种操作:.1:x*y然后x消失,2:除掉x(2操作最多只能进行一次).问最大的结果的一种操作方式.逻辑题,看能不能想全面. 1先数好0,正,负的数量,zero,pos,neg.如 ...

  2. Array Product(模拟)

    Array Product http://codeforces.com/problemset/problem/1042/C You are given an array aa consisting o ...

  3. Array Product CodeForces - 1042C (细节)

    #include <iostream> #include <sstream> #include <algorithm> #include <cstdio> ...

  4. 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 ...

  5. Codeforces F. Maxim and Array(构造贪心)

    题目描述: Maxim and Array time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  6. 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 ...

  7. Codeforces Round #643 (Div. 2) D. Game With Array (思维,构造)

    题意:给你两个正整数\(N\)和\(S\),构造一个长度为\(N\)并且所有元素和为\(S\)的正整数数组,问是否能找到一个\(K (0\le K \le S)\)使得这个数组的任意_子数组_的和都不 ...

  8. Codeforces.487C.Prefix Product Sequence(构造)

    题目链接 \(Description\) 对于一个序列\(a_i\),定义其前缀积序列为\(a_1\ \mathbb{mod}\ n,\ (a_1a_2)\ \mathbb{mod}\ n,...,( ...

  9. Codeforces Round #510 (Div. 2) C. Array Product

    题目 题意: 给你n个数,有两种操作,操作1是把第i个位置的数删去, 操作2 是把 a[ j ]= a[ i ]* a[ j ],把a[ i ]删去 .n-1个操作以后,只剩1个数,要使这个数最大 . ...

随机推荐

  1. Pulsar 社区周报|2021-01-11~2021-01-17

    Pulsar 周报由 StreamNative 翻译整理.原文内容来自 StreamNative 官网 Pulsar 周报模块. 本期编辑:Tango@StreamNative. 关于 Apache ...

  2. SpringBoot JPA简单使用

    引自B站楠哥:https://www.bilibili.com/video/BV137411B7vB 一.新建Springboot项目 ​ pom.xml文件 <?xml version=&qu ...

  3. 【IDEA】Lombok--是否值得我们去使用

    官网 https://projectlombok.org/ 简介 Project Lombok is a java library that automatically plugs into your ...

  4. Failed to start ssh.service: Unit not found.

    Failed to start ssh.service: Unit not found. 报错内容: [Centos7@localhost ~]$ service ssh start Redirect ...

  5. TCP/IP建立连接的时候ISN序号分配问题

    初始建立TCP连接的时候的系列号(ISN)是随机选择的,那么这个系列号为什么不采用一个固定的值呢?主要有两方面的原因 防止同一个连接的不同实例(different instantiations/inc ...

  6. tcp服务器

    如同上面的电话机过程一样,在程序中,如果想要完成一个tcp服务器的功能,需要的流程如下: socket创建一个套接字 bind绑定ip和port listen使套接字变为可以被动链接 accept等待 ...

  7. docker镜像加速,docker更换为国内镜像

    docker镜像加速,docker更换为国内镜像 一.使用官方镜像 二.Docker守护进程配置加速器 相关博文原文地址: CSDN:让我思考一下 :docker更换为国内镜像 一.使用官方镜像 Do ...

  8. SpringMVC传递JSON数据

    文章目录 一.前后端传递和接收JSON数据 1:是要Ajax默认格式来传递数据(*) 2:使用application/json格式来传递数据 二.spring-web.xml中需要如下配置 一.前后端 ...

  9. 报表生成工具ireport

    最近又开始学习新的玩意儿了,扒拉扒拉网上的资源,先捣鼓个思维导图.

  10. Spring boot AOP 记录请求日志

    如何将所有的通过url的请求参数以及返回结果都输出到日志中? 如果在controller的类中每个方法名都写一个log输出肯定是不明智的选择. 使用spring的AOP功能即可完成. 1. 在pom. ...