code forces Jeff and Periods

qingyezhu發表於2013-10-07

 

/*
 * c.cpp
 *
 *  Created on: 2013-10-7
 *      Author: wangzhu
 */

#include<cstdio>
#include<iostream>
using namespace std;
#define NMAX 100010
struct Node {
    //index:表示兩個數直接的距離(-1:表示未開始,0:表示開始:> 0:表示有了距離
    //val:記錄最後一次的位置
    //flag:不符合條件 (0:表示符合條件,1:表示不符合條件)
    int index, val, flag;
} node[NMAX];
int main() {
    freopen("data.in", "r", stdin);
    int n,val,k;
    while(~scanf("%d",&n)) {
        for(int i = 0;i < NMAX;i++) {
            node[i].index = -1;
            node[i].val = 0;
            node[i].flag = 0;
        }

        for(int i = 1;i <= n;i++) {
            scanf("%d",&val);
            if(node[val].index > 0) {
                if(node[val].index != i - node[val].val) {
                    node[val].flag = 1;
                }
            } else {
                if(node[val].val > 0) {
                    node[val].index = i - node[val].val;
                } else {
                    node[val].index = 0;
                }
            }
            node[val].val = i;
        }
        k = 0;
        for(int i = 0;i < NMAX;i++) {
            if(node[i].index != -1 && node[i].flag == 0) {
                k++;
            }
        }
        printf("%d\n",k);
        for(int i = 0;i < NMAX;i++) {
            if(node[i].index != -1 && node[i].flag == 0) {
                printf("%d %d\n",i,node[i].index);
            }
        }
    }
    return 0;
}

 

 

超時的方法,如下:

/*
 * c.cpp
 *
 *  Created on: 2013-10-7
 *      Author: wangzhu
 */

#include<cstdio>
#include<iostream>
using namespace std;
#define NMAX 100010
struct Node {
    int index, val;
} node[NMAX];
int main() {
    freopen("data.in", "r", stdin);
    int n,val,k,tempIndex;
    bool flag;
    Node tempNode;
    while(~scanf("%d",&n)) {
        for(int i = 0;i < n;i++) {
            scanf("%d",&val);
            node[i].index = i;
            node[i].val = val;
        }
        for(int i = 0;i < n;i++) {
            for(int j = 0;j < n - i -1;j++) {
                if(node[j].val > node[j + 1].val) {
                    tempNode = node[j];
                    node[j] = node[j+1];
                    node[j+1] = tempNode;
                }
            }
        }
        k = 0;
        flag = true;
        for(int i = 0;i < n;) {
            tempNode = node[i];
            tempIndex = 0;
            flag = true;
            while(i < n && tempNode.val == node[i].val) {
                if(tempIndex == 0) {
                    tempIndex = node[i].index - tempNode.index;
                } else {
                    if(tempIndex != node[i].index -tempNode.index) {
                        flag = false;
                    }
                }
                tempNode = node[i];
                i++;
            }
            if(flag) {
                node[k].val = tempNode.val;
                node[k].index = tempIndex;
                k++;
            }
        }
        printf("%d\n",k);
        for(int i = 0;i <k;i++) {
            printf("%d %d\n",node[i].val,node[i].index);
        }
    }
    return 0;
}

 

相關文章