zoukankan      html  css  js  c++  java
  • [洛谷] P1209 修理牛棚

    问题化简了就是在找间断点

    用贪心差值越大越优

    //#pragma GCC optimize(2)
    #include <cstdio>
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    #include <cctype>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <stack>
    #include <queue>
    #include <set>
    #include <map>
    #include <ctime>
    #include <vector>
    #include <fstream>
    #include <list>
    #include <iomanip>
    #include <numeric>
    using namespace std;
    #define int long long
    const int MAXN = 1e6 + 10;
    
    int arr[MAXN];
    
    int sum[MAXN];
    
    signed main()
    {
        //ios::sync_with_stdio(false);
        //cin.tie(0);     cout.tie(0);
        //freopen("D://test.in", "r", stdin);
        //freopen("D://test.out", "w", stdout);
        
        int N, M, C;
    
        cin>>N>>M>>C;
    
        for(int i = 1; i <= C; i++)
        {
            cin>>arr[i];
        }
    
        sort(arr + 1, arr + C + 1);
    
    //    for(int i = 1; i <= C; i++)
    //        cout<<arr[i]<<' ';
    
        set <int> used;
    	
    	if(C > 1)
    	{
    		for(int i = 1; i < min(N, C); i++)
    	    {
    	        int cha = -0x3f3f3f3f, pos = 0;
    	
    	        for(int j = 2; j <= C; j++)
    	        {
    	            if(arr[j] - arr[j - 1] > cha && !used.count(j))
    	            {
    	                cha = arr[j] - arr[j - 1];
    	                pos = j;
    	            }
    	        }
    			used.insert(pos);
    	    }
    		used.insert(C + 1);
    	}
    	
        int fst = 1;
    
        int ans = 0;
        
        for(set<int>::iterator it = used.begin(); it != used.end(); it++)
        {
            ans += (arr[*it - 1] - arr[fst] + 1);
            fst = *it;
        }
    
        cout<<ans<<endl;
    
        
        return 0;
    }
  • 相关阅读:
    Fedora 23 配置
    小小的告别一下这个博客
    markdown测试
    ihhh题解
    【BZOJ】1998: [Hnoi2010]Fsk物品调度
    【BZOJ】2563: 阿狸和桃子的游戏
    【BZOJ】3712: [PA2014]Fiolki
    【BZOJ】2333: [SCOI2011]棘手的操作
    我的vimrc
    Ubuntu Gnome 14.04.2 lts 折腾笔记
  • 原文地址:https://www.cnblogs.com/zeolim/p/12270427.html
Copyright © 2011-2022 走看看