zoukankan      html  css  js  c++  java
  • 题解 P1469 【找筷子】

    这题真是水
    咳咳。。
    基本思路:桶排
    但是可以剪枝。


    剪枝方法:

    好几种,可以用set(集合),可以用stack(栈)
    也可以像我一样的蒟蒻最大最小值......
    但是作者的毒瘤数据应该不会放过我们的...


    AC code奉上

    #include <iostream>
    #include <cstdio>
    using namespace std;
    int bfs(int bottle[],int max_,int min_)
    {
    	for (int i=min_;i<=max_;i++)
    	{if (bottle[i]%2==1)return i;}
    }
    //基本思路:桶排 
    //剪枝优化方式:最大最小值 
    int main()
    {
    	int n,max_=0,min_=10000010;
    	static int bottle[10000001];
    	//方便的是,静态数组默认为0 
    	scanf("%d",&n);
    	for (int i=1;i<=n;i++)
    	{
    		int temp;scanf("%d",&temp);
    		bottle[temp]++;
    		max_=max_>temp?max_:temp;
    		min_=min_<temp?min_:temp;
    	}
    	int num=bfs(bottle,max_,min_);
    	printf("%d",num);
    	return 0;
    }
    

    集合的思想就是去重
    查找的时候只需要一个个抽元素出来找就行。
    可以大大减少查找次数。
    话说快排真的有必要吗

  • 相关阅读:

    双向链表
    obs分析 笔记
    循环链表
    静态链表
    链式顺序表
    线性表
    ffmpeg-4.1.1-win64-dev在vs2017的搭建
    G1 与 CMS 两个垃圾收集器的对比
    垃圾回收算法有几种类型? 他们对应的优缺点又是什么?
  • 原文地址:https://www.cnblogs.com/jelly123/p/10385896.html
Copyright © 2011-2022 走看看