Red Huang

Red Huang

uva 11052

Optimize all combinations that meet the conditions from top to bottom

That is 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) <-- does not meet the condition

dp[3]=dp[2]+1  (dp[1]+1 dp[0]+1)<-- does not meet the condition

dp[i]=min(dp[i],dp[j]+1) j from i-1 to 0

That is, DP gives the minimum and satisfying pairing from above to below, but the + sign must be retained, so as soon as the + sign is encountered, immediately break

So all permutations and combinations after the + sign must have a + sign

//  
//        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();  
    }  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
}  

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.