# P3084 [USACO13OPEN]照片Photo

Input

## 输入输出格式

* Line 1: Two integers N and M.

* Lines 2..M+1: Line i+1 contains a_i and b_i.

* Line 1: The maximum possible number of spotted cows on FJ‘s farm, or -1 if there is no possible solution.

## 输入输出样例

```5 3
1 4
2 5
3 4
```

`1 `
```#include<bits/stdc++.h>
using namespace std;
#define maxn 200005
typedef long long ll;
#define inf 0x3fffffff

struct fuck
{
int l,r;
} a[maxn];

int n,m;
int rp[maxn],lp[maxn];
int q[maxn*2],dp[maxn];

int main()
{
//    freopen("test.txt","r",stdin);
cin>>n>>m;
for(int i=1; i<=n+1; i++)
rp[i]=i-1;
for(int i=1; i<=m; i++)
{
cin>>a[i].l>>a[i].r;
lp[a[i].r+1]=max(lp[a[i].r+1],a[i].l);
rp[a[i].r]=min(rp[a[i].r],a[i].l-1);
}
for(int i=n; i>=1; i--)
rp[i]=min(rp[i],rp[i+1]);
for(int i=2; i<=n+1; i++)
lp[i]=max(lp[i],lp[i-1]);
int tail=1;
int j=1;
for(int i=1; i<=n+1; i++)
{
while(j<=rp[i]&&j<=n)
{
if(dp[j]==-1)
{
j++;
continue;
}
tail--;
q[++tail]=j;
j++;
}
else dp[i]=-1;
}
if(dp[n+1]!=-1)
cout<<dp[n+1];
else cout<<-1;

return 0;
}```

P3084 [USACO13OPEN]照片Photo

(0)
(0)

0条