485. Arrays

Time limit per test: 1.75 second(s)
Memory limit: 262144 kilobytes
input: standard
output: standard

You are given a sequence of 3· N integers (X1X2, ·s, X3· N). Create three sequences (A1A2, ·s, AN), (B1B2, ·s, BN) and (C1C2, ·s, CN) such that:

  • each of the integers from 1 to 3· N belongs to exactly one of the sequences AB or C;
  • the value of  is the largest possible.
Input

Constraints on N Constraints on T
1 ≤ N ≤ 10 1 ≤ T ≤ 1000
11 ≤ N ≤ 15 1 ≤ T ≤ 100
16 ≤ N ≤ 20 1 ≤ T ≤ 10
21 ≤ N ≤ 25 T = 1

The input file contains T test cases, all having the same value of N. The first line of the input file contains the integers T and N, constrained as shown in the adjacent table. Each of the following T lines describes one test case and contains 3· N integers, the members of the sequence X. All these values are in the range from 0 to 1000.

Output

The output file should consist of T lines. Each line should contain the largest possible value of S for the corresponding test case from the input file.

Example(s)
sample input
sample output
1 2 4 1 8 2 0 5 
46 

Note. The maximal value is attained by taking A = (1, 3), B = (2, 5), C = (4, 6).

题意:给出一组数,将这些数分成三组,记为A,B,C,求出满足sigma[(Ai-Bi)*Ci]的最大值。

sl: 首先考虑下B ,很显然B中的元素应该是最小的N个,在考虑A,C很容易看出A,C应该是满足

A>=C的关系。

所以我们只需要枚举A,C就好了。

其中包含2个优化

优化1:在计算的过程中应满足全局平均值最小。

优化2:不等式 (ai-bi)*ci+(aj-bj)*cj - {(ai-bi)*aj+(ci-bj)*cj   } >0成立 即满足:

(ci-aj)*(ai-bi-cj)>0 成立。

所以ci>aj 时 ai-bi>cj

ci<aj时 ai-bi<cj

详见代码。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 typedef long long LL;
 6 const int maxn = 76;
 7 int T, N;
 8 int ans, a[maxn], va[maxn], vc[maxn];
 9 bool vis[maxn];
 bool test(int cur) {
     for (int i=1; i<cur; ++i) {
         if (abs(va[cur])>abs(vc[i]) && abs(vc[cur])<abs(va[i])-abs(a[3*N-i+1])) {
             return false;
         }
         if (abs(va[cur])<abs(vc[i]) && abs(vc[cur])>abs(va[i])-abs(a[3*N-i+1])) {
             return false;
         }
     }
     return true;
 }
 void dfs(int cur, int last, int val) {
     if (cur==N+1) {
         ans = max(ans, val);
         return;
     }
     for (int i=cur; i<=2*N; ++i) {
         if (!vis[i]) {
             vis[i] = 1;
             va[cur] = a[i];
             for (int j=max(last+1, i+1); j<=N*2; ++j) {
                 if (!vis[j]) {
                     vc[cur] = a[j];
                     vis[j] = 1;
                     int netVal = val+(a[i]-a[3*N-cur+1])*a[j];
                     if (netVal*N>ans*cur) {
                         if (test(cur)) {
                             dfs(cur+1, j, netVal);
                         }
                     }
                     vis[j] = 0;
                 }
             }
             vis[i] = 0;
             break;
         }
     }
 }
 int main()
 {
     scanf ("%d%d", &T, &N);
     while (T--) {
         for (int i=1; i<=3*N; ++i) {
             scanf ("%d", &a[i]);
             a[i] = -a[i];
         }
         sort(a+1, a+3*N+1);
         ans = 0;
         dfs(1, 0, 0);
         printf ("%d\n", ans);
     }
     return 0;

62 }

