Loading

HDU 6019:MG loves gold(暴力set)

http://acm.hdu.edu.cn/showproblem.php?pid=6019

题意:给出n个颜色的物品,你每次取只能取连续的不同颜色的物品,问最少要取多少次。

思路:从头往后扫,用set存之前取了什么物品,然后每次重复就clear,ans++。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define N 100010
 4 int c[N];
 5 set<int> se;
 6 
 7 int main() {
 8     int t; scanf("%d", &t);
 9     while(t--) {
10         int n; scanf("%d", &n); se.clear();
11         for(int i = 1; i <= n; i++) scanf("%d", &c[i]);
12         int ans = 0;
13         for(int i = 1; i <= n; i++) {
14             if(se.count(c[i]))
15                 ans++, se.clear();
16             se.insert(c[i]);
17         }
18         if(!se.empty()) ans++;
19         printf("%d\n", ans);
20     }
21     return 0;
22 }

 

posted @ 2017-05-05 21:12  Shadowdsp  阅读(182)  评论(0编辑  收藏  举报