2013年8月11日

UVa 11572 - Unique Snowflakes

#include <cstdio>
#include <map>
using namespace std;

int id[1000001];

int main() {
  int t, n;
  scanf("%d", &t);
  while (t--) {
    map<int, int> last;
    scanf("%d", &n);
    int ans = 0, begin = 1;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &id[i]);
      if (last[id[i]] >= begin) {
        begin = last[id[i]] + 1;
      }
      last[id[i]] = i;
      ans = (i - begin + 1) > ans ? (i - begin + 1) : ans;
    }
    printf("%d\n", ans);
  }
  return 0;
}

2 則留言:

  1. Can you make me understand the solution I mean what the variables stand for

    回覆刪除
    回覆
    1. "last" records the last appearance position of a snowflake
      "begin" is the starting position of non-duplicate array

      What line 16 did is to check if there's already a snowflake with the same id in the non-duplicate array.

      刪除