Intervals poj1089 解题报告
来源:互联网 发布:怎么看java源码 编辑:程序博客网 时间:2024/05/01 21:16
Description
There is given the series of n closed intervals [ai; b i], where i=1,2,...,n. The sum of those intervals may berepresented as a sum of closed pairwise non−intersecting intervals. The task is to find such representation with the minimal number of intervals. The intervals of this representation should be written in the output file in acceding order. We say that the intervals [a; b] and [c; d] are in ascending order if, and only if a <= b < c <= d.
Task
Write a program which:
.reads from the std input the description of the series of intervals,
.computes pairwise non−intersecting intervals satisfying the conditions given above,
.writes the computed intervals in ascending order into std output
Task
Write a program which:
.reads from the std input the description of the series of intervals,
.computes pairwise non−intersecting intervals satisfying the conditions given above,
.writes the computed intervals in ascending order into std output
Input
In the first line of input there is one integer n, 3 <= n <= 50000. This is the number of intervals. In the (i+1)−st line, 1 <= i <= n, there is a description of the interval [ai; bi] in the form of two integers ai and bi separated by a single space, which are respectively the beginning and the end of the interval,1 <= ai <= bi <= 1000000.
Output
The output should contain descriptions of all computed pairwise non−intersecting intervals. In each line should be written a description of one interval. It should be composed of two integers, separated by a single space, the beginning and the end of the interval respectively. The intervals should be written into the output in ascending order.
Sample Input
55 61 410 106 98 10
Sample Output
1 45 10
解题思路:用到贪心,主要是对数据排序问题,用到插入排序可以解决问题。 代码如下:
- #include <stdio.h>
- int a[50004],b[50004];
- void solve(int *a,int *b,int n);
- void Insertsort(int n)
- {
- int i,j;
- for(i=2;i<=n;i++)
- {
- if(a[i]<a[i-1])
- {
- a[0]=a[i];
- b[0]=b[i];
- for(j=i-1;a[j]>a[0];j--)
- {
- a[j+1]=a[j];
- b[j+1]=b[j];
- }
- a[j+1]=a[0];
- b[j+1]=b[0];
- }
- }
- }
- int main ()
- {
- int i;
- int n;
- scanf("%d",&n);
- for(i=1;i<=n;i++)
- scanf("%d %d",&a[i],&b[i]);
- Insertsort(n);
- solve(a,b,n);
- return 0;
- }
- void solve(int *a,int *b,int n)
- {
- int i;
- int start=a[1];
- int end=b[1];
- for(i=2;i<=n;i++)
- {
- if(b[i]>end)
- {
- if(a[i]>end)
- {
- printf("%d %d/n",start,end);
- start=a[i];
- end=b[i];
- }
- if(a[i]<=end)
- end=b[i];
- }
- }
- printf("%d %d/n",start,end);
- }
- Intervals poj1089 解题报告
- POJ1089解题报告 区间合并
- Merge Intervals解题报告
- 1716 Integer Intervals 解题报告
- [LeetCode]Merge Intervals, 解题报告
- 【LeetCode】Merge Intervals 解题报告
- 解题报告 之 POJ1201 Intervals
- 解题报告 之 POJ3680 Intervals
- poj 1201 Intervals 解题报告
- 【LeetCode】Merge Intervals 解题报告
- 【贪心算法,区间覆盖】:poj1089,Intervals
- [leetcode] 56. Merge Intervals 解题报告
- leetcode解题报告:56. Merge Intervals
- [Leetcode] 56. Merge Intervals 解题报告
- [LeetCode] 56. Merge Intervals 解题报告
- [LeetCode] 435. Non-overlapping Intervals 解题报告
- [Leetcode] 435. Non-overlapping Intervals 解题报告
- poj1089
- 刚开始学~不知晚不晚?
- Sum
- poj1328 Radar Installation 解题报告
- Word Amalgamation(poj1318) 解题报告
- 滑雪 poj1088 解题报告
- Intervals poj1089 解题报告
- 一个很不愿意去想的问题!
- 太麻烦
- 新手报到
- 1008p
- 明天就是新的一周了
- 让你的游戏运行不起来(不知道的看看)
- DNA Sorting poj1007 解题报告
- JAVA继承