您的位置:首页 > 其它

Codeforces689E Mike and Geometry Problem(数学+算贡献)

2016-07-24 16:50 197 查看
http://codeforces.com/problemset/problem/689/E

题意:给你n个区间,让你选k个,然后k个区间的交的长度,所有的选法的长度和。

题解:看到k个区间,一般就想到了左右端点存起来,然后扫描,当覆盖超过k的时候,计算这一段的贡献,大体思路是这样。然后就是考虑端点细节,当覆盖超过k的时候,需要组合数,tmp存当前要计算的这一段的起始位置,然后如果这会是左端点,计算到左端点-1的位置,然后tmp存左端点,如果是右端点,就是要计算到右端点的位置,然后tmp存右端点+1。

为什么左右端点计算方法不同呢,主要是因为有重复的端点,然后按照排序,左端点应该排在前面,然后如果连续多个重复的左端点,这样计算的话这段的长度一直是0,然后下一个是右端点的话,这段的长度就是1了,并且一个组合数,就能把这段所有取的情况都算进去,然后tmp移动到右端点+1,如果后面还有重复的右端点,就不用算这段区间的贡献了,因为这段已经在覆盖最多的时候算过了。

代码:

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")

using namespace std;
#define   MAX           200005
#define   MAXN          6005
#define   maxnode       15
#define   sigma_size    30
#define   lson          l,m,rt<<1
#define   rson          m+1,r,rt<<1|1
#define   lrt           rt<<1
#define   rrt           rt<<1|1
#define   middle        int m=(r+l)>>1
#define   LL            long long
#define   ull           unsigned long long
#define   mem(x,v)      memset(x,v,sizeof(x))
#define   lowbit(x)     (x&-x)
#define   pii           pair<int,int>
#define   bits(a)       __builtin_popcount(a)
#define   mk            make_pair
#define   limit         10000

//const int    prime = 999983;
const int    INF   = 0x3f3f3f3f;
const LL     INFF  = 0x3f3f;
const double pi    = acos(-1.0);
//const double inf   = 1e18;
const double eps   = 1e-8;
const LL    mod    = 1e9+7;
const ull    mx    = 133333331;

/*****************************************************/
inline void RI(int &x) {
char c;
while((c=getchar())<'0' || c>'9');
x=c-'0';
while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
/*****************************************************/

struct Edge{
int pos,pre,f;
bool operator < (const Edge&a)const{
if(pos==a.pos) return f>a.f;
return pos<a.pos;
}
}p[MAX*2];
LL fact[MAX];

LL qpow(LL a,LL n){
LL ans=1;
while(n){
if(n&1) ans=ans*a%mod;
a=a*a%mod;
n>>=1;
}
return ans;
}
LL C(int n,int m){
return (fact
*qpow(fact[m],mod-2)%mod)*qpow(fact[n-m],mod-2)%mod;
}
int main(){
int n,k;
fact[0]=1;
for(int i=1;i<=200000;i++) fact[i]=fact[i-1]*i%mod;
while(cin>>n>>k){
for(int i=0;i<n;i++){
int l,r;
scanf("%d%d",&l,&r);
p[2*i]=(Edge){l,r,1};
p[2*i+1]=(Edge){r,l,0};
}
sort(p,p+2*n);
int num=0;
int tmp=0;
LL ans=0;
for(int i=0;i<2*n;i++){
if(p[i].f){
if(num<k){
num++;
tmp=p[i].pos;
}
else{
ans=(ans+(LL)(p[i].pos-tmp)*C(num,k)%mod)%mod;
tmp=p[i].pos;
num++;
}
}
else{
if(num<k) num--;
else{
ans=(ans+(LL)(p[i].pos-tmp+1)*C(num,k)%mod)%mod;
tmp=p[i].pos+1;
num--;
}
}
}
cout<<ans<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: