java-线程池(二)

Background

Some concepts in Mathematics and Computer Science are simple in one or two dimensions but become more complex when extended to arbitrary dimensions. Consider solving differential equations in several dimensions
and analyzing the topology of an n-dimensional hypercube. The former is much more complicated than its one dimensional relative while the latter bears a remarkable resemblance to its ``lower-class‘‘ cousin.

The Problem

Consider an n-dimensional ``box‘‘ given by its dimensions. In two dimensions the box (2,3) might represent a box with length 2 units and width 3 units. In three dimensions the box (4,8,9) can represent
a box (length, width, and height). In 6 dimensions it is, perhaps, unclear what the box (4,5,6,7,8,9) represents; but we can analyze
properties of the box such as the sum of its dimensions.

In this problem you will analyze a property of a group of n-dimensional boxes. You are to determine the longest nesting string of boxes, that is a sequence of boxes  such
that each box  nests in box (  .

A box D = (  ) nests in a box E = (  )
if there is some rearrangement of the  such that when rearranged each dimension is less than the corresponding dimension in box
E. This loosely corresponds to turning box D to see if it will fit in box E. However, since any rearrangement suffices, box D can be contorted, not just turned (see examples below).

For example, the box D = (2,6) nests in the box E = (7,3) since D can be rearranged as (6,2) so that each dimension is less than the corresponding dimension in E. The box D = (9,5,7,3) does NOT nest in the box E
= (2,10,6,8) since no rearrangement of D results in a box that satisfies the nesting property, but F = (9,5,7,1) does nest in box E since F can be rearranged as (1,9,5,7) which nests in E.

Formally, we define nesting as follows: box D = (  ) nests in box E = (  )
if there is a permutation  of  such
that (  ) ``fits‘‘ in (  )
i.e., if for all  .

The Input

The input consists of a series of box sequences. Each box sequence begins with a line consisting of the the number of boxes k in the sequence followed by the dimensionality of the boxes, n (on
the same line.)

This line is followed by k lines, one line per box with the n measurements of each box on one line separated by one or more spaces. The  line
in the sequence (  ) gives the measurements for the  box.

There may be several box sequences in the input file. Your program should process all of them and determine, for each sequence, which of the k boxes determine the longest nesting string and the length of
that nesting string (the number of boxes in the string).

In this problem the maximum dimensionality is 10 and the minimum dimensionality is 1. The maximum number of boxes in a sequence is 30.

The Output

For each box sequence in the input file, output the length of the longest nesting string on one line followed on the next line by a list of the boxes that comprise this string in order. The ``smallest‘‘ or ``innermost‘‘
box of the nesting string should be listed first, the next box (if there is one) should be listed second, etc.

The boxes should be numbered according to the order in which they appeared in the input file (first box is box 1, etc.).

If there is more than one longest nesting string then any one of them can be output.

Sample Input

5 2
3 7
8 10
5 2
9 11
21 18
8 6
5 2 20 1 30 10
23 15 7 9 11 3
40 50 34 24 14 4
9 10 11 12 13 14
31 4 18 8 27 17
44 32 13 19 41 19
1 2 3 4 5 6
80 37 47 18 21 9

Sample Output

5
3 1 2 4 5
4
7 2 5 6

这道题有两种解决办法。第一种可以看做是最长增子串问题的变形,第二种情况这是DAG上的最短路径。我采用的是第一种方法。

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<fstream>
using namespace std;
#define MAXSIZE 1000

int k,n;   //k表示有多少组
//int a[MAXSIZE][MAXSIZE];
vector<vector<int> > vec;
vector<vector<int> > old_vec;  //用来记录没排序的串

int d[MAXSIZE];  //用来描述状态的长度
int result[MAXSIZE][MAXSIZE];   //用来描述结果包含哪几项

int match[MAXSIZE];  //用来记录新串在旧串中的位子

bool compare(vector<int> vec1,vector<int> vec2)
{
    int i;
    for(i=0;i<n;i++)
    {
        if(vec1[i]==vec2[i])
            continue;
        return vec1[i]<vec2[i];
    }
    return  false;

}

bool IFTRUE(vector<int> vec1,vector<int> vec2)  //用来判断vec1嵌套在vec2中
{
    int i;
    for(i=0;i<n;i++)
        if(vec1[i]>=vec2[i])
            return false;
    return true;
}
bool operator==(vector<int> vec1,vector<int> vec2)
{
    unsigned int _size=vec1.size();
    for(unsigned int i=0;i<_size;i++)
    {
        if(vec1[i]!=vec2[i])
            return false;
    }
    return true;
}

int main()
{
    //freopen("a.txt","r",stdin);
    //fstream fcout("b.txt");
    int i,j,temp,max_size,flag,m;
    while(cin>>k)
    {
        cin>>n;
        for(i=0;i<k;i++)   //a[0]应当去掉,题目要求从1开始计算
        {
            vector<int> it;
            for(j=0;j<n;j++)
            {
                cin>>temp;
                it.push_back(temp);
            }
            sort(it.begin(),it.end());  //将box内元素升序排列
            old_vec.push_back(it);
            vec.push_back(it);
        }
        //将整个box串重新排序,这样有利于转化为最长增子串问题,同时还要记录新串与原串之间的对应关系
        sort(vec.begin(),vec.end(),compare);
        for(i=0;i<k;i++)
            for(j=0;j<k;j++)
                if(vec[i]==old_vec[j])
                {
                     match[i]=j;
                     break;
                }
        //准备工作已做完,现在开始模拟最长增子序列
        d[0]=1;
        result[0][0]=0;
        max_size=1;
        flag=0;
        for(i=1;i<k;i++)
        {
            d[i]=1;
            for(j=0;j<i;j++)
            {
                if(IFTRUE(vec[j],vec[i])&&d[j]+1>d[i])
                {
                    d[i]=d[j]+1;
                    for(m=0;m<d[j];m++)
                        result[i][m]=result[j][m];
                    result[i][m]=i;
                }
            }
            if(d[i]>max_size)
            {
                max_size=d[i];
                flag=i;
            }
        }
        cout<<max_size<<endl;
        for(i=0;i<max_size;i++)
        {
            if(i!=max_size-1)
                cout<<match[result[flag][i]]+1<<"  ";
            else
                cout<<match[result[flag][i]]+1<<endl;

        }
        vec.clear();
        old_vec.clear(); //必须删除掉,不然会影响下一组数据
    }
    return 0;
}

 Stacking Boxes 

Background

Some concepts in Mathematics and Computer Science are simple in one or two dimensions but become more complex when extended to arbitrary dimensions. Consider solving differential equations in several dimensions
and analyzing the topology of an n-dimensional hypercube. The former is much more complicated than its one dimensional relative while the latter bears a remarkable resemblance to its ``lower-class‘‘ cousin.

The Problem

Consider an n-dimensional ``box‘‘ given by its dimensions. In two dimensions the box (2,3) might represent a box with length 2 units and width 3 units. In three dimensions the box (4,8,9) can represent
a box (length, width, and height). In 6 dimensions it is, perhaps, unclear what the box (4,5,6,7,8,9) represents; but we can analyze
properties of the box such as the sum of its dimensions.

In this problem you will analyze a property of a group of n-dimensional boxes. You are to determine the longest nesting string of boxes, that is a sequence of boxes  such
that each box  nests in box (  .

A box D = (  ) nests in a box E = (  )
if there is some rearrangement of the  such that when rearranged each dimension is less than the corresponding dimension in box
E. This loosely corresponds to turning box D to see if it will fit in box E. However, since any rearrangement suffices, box D can be contorted, not just turned (see examples below).

For example, the box D = (2,6) nests in the box E = (7,3) since D can be rearranged as (6,2) so that each dimension is less than the corresponding dimension in E. The box D = (9,5,7,3) does NOT nest in the box E
= (2,10,6,8) since no rearrangement of D results in a box that satisfies the nesting property, but F = (9,5,7,1) does nest in box E since F can be rearranged as (1,9,5,7) which nests in E.

Formally, we define nesting as follows: box D = (  ) nests in box E = (  )
if there is a permutation  of  such
that (  ) ``fits‘‘ in (  )
i.e., if for all  .

The Input

The input consists of a series of box sequences. Each box sequence begins with a line consisting of the the number of boxes k in the sequence followed by the dimensionality of the boxes, n (on
the same line.)

This line is followed by k lines, one line per box with the n measurements of each box on one line separated by one or more spaces. The  line
in the sequence (  ) gives the measurements for the  box.

There may be several box sequences in the input file. Your program should process all of them and determine, for each sequence, which of the k boxes determine the longest nesting string and the length of
that nesting string (the number of boxes in the string).

In this problem the maximum dimensionality is 10 and the minimum dimensionality is 1. The maximum number of boxes in a sequence is 30.

The Output

For each box sequence in the input file, output the length of the longest nesting string on one line followed on the next line by a list of the boxes that comprise this string in order. The ``smallest‘‘ or ``innermost‘‘
box of the nesting string should be listed first, the next box (if there is one) should be listed second, etc.

The boxes should be numbered according to the order in which they appeared in the input file (first box is box 1, etc.).

If there is more than one longest nesting string then any one of them can be output.

Sample Input

5 2
3 7
8 10
5 2
9 11
21 18
8 6
5 2 20 1 30 10
23 15 7 9 11 3
40 50 34 24 14 4
9 10 11 12 13 14
31 4 18 8 27 17
44 32 13 19 41 19
1 2 3 4 5 6
80 37 47 18 21 9

Sample Output

5
3 1 2 4 5
4
7 2 5 6

java-线程池(二),布布扣,bubuko.com

时间: 2024-12-13 17:55:48

java-线程池(二)的相关文章

Java线程池使用和分析(二) - execute()原理

相关文章目录: Java线程池使用和分析(一) Java线程池使用和分析(二) - execute()原理 execute()是 java.util.concurrent.Executor接口中唯一的方法,JDK注释中的描述是“在未来的某一时刻执行命令command”,即向线程池中提交任务,在未来某个时刻执行,提交的任务必须实现Runnable接口,该提交方式不能获取返回值.下面是对execute()方法内部原理的分析,分析前先简单介绍线程池有哪些状态,在一系列执行过程中涉及线程池状态相关的判断

Java多线程和并发(十二),Java线程池

目录 1.利用Executors创建线程的五种不同方式 2.为什么要使用线程池 3.Executor的框架 4.J.U.C的三个Executor接口 5.ThreadPoolExecutor 6.线程池的状态 7.线程池大小如何选定 十二.Java线程池 1.利用Executors创建线程的五种不同方式 2.为什么要使用线程池 3.Executor的框架 4.J.U.C的三个Executor接口 5.ThreadPoolExecutor 虽然Executor提供的五种方法够用了,但是仍然不能满足

Java线程池详解(二)

一.前言 在总结了线程池的一些原理及实现细节之后,产出了一篇文章:Java线程池详解(一),后面的(一)是在本文出现之后加上的,而本文就成了(二).因为在写完第一篇关于java线程池的文章之后,越发觉得还有太多内容需要补充,每次都是修修补补,总觉得还缺点什么.在第一篇中,我着重描述了java线程池的原理以及它的实现,主要的点在于它是如何工作的.而本文的内容将更为上层,重点在于如何应用java线程池,算是对第一篇文章的一点补充,这样对于java线程池的学习和总结稍微完整一些. 使用过java线程池

Java线程池使用说明

Java线程池使用说明 一 简介 线程的使用在java中占有极其重要的地位,在jdk1.4极其之前的jdk版本中,关于线程池的使用是极其简陋的.在jdk1.5之后这一情况有了很大的改观.Jdk1.5之后加入了java.util.concurrent包,这个包中主要介绍java中线程以及线程池的使用.为我们在开发中处理线程的问题提供了非常大的帮助. 二:线程池 线程池的作用: 线程池作用就是限制系统中执行线程的数量.     根 据系统的环境情况,可以自动或手动设置线程数量,达到运行的最佳效果:少

JAVA线程池ThreadPoolExecutor与阻塞队列BlockingQueue .

从Java5开始,Java提供了自己的线程池.每次只执行指定数量的线程,java.util.concurrent.ThreadPoolExecutor 就是这样的线程池.以下是我的学习过程. 首先是构造函数签名如下: [java] view plain copy print ? public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue<

浅聊JAVA 线程池的一般用法

一.为什么要用线程池 1).降低资源消耗,通过重复利用已创建的线程降低线程的创建和销毁造成的消耗. 2).提高响应速度,当任务到达时,任务可以不需要等到线程创建就能立即执行. 3).提高线程的可管理性,线程是稀缺资源,如果无限制的创建,不仅会消耗系统资源,还会降低系统的稳定性,使用线程池可以进行统一的分配,调优和监控. 二.注意 1).需要对线程池原理了如指掌. 三.线程的常见用法 1).New Thread. 2).Thread Pool. 四.New Thread 常见用法 new  thr

