Problem Statement

     The pony Rainbow Dash wants to choose her pet. There are N animals who want to be her pet. Rainbow Dash numbered them 0 through N-1.

To
help her make the decision, Rainbow Dash decided to organize a relay
race for the animals. The race track is already known, and for each
animal we know how fast it is. More precisely, you are given vector
<int>s A and B with the following meaning: For
each i, the animal number i will take between A[i] and B[i]
seconds (inclusive) to complete the track.

For the race the
animals will be divided into two competing teams. This is a relay
race, so the team members of each team will all run the same track,
one after another -- when the first team member finishes, the second
one may start, and so on. Thus the total time in which a team
completes the race is the sum of the times of all team members. Note
that we can use the estimates given by A and B to
estimate the total time for any team of animals.

Given two
teams S and T, the value maxdiff(S,T) is defined as the largest
possible difference in seconds between the time in which team S
finishes the course and the time in which team T finishes the course.

Rainbow
Dash now needs to assign each of the animals to one of the two
competing teams. She wants to see a close competition, so she wants
the teams to finish as close to each other as possible. Formally,
she wants to divide all animals into teams S and T in a way that
minimizes maxdiff(S,T). Return the smallest possible value of
maxdiff(S,T).

Definition

    
Class: MayTheBestPetWin
Method: calc
Parameters: vector <int>, vector <int>
Returns: int
Method signature: int calc(vector <int> A, vector <int> B)
(be sure your method is public)
    
 

Notes

- The teams are not required to contain the same number of animals.

Constraints

- A will contain between 2 and 50 elements, inclusive.
- A and B will contain the same number of elements.
- Each element of A will be between 1 and 10,000, inclusive.
- Each element of B will be between 1 and 10,000, inclusive.
- For each i, B[i] will be greater than or equal to A[i].

Examples

0)  
    
{3,4,4,7}
{3,4,4,7}
Returns: 2
In this test case we know the exact time in which each of the animals completes the track. An optimal solution is to choose teams S={0,3} and T={1,2}. Then team S will certainly complete the track in 3+7 = 10 seconds, and team T in 4+4 = 8 seconds. Thus, maxdiff(S,T)=2.
1)  
    
{1,3,5,4,5}
{2,5,6,8,7}
Returns: 5
Here one of the optimal solutions is S={2,3} and T={0,1,4}. For these two teams we have maxdiff(S,T)=5. For example, it is possible that S will complete the track in 6+8 = 14 seconds, and T will complete it in 1+3+5 = 9 seconds. It is also possible that S will complete the track up to 5 seconds before T does.
2)  
    
{2907,949,1674,6092,8608,5186,2630,970,1050,2415,1923,2697,5571,6941,8065,4710,716,756,5185,1341,993,5092,248,1895,4223,1783,3844,3531,2431,1755,2837,4015}
{7296,6954,4407,9724,8645,8065,9323,8433,1352,9618,6487,7309,9297,8999,9960,5653,4721,7623,6017,7320,3513,6642,6359,3145,7233,5077,6457,3605,2911,4679,5381,6574}
Returns: 52873
 

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

木想出来

按和来进行背包 这样来看吧 如果按a数组来背 那我们要算出这几个值

s1(前面背出来的和值)    s2(剩余的) a数组

s3(前面背出来的和值)   s4(剩余的) b数组

想求s3-s2  和s1-s4 最值也就从这两种情况里取一个

如果按和背的话 直接就保存了 s1+s3 而(s2+s1)和(s3+s4)是定值 每次背的值 s1+s3-(s2+s1) 这样就出了s3-s2 同样s1-s4也求出来了

so easy...

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
using namespace std;
#define INF 0xfffffff
int dp[];
class MayTheBestPetWin
{
public:
int calc(vector <int> A, vector <int> B)
{
int n = A.size(),s1=,s2=,i,j;
for(i = ; i < n ; i++)
{
s1+=A[i];
s2+=B[i];
A[i]+=B[i];
}
int v = s1+s2;
memset(dp,,sizeof(dp));
dp[] = ;
for(i = ; i < n ; i++)
for(j = v ; j>=A[i] ; j--)
dp[j]=max(dp[j],dp[j-A[i]]);
int ans = INF;
for(i = ; i <= v ; i++)
{
if(dp[i]>)
{
ans = min(ans,max(abs(i-s1),abs(i-s2)));
}
}
return ans;
}
};

