2013 ACM/ICPC Asia Regional Nanjing Online( hdu 4749)


Parade Show

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 633    Accepted Submission(s): 269


Problem Description

  2013 is the 60 anniversary of Nanjing University of Science and Technology, and today happens to be the anniversary date. On this happy festival, school authority hopes that the new students to be trained for the parade show. You should plan a better solution to arrange the students by choosing some queues from them preparing the parade show. (one student only in one queue or not be chosen)
  Every student has its own number, from 1 to n. (1<=n<=10^5), and they are standing from 1 to n in the increasing order the same with their number order. According to requirement of school authority, every queue is consisted of exactly m students. Because students who stand adjacent in training are assigned consecutive number, for better arrangement, you will choose in students with in consecutive numbers. When you choose these m students, you will rearrange their numbers from 1 to m, in the same order with their initial one.
  If we divide our students’ heights into k (1<=k<=25) level, experience says that there will exist an best viewing module, represented by an array a[]. a[i] (1<=i<=m)stands for the student’s height with number i. In fact, inside a queue, for every number pair i, j (1<=i,j<=m), if the relative bigger or smaller or equal to relationship between the height of student number i and the height of student number j is the same with that between a[i] and a[j], then the queue is well designed. Given n students’ height array x[] (1<=x[i]<=k), and the best viewing module array a[], how many well designed queues can we make at most?
 

Input
Multiple cases, end with EOF.
First line, 3 integers, n (1<=n<=10^5) m (1<=m<=n) k(1<=k<=25),
Second line, n students’ height array x[] (1<=x[i]<=k,1<=i<=n);
Third line, m integers, best viewing module array a[] (1<=a[i]<=k,1<=i<=m);
 

Output
One integer, the maximal amount of well designed queues.
 

Sample Input
 
   
10 5 10 2 4 2 4 2 4 2 4 2 4 1 2 1 2 1
 

Sample Output
 
   
1
 

Source
2013 ACM/ICPC Asia Regional Nanjing Online
 

Recommend
liuyiding


这个题目比赛的时候没有作出来,比较遗憾。

注意两点:

1.使用贪心来遍历,即如果 i开始的队列check成功,则直接跳转到 i+m开始check。无须继续判断i+1的队伍。

2.判断一个队列是否合法,可以是O(m)的复杂度,窍门就是,把所有1~k中出现的位置,都用vector 存储,即v[k] 代表 模式串中身高为k的出现位置。

   那么对于目标串,遍历每一个身高p,看看对应的那几个位置是否相等,并存储那个对应值 data[p]。

   最后扫一遍data,看是否为递增序列即可。



核心思想就是(该相等的地方,都去给我相等,然后只需要再扫一遍判断递增即可!


代码如下:


#include 
#include 
#include 
#include 
#include 
using namespace std;
int x[100100] = {0};
int a[100100] = {0};
int n, m, k;
int data[30];
vector v[30];
bool check(int * b) {
    int i, j;
    memset(data, 0, sizeof(data));
    for (i = 1; i <= k; i++) {
        for (j = 0; j < v[i].size(); j++) {
            if (j == 0) data[i] = b[v[i][j]];
            else if (b[v[i][j]] != data[i]) return false;
        }
    }
    for (i = 1; i <= k; i++) {
        for (j = i+1; data[j] == 0 && j <= k; j++) {
        }
        if (data[i] && data[j] && data[i] >= data[j]) return false;
        i = j-1;
    }
    return true;
}
int main() {
    while (EOF != scanf("%d%d%d", &n, &m, &k)) {
        int i, j;
        for (i = 0; i < n; i++) {
            scanf("%d", &x[i]);
        }
        for (i = 0; i < 30; i++) {
            v[i].clear();
        }
        for (i = 0; i < m; i++) {scanf("%d", &a[i]);
            v[a[i]].push_back(i); 
        }
        //memset(x, 0, sizeof(x));
        //memset(a, 0, sizeof(a));
        int sum = 0;
        for (i = 0; i < n-m+1; i++) {
            //cout << i << endl;
            if (check(x + i)) {
                sum++;
                i += (m - 1);
            }
        }
        printf("%d\n", sum);
    }
}


你可能感兴趣的:(oj)