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 }

 

转载于:https://www.cnblogs.com/fightfordream/p/6814944.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值