zoukankan      html  css  js  c++  java
  • 编程算法

    区间调度问题 代码(C)


    本文地址: http://blog.csdn.net/caroline_wendy


    题目: 有n项工作, 每项工作分别在s时间開始, 在t时间结束. 对于每项工作能够选择參与与否, 假设參与, 则全程參与.

    參与时间段不能重叠, 包含起始结束瞬间也不能重叠. 求最多能參与多少项工作?


    使用贪心法, 策略是在可选工作中, 每次都选取结束时间最早的工作.


    代码:

    /*
     * main.cpp
     *
     *  Created on: 2014.7.17
     *      Author: spike
     */
    
    /*eclipse cdt, gcc 4.8.1*/
    
    #include <stdio.h>
    #include <limits.h>
    
    #include <utility>
    #include <queue>
    #include <algorithm>
    
    using namespace std;
    
    class Program {
    	static const int MAX_N = 10000;
    	int N=5, S[MAX_N]={1,2,4,6,8}, T[MAX_N]={3,5,7,9,10};
    	pair<int, int> itv[MAX_N];
    
    public:
    	void solve() {
    		for (int i=0; i<N; i++) {
    			itv[i].first = T[i]; //结束时间在前, 能够排序.
    			itv[i].second = S[i];
    		}
    		sort(itv, itv+N);
    		int ans = 0, t = 0;
    		for (int i=0; i<N; ++i) {
    			if (t<itv[i].second) { //首尾不能重合
    				ans++;
    				t = itv[i].first;
    			}
    		}
    		printf("result = %d
    ", ans);
    	}
    };
    
    
    int main(void)
    {
    	Program P;
    	P.solve();
        return 0;
    }
    
    
    
    


    输出:

    result = 3
    







    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    AC自动机模板
    HDU 3065 病毒侵袭持续中(AC自动机)
    HDU 2896 病毒侵袭(AC自动机)
    HDU 2222 Keywords Search (AC自动机模板题)
    HDU 1113 Word Amalgamation
    HDU 1171 Big Event in HDU(母函数或01背包)
    37.Qt网络与通信
    2.Matlab数值数组及其运算
    36.QT地图
    35.QT蝴蝶飞舞
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4864899.html
Copyright © 2011-2022 走看看