把滿足條件的由上到下所有組合最佳化一次
也就是DP
EX
index year month
0 0 1
1 0 3
2 1 2
3 1 11
4 1 12
5 2 5
6 2 8
7 3 4
dp[0]=1
dp[1]=dp[0]+1
dp[2]=dp[1]+1 (dp[0]+1) <--不滿足條件
dp[3]=dp[2]+1 (dp[1]+1 dp[0]+1)<--不滿足條件
…
dp[i]=min(dp[i],dp[j]+1) j from i-1 to 0
也就是說DP是把上面最少且符合條件的配對往下給 但是+號必須絕對保留 所以一碰到+號立即break
那麼+號之後的排列組合必定都有加號
//
// GGGGGGGGGGGGG CCCCCCCCCCCCC AAA
// GGG::::::::::::G CCC::::::::::::C A:::A
// GG:::::::::::::::G CC:::::::::::::::C A:::::A
// G:::::GGGGGGGG::::G C:::::CCCCCCCC::::C A:::::::A
// G:::::G GGGGGG C:::::C CCCCCC A:::::::::A
//G:::::G C:::::C A:::::A:::::A
//G:::::G C:::::C A:::::A A:::::A
//G:::::G GGGGGGGGGGC:::::C A:::::A A:::::A
//G:::::G G::::::::GC:::::C A:::::A A:::::A
//G:::::G GGGGG::::GC:::::C A:::::AAAAAAAAA:::::A
//G:::::G G::::GC:::::C A:::::::::::::::::::::A
// G:::::G G::::G C:::::C CCCCCC A:::::AAAAAAAAAAAAA:::::A
// G:::::GGGGGGGG::::G C:::::CCCCCCCC::::C A:::::A A:::::A
// GG:::::::::::::::G CC:::::::::::::::C A:::::A A:::::A
// GGG::::::GGG:::G CCC::::::::::::C A:::::A A:::::A
// GGGGGG GGGG CCCCCCCCCCCCCAAAAAAA AAAAAAA
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cctype>
#include <utility>
#include <ctime>
using namespace std;
#ifdef DEBUG
#define debug(...) printf("DEBUG: "),printf(__VA_ARGS__)
#define gettime() end_time=clock();printf("now running time is %.7f\n",(float)(end_time - start_time)/CLOCKS_PER_SEC);
#else
#define debug(...)
#define gettime()
#endif
typedef unsigned int uint;
typedef long long int Int;
#define Set(a,s) memset(a,s,sizeof(a))
#define Write(w) freopen(w,"w",stdout)
#define Read(r) freopen(r,"r",stdin)
#define Pln() printf("\n")
#define I_de(x,n)for(int i=0;i<n;i++)printf("%d ",x[i]);Pln()
#define De(x)printf(#x"%d\n",x)
#define For(i,x)for(int i=0;i<x;i++)
#define CON(x,y) x##y
#define Pmz(dp,nx,ny)for(int hty=0;hty<ny;hty++){for(int htx=0;htx<nx;htx++){\
printf("%d ",dp[htx][hty]);}Pln();}
#define M 1055
#define PII pair<int,int>
#define PB push_back
#define oo INT_MAX
#define Set_oo 0x3f
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define eps 1e-6
clock_t start_time=clock(), end_time;
bool xdy(double x,double y){return x>y+eps;}
bool xddy(double x,double y){return x>y-eps;}
bool xcy(double x,double y){return x<y-eps;}
bool xcdy(double x,double y){return x<y+eps;}
int min3(int x,int y,int z){
int tmp=min(x,y);
return min(tmp,z);
}
int max3(int x,int y,int z){
int tmp=max(x,y);
return max(tmp,z);
}
struct logss{
int time;
bool keep;
int year;
}logs[M];
int n;
char cl[20];
int start,ed;
int cur;
int dp[1024];
void solve(){
// for(int i=0;i<start;i++)dp[i]=1;
dp[start]=1;
for(int i=start+1;i<n;i++){
dp[i]=dp[i-1]+1;
for(int j=i-1;j>=0;j--){
if(logs[i].year==logs[j].year){
dp[i]=min(dp[i],dp[j]+1);
}else if(logs[i].year==logs[j].year+1&&logs[i].time<logs[j].time){
dp[i]=min(dp[i],dp[j]+1);
}else break;
if(logs[j].keep)break;
}
}
int res=dp[n-1];
for(int i=n-2;i>=ed&&logs[i].year==cur;i--){
// printf("%d\n",dp[i]);
res=min(res,dp[i]);
}
printf("%d\n",res);
}
int main() {
ios_base::sync_with_stdio(0);
while(~scanf("%d",&n)&&n){
for(int i=0;i<n;i++){
int w,x,y,z,num;
char c;
scanf("%d:%d:%d:%d %s %c",&w,&x,&y,&z,cl,&c);
logs[i].time=((w*31+x)*24+y)*60+z;
if(c=='+')logs[i].keep=true;
else logs[i].keep=false;
}
logs[0].year=0;
start=(logs[0].keep)?0:-1;
ed=cur=0;
for(int i=1;i<n;i++){
if(start==-1&&logs[i].keep)start=i;
if(logs[i].keep)ed=i;
if(logs[i-1].time<=logs[i].time){
logs[i].year=logs[i-1].year;
}else{
logs[i].year=logs[i-1].year+1;
cur++;
}
}
solve();
}
}
留言
張貼留言