zoukankan      html  css  js  c++  java
  • poj_1716Integer Intervals

    Integer Intervals
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 11782   Accepted: 4964

    Description

    An integer interval [a,b], a < b, is a set of all consecutive integers beginning with a and ending with b. 
    Write a program that: finds the minimal number of elements in a set containing at least two different integers from each interval.

    Input

    The first line of the input contains the number of intervals n, 1 <= n <= 10000. Each of the following n lines contains two integers a, b separated by a single space, 0 <= a < b <= 10000. They are the beginning and the end of an interval.

    Output

    Output the minimal number of elements in a set containing at least two different integers from each interval.

    Sample Input

    4
    3 6
    2 4
    0 2
    4 7
    

    Sample Output

    4
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<map>
    #include<cmath>
    #include<vector>
    #include<algorithm>
    #include<set>
    #include<string>
    #include<queue>
    #include <stack>
    using namespace std;
    #pragma warning(disable : 4996)
    const int MAXN = 10005;
    const int INF = 999999;
    
    typedef struct Node 
    {
    	int v;//终点位置
    	int value;//权值
    	int next;//同一起点下在edge数组中的位置
    }Node;
    Node edge[MAXN * 4];//邻接表
    int first[MAXN];//以该点为起点的第一条边在edge数组中的位置
    int n; //n点数
    bool visited[MAXN];
    int dist[MAXN];
    queue<int>Q;
    int MIN, MAX;
    
    void init()
    {
    	int x, y, index;
    	memset(first, -1, sizeof(first));
    	index = 1;
    	MIN = INF;
    	MAX = -INF;
    	for (int i = 1; i <= n; i++)
    	{
    		scanf("%d %d", &x, &y);
    		y++;
    		if(x < MIN)
    		{
    			MIN = x;
    		}
    		if(y > MAX)
    		{
    			MAX = y;
    		}
    		edge[index].v = y;
    		edge[index].value = 2;
    		edge[index].next = first[x];
    		first[x] = index++;
    	}
    	//cout << MIN << " " << MAX << endl;
    	for (int i = MIN; i < MAX; i++)
    	{
    		edge[index].v = i + 1;
    		edge[index].value = 0;
    		edge[index].next = first[i];
    		first[i] = index++;
    
    		edge[index].v = i;
    		edge[index].value = -1;
    		edge[index].next = first[i + 1];
    		first[i + 1] = index++;
    	}
    
    }
    
    
    void SPFA(int Start)
    {
    	while (!Q.empty())
    	{
    		Q.pop();
    	}
    	for (int i = 0; i <= MAX; i++)
    	{
    		visited[i] = false;
    		dist[i] = -INF;
    	}
    	dist[Start] = 0;
    	visited[Start] = true;
    	Q.push(Start);
    	while (!Q.empty())
    	{
    		int top = Q.front();
    		Q.pop();
    		visited[top] = false;
    		for (int i = first[top]; i != -1 ; i = edge[i].next)
    		{
    			int e = edge[i].v;
    			if(dist[e] < edge[i].value + dist[top])
    			{
    				dist[e] = edge[i].value + dist[top];
    				if(!visited[e])
    				{
    					Q.push(e);
    					visited[e] = true;
    				}
    			}
    		}
    	}
    }
    
    int main()
    {
    	freopen("in.txt", "r", stdin);
    	while (scanf("%d", &n) != EOF)
    	{
    		init();
    		SPFA(MIN);
    		printf("%d\n", dist[MAX]);
    		//print();
    	}
    	return 0;
    }
    


    
       
    
    
  • 相关阅读:
    在win2003中sql server2005的安装及配置
    excel多行以逗号拼接为一行
    git删除分支
    IeXglEvEyH
    excel为某列数据加双引号和逗号,用于拼接成json列表
    where 1=1 的作用
    ThreadLocal 定义、使用场景、案例、原理、注意事项
    gitlab第一次开发项目步骤
    git切回旧版本代码后再切回最新代码
    CountDownLatch与CyclicBarrier与Semaphore的区别
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835030.html
Copyright © 2011-2022 走看看