TCSRM 593 div2(1000)(dp)的更多相关文章

  1. Codeforces #426 Div2 D(线段树优化 DP )

    #426 Div2 D 题意 给出 \(n\) 个数字,将这些数字隔成 \(k\) 个部分(相对位置不变),统计每个部分有几个不同数字,然后全部加起来求和,问和最大是多少. 分析 很容易想到 \(DP ...

  2. 洛谷3月月赛div2 题解(模拟+数学+贪心+数学)

    由于本人太蒻了,div1的没有参加,胡乱写了写div2的代码就赶过来了. T1 苏联人 题目背景 题目名称是吸引你点进来的. 这是一道正常的题,和苏联没有任何关系. 题目描述 你在打 EE Round ...

  3. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  4. CCPC-Wannafly Winter Camp Day8 (Div2, onsite) A 题 Aqours (精巧的树形DP)

    题目链接: https://www.cometoj.com/contest/29/problem/A?problem_id=414 Aqours 题目描述 Aqours 正在 LoveLive! 决赛 ...

  5. Chrome开发者工具不完全指南:(三、性能篇)

    卤煮在前面已经向大家介绍了Chrome开发者工具的一些功能面板,其中包括Elements.Network.Resources基础功能部分和Sources进阶功能部分,对于一般的网站项目来说,其实就是需 ...

  6. jquery笔记(仅供个人参考)

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  7. 2天驾驭DIV+CSS (实战篇)(转)

     这是去年看到的一片文章,感觉在我的学习中,有不少的影响.于是把它分享给想很快了解css的兄弟们.本文是实战篇. 基础篇[知识一] “DIV+CSS” 的叫法是不准确的[知识二] “DIV+CSS” ...

  8. javascript 函数和作用域(闭包、作用域)(七)

    一.闭包 JavaScript中允许嵌套函数,允许函数用作数据(可以把函数赋值给变量,存储在对象属性中,存储在数组元素中),并且使用词法作用域,这些因素相互交互,创造了惊人的,强大的闭包效果.[upd ...

  9. 集群之mysql主从配置(windows和linux版)

    起因 由于网站进一步开发运行的需求,要求主机7*24小时运行正常,同时要求能够防止数据库灾难.考虑到后期的开发程度和业务量,准备向高可用系统进行改变,同时通过负载均衡提高网络性能.于是第一步就考虑到了 ...

随机推荐

  1. 你用什么方法检查 PHP 脚本的执行效率(通常是脚本执行时间)和数据库 SQL 的效率(通常是数据库 Query 时间), 并定位和分析脚本执行和数据库查询的瓶颈所在?

    php: 一般是在你要检查的代码开头记录一个时间,结尾记录一个时间.取差值, 数据库SQL的效率    sql的explain(mysql),启用slow query log记录慢查询.   通常还要 ...

  2. python 笔记总结

    python  3.5 面向对象:类:具有同种属性的对象称为类,是个抽象的概念.比如说:汽车.人.狗.神:对象:日常生活中的所有东西都是对象,是类的实例化.比如说:推土车是汽车的实例化:姚明是人的实例 ...

  3. 巨大bug

    //数据结构关于课程设计--------图书馆管理系统的设计 #include <stdio.h> #include <stdlib.h> #include <strin ...

  4. 使用maven多模块来构建系统时,spring初始化报错的问题

    最近在实验maven结构的maven工程时,碰到一个问题,springbean总是初始化失败: Related cause: org.springframework.beans.factory.Uns ...

  5. 使用SpringMVC+mybatis+事务控制+JSON 配置最简单WEB

    最近在总结一些项目的基础知识,根据公司最近的一些意向和技术路线,初步整理了一个简单的配置例子     1.使用springmvc代替strutsMVC     2.使用请求json数据串的方式代替传统 ...

  6. 阿里云ubuntu12.04下安装使用mongodb

    阿里云ubuntu12.04下安装mongodb   apt-get install mongodb 阿里云ubuntu12.04下卸载mongodb,同时删除配置文件     apt-get pur ...

  7. unit3d 4.6 document open solution

    发现4.6 的 本地 文档字体解析采用 fonts.googleapis.com ,可是google 自古就与天朝不在一个鼻孔,所以你打开往往需要半天. 网上查了一下,把所有本地的*.html 文档  ...

  8. ***解决PHP输出多余的空格或换行

    用CI框架写APP后台接口的时候,返回的JSON前面有多余的2哥换行,首先排查的是BOM,结果问题依旧 再就是排查<?php ?> 标签外没有多余的回车.换行,结果发现确实有多余的换行,去 ...

  9. 简单的线程同步问题:两个线程交替执行N次【Synchronized、Lock、ArrayBlockingQueue】

    方法一:传统的线程方法import org.apache.log4j.Logger; /** * 两个线程执行的代码片段要实现同步互斥的效果,它们必须用同一个Lock对象.<br/> * ...

  10. RMQ和LCA

    RMQ(Range Minimum/Maximum Query),即区间最值查询 查询很多的时候求[l,r]的最大值可以弄一个数组f[i,j]表示i~j的最大值 //这个是线段树 rmq是f[i,j] ...