zoukankan      html  css  js  c++  java
  • #luogu整理 种树

    洛谷 loj

    (贪心)思路

    同样,我们需要按照区间结束的位置排序,这样在结尾处放置可以使重合的区域变多。最后直接在区间末尾增加就好。

    代码

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    int n,ans;
    int tmp = 0;
    struct QJ_Happy{
    	int l;int r;int t;
    }a[40000];
    int road[40000];
    bool cmp(QJ_Happy a,QJ_Happy b){
    	return a.l > b.l;
    } 
    int main(){
    	cin >> n >> n;
    	for(int i = 1;i <= n; i++) cin >> a[i].l >> a[i].r >> a[i].t;
    	sort(a+1,a+1+n,cmp);
    	for(int i = 1;i <= n; i++){
    		if(!a[i].t) continue;
    		int k = 0;
    		int tot = 0;
    		for(int j = a[i].l;j <= a[i].r; j++) if(road[j]) k++;
    		if(k < a[i].t)
    			for(int j = a[i].l;j <= a[i].r; j++){
    				if(!road[j] && k < a[i].t){
    					k++;tot++;
    					road[j]++;
    				}
    			}
    		ans += tot;
    	}
    	cout << ans << endl;
    	return 0;
    }//lcez_cyc
    
  • 相关阅读:
    算术异常
    MySQL和Oracle的区别
    string常用方法
    io异常
    关于null和空指针异常
    string的一些特殊点
    Mybatis中的动态SQL
    ORM框架的概述
    朴素贝叶斯分类器
    正则表达式
  • 原文地址:https://www.cnblogs.com/Cao-Yucong/p/12187510.html
Copyright © 2011-2022 走看看