1.[SDOI2005]阶梯教室设备利用
题目背景
我们现有许多演讲要在阶梯教室中举行。每一个演讲都可以用唯一的起始和终止时间来确定,如果两个演讲时间有部分或全部重复,那么它们是无法同时在阶级教室中举行的。现在我们想要尽最大可能的利用这个教室,也就是说,我们需要在这些演讲中选择一些不重复的演讲来举行使得他们用的总时间尽可能的长。我们假设在某一演讲结束的瞬间我们就可以立即开始另一个演讲。
题目描述
请写一个程序:
读入所有演讲的起始和终止时间;
计算最大的可能演讲总时间;
输入输出格式
输入格式:
第一行包括一个正整数n,n<=10000,为所有的演讲的数目。以下的n行每行含有两个由空格隔开的整数p和k,0<=p< k<=30000。这样的一对整数表示一个演讲由时间p开始到时间k结束。
输出格式:
输出唯一的一个整数,为最长的演讲总时间。
输入输出样例
输入样例#1:
12
1 2
3 5
0 4
6 8
7 13
4 6
9 10
9 12
11 14
15 19
14 16
18 20
输出样例#1:
16
solution:sort+dp
#define Name "rez"
#include <cmath>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define N 10010
#define cls(a,b) memset(a,b,sizeof(a))
inline int read(){
int x = 0, f =1 ;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n, dp[N];
struct E{ int l, r, dis; } a[N];
inline bool cmp( E a, E b ){
if ( a.l == b.l ) return a.r < b.r;
return a.l < b.l;
}
int main(){
n = read();
for ( int i = 1; i <= n; i++ ) a[i].l = read(), a[i].r = read(), a[i].dis = a[i].r - a[i].l;
sort( a+1, a+1+n, cmp );
for ( int i = 1; i <= n; i++ ) dp[i] = a[i].dis;
for ( int i = 1; i <= n; i++ )
for ( int j = 1; j <= i; j++ ){
if( a[i].l >= a[j].r ) dp[i] = max( dp[i], dp[j]+a[i].dis );
}
int ans = 0;
for ( int i = 1; i <= n; i++) ans = max( ans, dp[i] );
printf( "%d", ans);
return 0;
}
- [SDOI2005]位图
题目描述
现在我们给出一个n*m的单色位图,且该图中至少含有一个白色的像素。我们用(i, j)来代表第i行第j列的像素,并且定义两点p1=(i1, j1)和p2=(i2, j2)之间的距离为:
d(p1, p2)=|i1 - i2| + |j1 – j2| 任务:
请写一个程序:
从文本文件BIT.IN中读入该位图;
对于每个像素,计算出离该像素最近的白色像素与它的距离;
把结果输出到文本文件BIT.OUT中。
输入输出格式
输入格式:
在文本文件BIT.IN的第一行包括两个用空格分开的整数n和m,1<=n<=150,1<=m<=150。以下的n行每行包括一个长度为m的整数为零或一,在第i+1行的第j个字符如果为”1”,那么表示像素(i, j)为白的,否则为黑的。
输出格式:
在文本文件BIT.OUT中输出一个n*m的数表,其中的第i行的第j个数字为f(i, j)表示像素(i, j)到最近的白色像素的距离
输入输出样例
输入样例#1:
3 4
0 0 0 1
0 0 1 1
0 1 1 0
输出样例#1:
3 2 1 0
2 1 0 0
1 0 0 1
solution:SPFA
#define Name "rez"
#include <cmath>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define N 10010
#define cls(a,b) memset(a,b,sizeof(a))
inline int read(){
int x = 0, f =1 ;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n, dp[N];
struct E{ int l, r, dis; } a[N];
inline bool cmp( E a, E b ){
if ( a.l == b.l ) return a.r < b.r;
return a.l < b.l;
}
int main(){
n = read();
for ( int i = 1; i <= n; i++ ) a[i].l = read(), a[i].r = read(), a[i].dis = a[i].r - a[i].l;
sort( a+1, a+1+n, cmp );
for ( int i = 1; i <= n; i++ ) dp[i] = a[i].dis;
for ( int i = 1; i <= n; i++ )
for ( int j = 1; j <= i; j++ ){
if( a[i].l >= a[j].r ) dp[i] = max( dp[i], dp[j]+a[i].dis );
}
int ans = 0;
for ( int i = 1; i <= n; i++) ans = max( ans, dp[i] );
printf( "%d", ans);
return 0;
}
*