SGU 485 Arrays
485. Arrays
Memory limit: 262144 kilobytes
output: standard
You are given a sequence of 3· N integers (X1, X2, ·s, X3· N). Create three sequences (A1, A2, ·s, AN), (B1, B2, ·s, BN) and (C1, C2, ·s, CN) such that:
- each of the integers from 1 to 3· N belongs to exactly one of the sequences A, B or C;
- the value of
is the largest possible.
| 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.
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.
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的更多相关文章
- Java程序员的日常—— Arrays工具类的使用
这个类在日常的开发中,还是非常常用的.今天就总结一下Arrays工具类的常用方法.最常用的就是asList,sort,toStream,equals,copyOf了.另外可以深入学习下Arrays的排 ...
- 使用 Arrays 类操作 Java 中的数组
Arrays 类是 Java 中提供的一个工具类,在 java.util 包中.该类中包含了一些方法用来直接操作数组,比如可直接实现数组的排序.搜索等(关于类和方法的相关内容在后面的章节中会详细讲解滴 ...
- 【转】java.util.Arrays.asList 的用法
DK 1.4对java.util.Arrays.asList的定义,函数参数是Object[].所以,在1.4中asList()并不支持基本类型的数组作参数. JDK 1.5中,java.util.A ...
- System.arraycopy()和Arrays.copyOf()的区别
先看看System.arraycopy()的声明: public static native void arraycopy(Object src,int srcPos, Object dest, in ...
- 计算机程序的思维逻辑 (31) - 剖析Arrays
数组是存储多个同类型元素的基本数据结构,数组中的元素在内存连续存放,可以通过数组下标直接定位任意元素,相比我们在后续章节介绍的其他容器,效率非常高. 数组操作是计算机程序中的常见基本操作,Java中有 ...
- 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 ...
- [LeetCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- [LeetCode] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- [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 ...
随机推荐
- java 线程开元篇
学习java的读者都知道,Java的每个对象都会有默认的12个方法,这12个方法分别是 object() finalize() hashCode() equals() wait() wait(long ...
- Syntax error on token ";", , expected 错误
eclipse错误提示如图: 错误代码如图: 一开始百思不得其解,后来终于发现问题的原因所在,java中变量的声明可以不在方法中,但语句只能出现在方法中,可以再声明变量的时候就赋初值,但如果要单独赋值 ...
- Java实现日期时间对象的使用
利用类对象计算日期 在利用Java语言进行信息系统开发中,经常需要对日期进行计算和转换,比如,设置了某活动的开始日期和结束日期,系统需要判断当前是否是该活动时间,在Java开发的信息系统中,通常日期以 ...
- 第八届河南省省赛 A.挑战密室
挑战密室 时间限制: ms | 内存限制: KB 难度: 描述 R组织的特工Dr. Kong 为了寻找丢失的超体元素,不幸陷入WTO密室.Dr. Kong必须尽快找到解锁密码逃离,否则几分钟之后,WT ...
- Hive扩展功能(九)--Hive的行级更新操作(Update)
软件环境: linux系统: CentOS6.7 Hadoop版本: 2.6.5 zookeeper版本: 3.4.8 主机配置: 一共m1, m2, m3这三部机, 每部主机的用户名都为centos ...
- IBatis的分页研究
IBatis的分页研究 博客分类: Ibatis学习 摘自: http://cpu.iteye.com/blog/311395 yangtingkun Oracle分页查询语句 ibaits. ...
- HTML——meta
http://www.cnblogs.com/jr1993/p/4542862.html
- 如何在mybatis中引用java中的常量和方法
转自:http://www.68idc.cn/help/jiabenmake/qita/20140821125261.html 在mybatis的映射xml文件调用java类的方法: 1. SELEC ...
- CLISTCTRL2
回顾: 刚刚写完,因为是分期写的,所以最初想好好做一下的文章格式半途而废了~说的也许会有点啰嗦,但是所有的基础用到的技术细节应该都用到了. 如果还有什么疑问,请回复留言,我会尽力解答. 如果有错误,请 ...
- vue基础---实例
(1)数据和方法 ①响应式双向绑定 当一个 Vue 实例被创建时,它向 Vue 的响应式系统中加入了其 data 对象中能找到的所有的属性.当这些属性的值发生改变时,视图将会产生“响应”,即匹配更新为 ...