做法:dp
思路:
- 1.先用结构体把每个横坐标的情况记录下来
- 2.dp[i][j]表示到坐标(i,j)最少需要跳的次数
- 3.考虑上升转移和下降转移,其中上升转移要考虑到顶这种特殊情况
代码
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp(aa,bb) make_pair(aa,bb)
#define _for(i,b) for(int i=(0);i<(b);i++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,b,a) for(int i=(b);i>=(a);i--)
#define mst(abc,bca) memset(abc,bca,sizeof abc)
#define X first
#define Y second
#define lowbit(a) (a&(-a))
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef long double ld;
const int N=10010,M=1010;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-6;
const double PI=acos(-1.0);
int n,m,k;
struct node{
int x,y,minh,maxh;
bool st;
}a[N];
int dp[N][M<<1]; //dp[i][j]:到坐标(i,j)最少需要跳的次数
int ans=INF;
void solve(){
cin>>n>>m>>k;
rep(i,1,n) {
cin>>a[i].x>>a[i].y;
a[i].minh=0,a[i].maxh=m+1;
}
while(k--){
int p,l,h;
cin>>p>>l>>h;
a[p].minh=l,a[p].maxh=h,a[p].st=1;
}
mst(dp,INF);
rep(i,1,m) dp[0][i]=0;
rep(i,1,n){
rep(j,1,m) dp[i][j+a[i].x]=min(dp[i-1][j]+1,dp[i][j]+1);
rep(j,m+1,m+a[i].x) dp[i][m]=min(dp[i][j],dp[i][m]);//到顶
rep(j,1,m-a[i].y) dp[i][j]=min(dp[i][j],dp[i-1][j+a[i].y]);
rep(j,1,a[i].minh) dp[i][j]=INF;
rep(j,a[i].maxh,m) dp[i][j]=INF;
}
rep(i,1,m) ans=min(ans,dp[n][i]);
if(ans<INF){
cout<<"1\n"<<ans<<"\n";
return;
}
cout<<"0\n";
int i,j,num=0;
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
if(dp[i][j]<INF) break;
}
if(j==m+1) break;
if(a[i].st) num++;
}
cout<<num<<"\n";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
#ifdef DEBUG
freopen("F:/laji/1.in", "r", stdin);
// freopen("F:/laji/2.out", "w", stdout);
#endif
// int t;cin>>t;while(t--)
solve();
return 0;
}