# Jiangxi Province ccpc game -waves- (DP practice)

Jiangxi Province ccpc game -waves- (DP practice)

Chain of title: http://acm.hdu.edu.cn/showproblem.php?pid=6570

  • Question is intended: to give you the length of N, an array 1≤N≤100000, wherein the element [1, c] of the 1≤c≤100, seeking to satisfy a longest sequence numbers are the same odd bit, even bit numbers are also the same, but the difference between parity and asked the length of your sequence.

  • Solution 1: c, a smaller range, is apparent from these properties, the sequence consists of only two numbers, the two numbers can be enumerated in the range of c. First save location of each number appears.

AcCode:

#include <bits/stdc++.h>
using namespace std;
#define fre freopen("C:\\Users\\22765\\Desktop\\in.txt","r",stdin);
#define ms(a) memset((a),0,sizeof(a))
#define re(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define sf(x) scanf("%d",&(x))
#define rg register
#define il inline
typedef long long LL;
const int inf=(0x7f7f7f7f);
const int maxn=3000;
vector<int> v[105];

int main(){
    int n,c;
    sf(n);sf(c);
    int x;
    re(i,0,n){
        sf(x);
        v[x].push_back(i);
    }
    
    int res=0,len;
    int p,q,fir,sec;
    bool f;
    re(i,1,c+1){
    
        if(v[i].size()==0)continue;
        re(j,1,c+1){
            if(i==j)continue;
            p=q=0;len=0;
            fir=sec=-1;f=1;
            
            while(p<v[i].size()&&q<v[j].size()){
                if(f){
                    while(p<v[i].size()&&v[i][p]<sec)p++;
                    if(p<v[i].size()){
                        fir=v[i][p],len++,f=0;
                    }
                }
                else {
                    while(q<v[j].size()&&v[j][q]<fir)q++;
                    if(q<v[j].size()){
                        len++,sec=v[j][q],f=1;
                    }
                }       
            }
            res=max(res,len) ;
        }
    } 
    printf("%d\n",res) ;
    return 0;
}
  • Solution 2: \ (DP [I] [j] \) represents the ending i, the former number is j sequence length, \ (DP [I] [I] = 0 \) , \ (I \) \ (! = j \) when, \ (DP [I] [J] DP = [J] [I] + 1'd \) , for each position, ending the current number of pretreatment, before a number is \ ([1, c ] \) to any number of all eleven cases.

AcCode:

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
#define re(i,a,b) for(int (i)=(a);(i)<(b);++(i))

const int maxn = 1e5 + 5;
int num[maxn];
int dp[105][105];
int main()
{
    std::ios::sync_with_stdio(false);
    int n, c;
    cin >> n >> c;
    int i;
    re(i, 0, n)
    {
        cin >> num[i];
    }
    int ans = 1;
    re(i, 0, c + 1){ 
    //初始化,对于数组第一个数字num[0]来说只要$i!=j那么$dp[i][j]=1$
    //这里为什么是1而不是2,因为可以确定的数只有dp数组的前一个参数,
    //第二个参数是不确定的
        if (num[0] == i)
            dp[num[0]][i] = 0;
        else
            dp[num[0]][i] = 1;
    }
    re(i, 1, n)
    {
        int j;
        re(j, 0, c + 1)//对原序列的每一个位置,预处理第二个参数的所有情况 
        {
            if (num[i] == j)
                dp[num[i]][j] = 0;
            else
                dp[num[i]][j] = dp[j][num[i]] + 1;//状态转移
            ans = max(ans, dp[num[i]][j]);//记录答案
        }
    }
    cout << ans << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/sstealer/p/11260057.html