Juice Extractor |
Jerry loses himself in the interesting game: Fruit Ninja. Fruit Ninja is a game of iPhone and iPad in which the players cut the fruits coming from the bottom of the screen and gain the bonus from cutting more than two fruits with a single slice. Once a fruit is cut, it breaks into small pieces and cannot be cut any more.
After months of training, he becomes pro of this game. Actually, he can cut all the fruits on the screen at any time. Jerry also has a bad habit that he has no willing to leave some fruits for the future cutting. In the other words, after Jerry cuts the fruits, all the fruits on the screen breaks and no one left. That is why all his friends call him `Juice Extractor'.
Now he only consider about the bonus, when he cuts more than two fruits, he can gain some bonus scores as same as the number of fruits he slice at that time. For example, if Jerry cuts 4 fruits with a single slice, he can get 4 scores from this slice.
After Jerry gets the fruit schedule, he knows the appearing time and the disappearing time for every single fruit. He can only cut a fruit into pieces between its appearing time and disappearing time inclusive. He wants to know the maximum possible bonus scores he can receive.
For each test case, the first line contains an integer N, denoting the total number of fruits. ( 1N1000)
The next N lines, each line describe a fruit. For each line, there are two integers Xi and Yi, where Xi is the appearing time of the fruit and Yi is the disappearing time of this fruit. ( 0XiYi1000000000)
For each test case, output a single integer denoting the maximum scores that Jerry could possibly gain. See the sample for further details.
1 10 1 10 2 11 3 12 4 13 13 14 14 15 13 19 20 22 21 23 22 24
Case #1: 10--------------------------------------
大体思路很容易想到,但是为什么wa了呢?
---------------------------------
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; const int OO=1e9; const int maxn=1111; struct Fruits{ int x; int y; }; int n,T; int f[maxn]; Fruits a[maxn]; bool cmp(Fruits a,Fruits b) { if (a.x==b.x) return a.y<b.y; return a.x<b.x; } int main() { scanf("%d",&T); int cas=1; while (T--) { memset(a,0,sizeof(a)); memset(f,0,sizeof(f)); scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d%d",&a[i].x,&a[i].y); sort(a+1,a+n+1,cmp); int ans=0; for (int i=1;i<=n;i++) { int cut=0; if (i+1<=n&&a[i].x==a[i+1].x) continue;//why??????? for (int j=i;j>=1;j--) { if (a[j].y>=a[i].x) cut++; if (cut>2) f[i]=max(f[i],f[j-1]+cut); else f[i]=max(f[i],f[j-1]); } ans=max(ans,f[i]); } printf("Case #%d: %d\n",cas++,ans); } return 0; }
Juice Extractor |
Jerry loses himself in the interesting game: Fruit Ninja. Fruit Ninja is a game of iPhone and iPad in which the players cut the fruits coming from the bottom of the screen and gain the bonus from cutting more than two fruits with a single slice. Once a fruit is cut, it breaks into small pieces and cannot be cut any more.
After months of training, he becomes pro of this game. Actually, he can cut all the fruits on the screen at any time. Jerry also has a bad habit that he has no willing to leave some fruits for the future cutting. In the other words, after Jerry cuts the fruits, all the fruits on the screen breaks and no one left. That is why all his friends call him `Juice Extractor'.
Now he only consider about the bonus, when he cuts more than two fruits, he can gain some bonus scores as same as the number of fruits he slice at that time. For example, if Jerry cuts 4 fruits with a single slice, he can get 4 scores from this slice.
After Jerry gets the fruit schedule, he knows the appearing time and the disappearing time for every single fruit. He can only cut a fruit into pieces between its appearing time and disappearing time inclusive. He wants to know the maximum possible bonus scores he can receive.
For each test case, the first line contains an integer N, denoting the total number of fruits. ( 1N1000)
The next N lines, each line describe a fruit. For each line, there are two integers Xi and Yi, where Xi is the appearing time of the fruit and Yi is the disappearing time of this fruit. ( 0XiYi1000000000)
For each test case, output a single integer denoting the maximum scores that Jerry could possibly gain. See the sample for further details.
1 10 1 10 2 11 3 12 4 13 13 14 14 15 13 19 20 22 21 23 22 24
Case #1: 10