题意很明确,就是求1->n之间,为0的点的个数
1487: 未覆盖顶点数量
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 267 Solved: 41
Description
有N个顶点,每个顶点有一个权值,初始值皆为0。接下来有M次操作,操作内容为 [a,b) or [b,a),将区间内顶点i 权值置为1,求最后顶点权值为0的数量。
Input
多组测试数据。
第一行为两个整数n, m,n(1<=n<=20000)表示顶点, m(1<=m<=50000)表示操作次数。
接下来包含m行,每行包含两个正整数 a,b属于区间[1,n] ,意义如上所述。
Output
每组测试输出一行,包含一个整数,表示顶点值为0的数量。
Sample Input
3 11 23 11 3
Sample Output
21
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
int n,m,a,b;
int mp[20005];
struct node
{
int s,e;
}t[50005];
bool cmp( node a , node b )
{
return a.s < b.s;
}
int main()
{
while( scanf("%d%d",&n,&m) == 2 ){
memset( mp , 0 , sizeof(mp) );
for( int i=1 ; i<=m ; i++ ){
scanf("%d%d",&a,&b);
if( a > b )
swap(a,b);
t[i].s = a;
t[i].e = b;
}
int sum = 0;
sort( t , t+m , cmp );
int tail = 0;
for( int i=1 ; i<=m ; i++ ){
if( t[i].e < tail )
continue;
sum += ( t[i].e - max(t[i].s , tail) );
tail = t[i].e;
}
printf("%d\n",n-sum);
}
return 0;
}
下面为曺钦学长代码:
View Code
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<assert.h>
using namespace std;
const int N = 2e5+10;
int t[N],n, m,ans;
main()
{
freopen("3.in","r",stdin);
freopen("3.out","w",stdout);
while(scanf("%d%d",&n,&m) !=EOF )
{
assert( n>=1 && n<=20000 );
assert( m>=1 && m<=50000 );
ans=0;
memset(t,0,sizeof(t));
int a, b, j = 0;
for(int i = 0; i < m; i++){
scanf("%d%d",&a,&b);
if( a > b ) swap(a,b);
assert( a>=1 && a<=n );
assert( b>=1 && b<=n );
t[a] += 1; t[b] -= 1;
}
for(int i = 1; i <= n; i++ )
{
j += t[i];
if( j == 0 ) ans++;
}
printf("%d\n",ans);
}
return 0;
}
974

被折叠的 条评论
为什么被折叠?



