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. npm 常用命令详解[转]

    npm是什么 NPM的全称是Node Package Manager,是随同NodeJS一起安装的包管理和分发工具,它很方便让JavaScript开发者下载.安装.上传以及管理已经安装的包. npm ...

  2. java中a++与++a区别

    java中a++与++a区别 a++与++a的区别,如果单独使用没有任何区别,如果在运算中就有区别了,a++是先运算在赋值,而++a是先赋值在运算!! 先看a++的代码哦 class demo1 { ...

  3. Android 四大组件之 Activity

    1 简介 Activity (活动) 即应用程序 显示的 界面.可以通过两种方式 设置显示的内容 1:纯代码方式 2:xml 布局方式 无论哪一种方式,都是通过 setContentView 来设置显 ...

  4. linux查看用户登录信息-w命令

    NAME w - Show who is logged on and what they are doing. SYNOPSIS w - [husfV] [user] DESCRIPTION w di ...

  5. sql server去除重复信息,

    SELECT st_id FROM ( SELECT *,ROW_NUMBER() OVER( PARTITION BY st_code ORDER BY st_code ) AS num FROM ...

  6. VS插件集

    Unit Test Generator  很好用的测试插件 注:在VS2015中,改名为Test generator Nunit extension了. ReSharperPlatformVs11   ...

  7. 解决 Visual Studio 2012 有时不能调试的问题

    有时候发现 Visual Studio 2012 不能调试,有时候又能调试.感觉很烦,今天找到了一个解决办法,我也不知道为什么这样能解决. 问题: 解决:1. 找到 Properties ,双击 2. ...

  8. IOS--工作总结--post上传文件(以流的方式上传)

    1.添加协议 <NSURLConnectionDelegate> 2.创建 @property (nonatomic,retain) NSURLConnection* aSynConnec ...

  9. 198. House Robber,213. House Robber II

    198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...

  10. T - stl 的mapⅡ

    Description Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language th ...