Problem B

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 59   Accepted Submission(s) : 27

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

Given three arraies A[],B[],C[], each contains N non-negative integers.You are asked to maxmize the vale:V=max(A[i]+B[j]+C[k]), where 0<i,j,k<=N and i!=j and j!=k and i!=k.

Input

Each case contains 4 lines,

the first line contains an integer N( 3<=N<=10000 ) ,

the second line contains N integers representing array A[],

the third line contains N integers representing array B[],

the fourth line contains N integers representing array C[].

Output

Each case contains a number seperately: the answer V.

Sample Input

3
1 2 3
3 2 1
3 2 1

Sample Output

8

思路:
int dfs(int i,int j,int k)

{

if(A[i].pos!=B[j].pos&&A[i].pos!=C[k].pos&&B[j].pos!=C[k].pos)

return A[i].l+B[j].l+C[k].l;

if(A[i].pos==B[j].pos)  

        return max(dfs(i+1,j,k),dfs(i,j+1,k));  

        if(A[i].pos==C[k].pos)  

        return max(dfs(i+1,j,k),dfs(i,j,k+1));  

        if(C[k].pos==B[j].pos)  

        return max(dfs(i,j+1,k),dfs(i,j,k+1));  

}



#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
using namespace std;
struct node
{
int l;int pos;
};
node A[10001],B[10001],C[10001];
int N;
int cmp(const void *i,const void *j)
{
node *ii=(node *)i,*jj=(node *)j;
return jj->l-ii->l;
}
void input()
{
for(int i=1;i<=N;i++)
{
scanf("%d",&A[i].l);
A[i].pos=i;
}
for(int i=1;i<=N;i++)
{
scanf("%d",&B[i].l);
B[i].pos=i;
}
for(int i=1;i<=N;i++)
{
scanf("%d",&C[i].l);
C[i].pos=i;
}
qsort(A+1,N,sizeof(A[1]),cmp);
qsort(B+1,N,sizeof(A[1]),cmp);
qsort(C+1,N,sizeof(A[1]),cmp);
}
int dfs(int i,int j,int k)
{
if(A[i].pos!=B[j].pos&&A[i].pos!=C[k].pos&&B[j].pos!=C[k].pos)
return A[i].l+B[j].l+C[k].l;
if(A[i].pos==B[j].pos)
return max(dfs(i+1,j,k),dfs(i,j+1,k));
if(A[i].pos==C[k].pos)
return max(dfs(i+1,j,k),dfs(i,j,k+1));
if(C[k].pos==B[j].pos)
return max(dfs(i,j+1,k),dfs(i,j,k+1));
}
void solve()
{
int ans=dfs(1,1,1);
printf("%d\n",ans);
}
int main()
{
while(cin>>N)
{
input();
solve();
}
}

【递归】【3月周赛1】【Problem B】的更多相关文章

  1. codevs http://www.codevs.cn/problem/?problemset_id=1 循环、递归、stl复习题

    12.10高一练习题 1.要求: 这周回顾复习的内容是循环.递归.stl. 不要因为题目简单就放弃不做,现在就是练习基础. 2.练习题: (1)循环   题目解析与代码见随笔分类  NOI题库 htt ...

  2. 【算法】N Queens Problem

    /* ** 目前最快的N皇后递归解决方法 ** N Queens Problem ** 试探-回溯算法,递归实现 */ #include "stdafx.h" #include & ...

  3. n皇后2种解题思路与代码-Java与C++实现

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了n皇后问题的解题思路,并分别用java和c++实现了过程,最后,对于算法改进 ...

  4. Hanoi Tower问题分析

    前言 回家休息第3天了,状态一直不是太好,主要是要补牙,检查身体,见同学见亲戚,心里又着急校招,难得能腾出时间来好好思考,这里也是看<cracking the coding interview& ...

  5. 深入N皇后问题的两个最高效算法的详解 分类: C/C++ 2014-11-08 17:22 117人阅读 评论(0) 收藏

    N皇后问题是一个经典的问题,在一个N*N的棋盘上放置N个皇后,每行一个并使其不能互相攻击(同一行.同一列.同一斜线上的皇后都会自动攻击). 一. 求解N皇后问题是算法中回溯法应用的一个经典案例 回溯算 ...

  6. 【BZOJ1791】【IOI2008】【基环树】island(status第一速度)

      1791: [Ioi2008]Island 岛屿  Time Limit: 20 Sec  Memory Limit: 162 MB Submit: 908  Solved: 159 [Su ...

  7. HDU 1754 I Hate It (段树 &amp; 树阵)

    I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  8. Java 学习笔记 (二) Selenium WebDriver Java 弹出框

    下面这段实例实现了以下功能: 1. profile使用用户本地电脑上的 (selenium 3有问题.因为selenium 3把profile复制到一个temp文件夹里,但并不复制回去.所以每次打开仍 ...

  9. 二叉树/DFS总结

    二叉搜索树(Binary Search Tree,又名排序二叉树,二叉查找树,通常简写为BST)定义如下: 空树或是具有下列性质的二叉树: ()若左子树不空,则左子树上所有节点值均小于或等于它的根节点 ...

随机推荐

  1. #include<math.h>

    1.sin(a)类:a是弧度值: 2.abs(b):结果是b的绝对值: 3.exp(c):exp()用来计算以e为底的x次方值,即ex值,然后将结果返回.返回值: 返回e的x次方计算结果. 4.log ...

  2. android-读取Assets图片资源保存到SD - 随心

    public class ReadBitmap { public void readByte(Context c, String name, int indexInt) { byte[] b = nu ...

  3. 关于mysql binlog日志的格式说明

    Binary Log 记录方式 Row Level Binary Log会记录成每一行数据被修改的形式,然后在Slave端再对相同的数据进行修改. 如果修改了表的结构,那么binlog日志记录的是重新 ...

  4. 网页 php开发中html空文本节点问题user agent stylesheetbody

    最近开发中遇到一个奇怪的问题,我的一个网站头部,代码固定不变,放在了不同的模板进行展示,一部分出现了问题,总是距离相差8个像素,用firebug查看发现:meta 跑到 body 下面去了,并且发现了 ...

  5. js打印数据类型

    console.log({}.toString.call(123))--- [object Number].... [object String] [object Undefined] [object ...

  6. EF Code Frist

    EF:学习资料 http://www.cnblogs.com/libingql/category/366833.html

  7. Reverse Integer (JAVA)

    public class Solution { public int reverse(int x) { StringBuffer sb = new StringBuffer(x+"" ...

  8. ASP.NET MVC 自定义错误页面心得

    自定义错误页面的目的,就是为了能让程序在出现错误/异常的时候,能够有较好的显示体验. 所以,首先要先了解,我们可以在哪里捕获异常. 当程序发生错误的时候,我们可以在两个地方捕获: Global里面的A ...

  9. C#中精确计时的一点收获

    以下所有代码运行环境:Windows 2003, Intel(R) Core(TM) 2 Duo CPU E8400 @  3.00GHz 2.99GHz,2.96GB内存 根据综合网上的一些文章,精 ...

  10. (转)Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds

    仰天长啸 Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds... 当启动tomcat时候出现 S ...