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. IOS 快速排序法

    - (NSMutableArray *)QuickSort:(NSMutableArray *)list StartIndex:(NSInteger)startIndex EndIndex:(NSIn ...

  2. Comet学习资料

    什么是Comet: http://baike.baidu.com/view/577938.htm?fr=ala0_1 Comet介绍: http://www.ibm.com/developerwork ...

  3. sql语句收集

    一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备 ...

  4. Ubuntu14.04搭建安装svnserver

    前两天,公司准备搭建一个svnserver,供大家使用.于是.就先装了一个Ubuntu系统,然后搭建了svnserver的环境.以下把svn搭建的详细过程描写叙述下: 1.安装svn sudo apt ...

  5. VisualStudio.DTE 对象可以通过检索 GetService() 方法

    DTE dte = (DTE)GetService(typeof(DTE)); string solutionDir = System.IO.Path.GetDirectoryName(dte.Sol ...

  6. SQL Server 添加一条数据获取自动增长列的几种方法

      数据库表设计  邓老师(老邓教的) insert into TestOne ','Test011') select @@IDENTITY as '自动增长ID' 杨老师(老杨教的) insert ...

  7. github教程--廖雪峰的官方网站

    http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

  8. EntityFramework sum嵌套

    一个查询中 用到了 sum , 可是返回结果的小数有很多位 , 都不准确了..类似js中的小数运算一样...不太熟悉C#,不知道这问题是因为double的关系 , 还是因为代码写的问题 , 通过 sq ...

  9. VS C4819 编译错误解决方法

    偶尔用别人的代码,出现: warning C4819: The file contains a character that cannot be represented ). Save the fil ...

  10. GDAL显示线性shp文件

    http://pan.baidu.com/s/1qWIDphU  (工程文件在vs2008中编写) 1.使用到的技术 GDAL:读取矢量数据 GDI:    绘制矢量数据 2.详细解释 GDI绘图: ...