zoukankan      html  css  js  c++  java
  • 今日头条2017校招编程题

    贪心?瞎搞  先排个序,然后扫一遍,边扫边维护一个数组v   v的长度为3    设vs数组的最后一个是x 如果abs(a[i]-x)大于10 那么贪心的策略我就 添加个v+10相应的ans++.  如果v数组的长度到达3了,就清空v数组。

    然后注意一些细节就好了...  我代码写的好挫

    /* ***********************************************
    Author        :guanjun
    Created Time  :2016/9/21 19:50:13
    File Name     :1001.cpp
    ************************************************ */
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <list>
    #include <deque>
    #include <stack>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 10010
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    priority_queue<int,vector<int>,greater<int> >pq;
    struct Node{
        int x,y;
    };
    struct cmp{
        bool operator()(Node a,Node b){
            if(a.x==b.x) return a.y> b.y;
            return a.x>b.x;
        }
    };
    
    bool cmp(int a,int b){
        return a>b;
    }
    int a[100010];
    int v[100010];
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        int n;
        while(cin>>n){
            for(int i=1;i<=n;i++){
                scanf("%d",&a[i]);
            }
            sort(a+1,a+1+n);
            int ans=0;
            int cnt=0;
            for(int i=1;i<=n;i++){
            //    cout<<a[i]<<endl;
            int mark=0;
                if(cnt>=1&&cnt<3){
                    int tmp=v[cnt];
                    if(abs(tmp-a[i])>10){
                        ans++;v[++cnt]=tmp+10;
                        mark=1;
                    }
                    else{
                        v[++cnt]=a[i];
                    }
                }
                else if(cnt==0){
                    v[++cnt]=a[i];
                }
    if(mark)i--;
    if(cnt==3){ //if(mark)i--; cnt=0; } } if(cnt>0)ans+=3-cnt; printf("%d ",ans); } return 0; }
  • 相关阅读:
    strip()、rstrip()和lstrip()
    Vim 中快速移动系列(1)
    Python中的read(), readline(), readlines()
    Python 列表解析(列表生成式)
    Python lambda 表达式介绍
    Python中sort()和sorted()的区别
    js 高级
    maven学习笔记
    Maven之settings.xml详解
    Eclipse 学习笔记
  • 原文地址:https://www.cnblogs.com/pk28/p/5894145.html
Copyright © 2011-2022 走看看