zoukankan      html  css  js  c++  java
  • HDU_6019(set)

    Description

    MG is a lucky boy. He is always be able to find gold from underground.
    It is known that the gold is a sequence with nelements, which has its own color C.

    MG can dig out a continuous area of sequence every time by using one shovel, but he's unwilling to dig the golds of the same color with one shovel.
    As a greedy person, he wish to take all the n golds away with least shovel.
    The rules also require MG not to dig twice at the same position.
    MG thought it very easy and he had himself disdained to take the job. As a bystander, could you please help settle the problem and calculate the answer?
     
    Input
     
    The first line is an integer Twhich indicates the case number.(1<T<=10.)
    And as for each case, there are Tinteger n in the first line which indicate gold-number.(1<=n<=1e5)
    Then there are n integers Cthe next line, the x-th integer means the x-th gold’s color.(|C|<2e9)
     
    Output
     
    As for each case, you need to output a single line.
    there should be one integer in the line which represents the least possible number of shovels after taking away all ngolds.
     
    Sample Input
    2
    5
    1 1 2 3 -1
    5
    1 1 2 2 3

    Sample Output
    2
    3


    学长讲了题解后,明白了一点,在数据自身的范围好大时应该用离散的思想,就是把这个数映射成一个较小的数 从而遍历那个范围。
    但一直也不会实现,碰到了一个用了set的题目后,想起这道题似乎也可以这样做。
    开始wa了一发,需要注意的是铲子换过一次,就要对set中的元素清空,不然会浪费铲子。
    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 100000;
    int a[maxn];
    set<int> s;
    
    int main(){
    	int T,n,ans;
    	scanf("%d",&T);
    	while(T--){
    		s.clear();		
    		ans = 1;
    		scanf("%d",&n);
    		for(int i=0;i<n;i++){
    			scanf("%d",&a[i]);
    		}
    		for(int i=0;i<n;i++){
    			if(s.count(a[i])){
    				ans++;
    				s.clear();
    			}
    			s.insert(a[i]);
    		}
    		printf("%d
    ",ans);
    	}
    	return 0;	
    }
    





  • 相关阅读:
    js停止(阻止)浏览器继续加载内容
    网页重构中区分IE6、IE7、IE8及标准浏览器的最佳方法
    MySQL学习笔记
    android asmack 注册 登陆 聊天 多人聊天室 文件传输
    解决系统改变字体大小的时候导致的界面布局混乱的问题
    Android UmengShareSDK第三方登录
    Android 设计模式
    Android HTTP session && cookie
    Android之使用HTTP协议的Get/Post方式向服务器提交数据
    Android之Activity的几种跳转方式
  • 原文地址:https://www.cnblogs.com/gjy963478650/p/7297542.html
Copyright © 2011-2022 走看看