JAVA线程池简介

一 简介 线程的使用在java中占有极其重要的地位,在jdk1.4极其之前的jdk版本中,关于线程池的使用是极其简陋的.在jdk1.5之后这一情况有了很大的改观.Jdk1.5之后加入了java.util.concurrent包,这个包中主要介绍java中线程以及线程池的使用.为我们在开发中处理线程的问题提供了非常大的帮助. 二 线程池线程池的作用: 线程池作用就是限制系统中执行线程的数量.根据系统的环境情况,可以自动或手动设置线程数量,达到运行的最佳效果:少了浪费了系统资源,多了造成系统拥挤效率

java线程池ThreadPoolExector源码分析

java线程池ThreadPoolExector源码分析 今天研究了下ThreadPoolExector源码,大致上总结了以下几点跟大家分享下: 一.ThreadPoolExector几个主要变量 先了解下ThreadPoolExector中比较重要的几个变量.  corePoolSize:核心线程数量     maximumPoolSize:最大线程数量 allowCoreThreadTimeOut:是否允许线程超时(设置为true时与keepAliveTime,TimeUnit一起起作用)

JAVA线程池的使用

概述 new Thread的弊端如下: a. 每次new Thread新建对象性能差. b. 线程缺乏统一管理,可能无限制新建线程,相互之间竞争,及可能占用过多系统资源导致死机或oom. c. 缺乏更多功能,如定时执行.定期执行.线程中断. 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降低系统的效率,因为频繁创建线程和销毁线程需要时间.那么有没有一种办法使得线程可以复用,就是执行完一个任务,并不被销毁,而是可以继续执行其他的任务?在Java中可

[转 ]-- Java线程池使用说明

Java线程池使用说明 原文地址:http://blog.csdn.net/sd0902/article/details/8395677 一简介 线程的使用在java中占有极其重要的地位,在jdk1.4极其之前的jdk版本中,关于线程池的使用是极其简陋的.在jdk1.5之后这一情况有了很大的改观.Jdk1.5之后加入了java.util.concurrent包,这个包中主要介绍java中线程以及线程池的使用.为我们在开发中处理线程的问题提供了非常大的帮助. 二:线程池 线程池的作用: 线程池作用