1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| # include <algorithm> # include <iostream> # include <cstdio> # include <cstring> # include <queue> # define R register # define LL long long # define inf 999999999
using namespace std;
int n,m,up[10010],down[10010],a[10010],b[10010],f[10010][1010],k,gd[10010],g;
inline int in(R int &a){ R char c = getchar();R int x = 0,f = 1; while(!isdigit(c)){if(c == '-') f = -1;c = getchar();} while(isdigit(c)) x = x*10+c-'0',c = getchar(); a = x*f; }
int main (){ in(n),in(m),in(k); n++; for(R int i=2; i<=n; ++i) for(R int j=0; j<=m+1; ++j) f[i][j] = inf; for(R int i=1; i<n; ++i) in(up[i]),in(down[i]); for(R int i=1; i<=n; ++i) b[i] = m+1; for(R int i=1; i<=k; ++i) { R int d,x,y; in(d),in(x),in(y); gd[++g] = d+1; a[d+1] = x,b[d+1] = y; } sort(gd+1,gd+g+1); for(R int i=1; i<=m; ++i) f[1][i]=0; for(R int i=2; i<=n; ++i){ for(R int j=1; j<=m; ++j){ if(j-up[i-1] > 0) f[i][j] = min(f[i][j],f[i-1][j-up[i-1]]+1), f[i][j] = min(f[i][j],f[i][j-up[i-1]]+1); }
for(R int j=m-up[i-1]; j<=m; ++j) if(j > 0) f[i][m] = min(f[i][m],f[i-1][j]+1), f[i][m] = min(f[i][m],f[i][j]+1); for(R int j=a[i]+1; j<b[i]; ++j) if(j+down[i-1] <= m) f[i][j] = min(f[i][j],f[i-1][j+down[i-1]]);
for(R int j=1; j<=a[i]; ++j) f[i][j] = inf; for(R int j=b[i]; j<=m; ++j) f[i][j] = inf; }
R int ans = inf;
for (R int i=a[n]+1; i<b[n]; ++i) ans = min(ans,f[n][i]);
if(ans < inf) printf("1\n%d",ans); else{ R int i = 1; for(; i<=g; ++i) for(R int j=a[gd[i]]+1; j<b[gd[i]]; ++j){ if(f[gd[i]][j] < inf) break; if(j == b[gd[i]]-1){ printf("0\n%d",i-1); return 0; } } } return 0; }
|