SGU 485 Arrays的更多相关文章

  1. Java程序员的日常—— Arrays工具类的使用

    这个类在日常的开发中,还是非常常用的.今天就总结一下Arrays工具类的常用方法.最常用的就是asList,sort,toStream,equals,copyOf了.另外可以深入学习下Arrays的排 ...

  2. 使用 Arrays 类操作 Java 中的数组

    Arrays 类是 Java 中提供的一个工具类,在 java.util 包中.该类中包含了一些方法用来直接操作数组,比如可直接实现数组的排序.搜索等(关于类和方法的相关内容在后面的章节中会详细讲解滴 ...

  3. 【转】java.util.Arrays.asList 的用法

    DK 1.4对java.util.Arrays.asList的定义,函数参数是Object[].所以,在1.4中asList()并不支持基本类型的数组作参数. JDK 1.5中,java.util.A ...

  4. System.arraycopy()和Arrays.copyOf()的区别

    先看看System.arraycopy()的声明: public static native void arraycopy(Object src,int srcPos, Object dest, in ...

  5. 计算机程序的思维逻辑 (31) - 剖析Arrays

    数组是存储多个同类型元素的基本数据结构,数组中的元素在内存连续存放,可以通过数组下标直接定位任意元素,相比我们在后续章节介绍的其他容器,效率非常高. 数组操作是计算机程序中的常见基本操作,Java中有 ...

  6. No.004:Median of Two Sorted Arrays

    问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...

  7. [LeetCode] Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  8. [LeetCode] Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  9. [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

随机推荐

  1. activiti遇到的问题

    1.act_hi_detail表里面没有数据 原因是没有加历史变量的判断 2.流程图添加网关,写流转表达式 比如请假流程   大于3天小于5天的条件:${请假实体类.属性名称}

  2. CSS中路径及form表单的用法

    1.什么是路径? 路劲分为三种 1.绝对路径: 从盘符开始,然后依次的往下查找 本地: C:/Users/Administrator/Desktop/0527day01/07.html 服务器的: w ...

  3. oracle-数据库泵EXPDP导出用户下所有

    1登录sys用户 2创建目录 create directory [dirname] as ‘[dirpath]’; dirname:取的名字 dirpath:dmp文件导出路径 示例:create d ...

  4. 常用JavaScript代码库(又名:WFang.js)

    1.根据公司项目封装ajax请求,结合layer框架一起使用 /*提取接口公共部分*/ var ApiConf = { server:"http://localhost:8080/Batte ...

  5. H5活动的一些事

    ISUX团队镇楼:https://isux.tencent.com/nine-question-of-swipe-html5-page.html IE6.7.8支持html5新元素 : http:// ...

  6. 16 继续讲C#中的条件执行。if...else if...else

    if...else...语句可以让我们判断两种情况.当条件为真的时候,执行一部分:当条件为假的时候,执行另一部分.如果我们需要判断3种,4种,5种情况,那我们应该怎么办呢? 在C#中我们可以 使用if ...

  7. SpringBoot 2.x (1):手动创建项目与自动创建项目

    SpringBoot 2.x基于Spring Framework 5.x 环境需求如下: JDK1.8或以上 Maven3.2或以上 这里我使用的是Eclipse,IDEA这个工具很强大,但不习惯它 ...

  8. 联想 Z5(L78011) 免解锁BL 免rec 保留数据 ROOT Magisk Xposed 救砖 ZUI 10.5.254

    >>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...

  9. 【C++】智能指针简述(六):智能指针总结及补充

    本文我们主要来总结一下前文介绍过的智能指针相关原理及实现,顺便补充一下前文未提到的shared_ptr删除器部分的内容. 总结: 1.智能指针,通过RAII机制,构造对象时完成资源的初始化,析构对象时 ...

  10. [JavaScript] Uncaught TypeError: Method get Set.prototype.size called on incompatible receiver

    在对Set进行方法扩展的时候,无法覆盖size属性 情景:定义一个SingletonSet,继承自Set,size只能为1,并且不能add和remove //首先是extend函数 var exten ...