拓撲排序 FlowerGarden - 2004 TCCC Round 1

這是拓撲排序,爲毛說成dp入門題。

一直看不懂題,就放棄了,但是今天還是於心不忍決定怎麼也要做了。

思路來自

Discuss裏面有詳盡的討論。


可以研究官方題解

這個題目的統計還是很牛逼的

Value 500
Submission Rate 230 / 376 (61.17%)
Success Rate 109 / 230 (47.39%)
High Score tomek for 469.94 points (7 mins 16 secs)
Average Score 311.32 (for 109 correct submissions)

題目:

 

You are planting a flower garden with bulbs to give you joyous flowers throughout the year. However, you wish to plant the flowers such that they do not block other flowers while they are visible.

You will be given a vector <int> height, a vector <int> bloom, and a vector <int> wilt. Each type of flower is represented by the element at the same index ofheight, bloom, and wilt. height represents how high each type of flower grows, bloom represents the morning that each type of flower springs from the ground, andwilt represents the evening that each type of flower shrivels up and dies. Each element inbloom and wilt will be a number between 1 and 365 inclusive, andwilt[i] will always be greater than bloom[i]. You must plant all of the flowers of the same type in a single row for appearance, and you also want to have the flowers in rows which are more towards the front be as tall as possible. However, if a flower type is taller than another type, and both types can be out of the ground at the same time, the shorter flower must be planted in front of the taller flower to prevent blocking. A flower blooms in the morning, and wilts in the evening, so even if one flower is blooming on the same day another flower is wilting, one can block the other.

For example, if your garden of flowers has heights 1, 2, and 3, and the flowers of height 3 bloom and wilt with flowers of height 1, but neither conflicts with 2, your best arrangement would be 2,1,3. 1,3,2 would be possible, but then the first row of flowers is shorter, which is less desirable.

You should return a vector <int> which contains the elements of height in the order you should plant your flowers to acheive the above goals. The front of the garden is represented by the first element in your return value, and is where you view the garden from. The elements of height will all be unique, so there will always be a well-defined ordering.


大概意思:

          高的花開花,不要在低的前面(所謂的阻礙)。給出一種種植方案。


Trick:

      When figuring out which type of flower to place next, you can only place flowers that can go in front of all the flowers left to place. Of those, you should place the type which is tallest.


解法:

    拓撲排序,每次選擇最大的就好了~~~


使用TopCoder 才知道自己寫代碼,是有多菜。

/*************************************************************************
    > File Name: 2004tccc1500.cpp
    > Author: cy
    > Mail: [email protected] 
    > Created Time: 2014/8/26 15:08:41
 ************************************************************************/

#include<iostream>
#include<cstring>
#include <algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>

#define maxn 50000+5

#define inf 0x3f3f3f3f
  #define INF 0x3FFFFFFFFFFFFFFFLL
#define rep(i,n) for(i=0;i<n;i++)
 #define reP(i,n) for(i=1;i<=n;i++)

#define ull unsigned long long
 #define ll long long

#define cle(a) memset(a,0,sizeof(a))

using namespace std;
struct edge{
	int to,w;
	int next;
}E[2050];
class FlowerGarden
{
	public:
		int n;
		int ip;
		int in[1005];
		int head[1005];
		void init()
		{
			ip=0;
			cle(in);
			memset(head,-1,sizeof(head));
		}
		void add(int x,int y){
			in[y]++;
			E[ip].to=y;
			E[ip].next=head[x];
			head[x]=ip++;
		}
    vector <int> getOrdering(vector <int> height, vector <int> bloom, vector <int> wilt)
       {
	      n=height.size();
	      int i,j,k;
          vector<int>ans;
	      ans.clear();
	      init();
	      rep(i,n){
	    	   rep(j,n){
		        if(i==j)continue;
		    	   if(height[i]<height[j]){
					  if(!((wilt[j]<bloom[i])||(bloom[j]>wilt[i])))
		    	        	add(height[i],height[j]);
		    	  }
		     }
	     }
	     int used[1500];cle(used);
	     for(i=1;i<=n;i++)
	     {
	     	int maxnum=0;
	    	for(j=0;j<n;j++)
	    	{
		    	if(used[height[j]])continue;
		    	if(in[height[j]]==0)
		    	{
		    		maxnum=max(maxnum,height[j]);
		    	}
				//break;
			}
	    	used[maxnum]=1;
        	ans.push_back(maxnum);
			for(k=head[maxnum];k!=-1;k=E[k].next)in[E[k].to]--;
	    }
    	return ans;
   }
};
int main()
{
#ifndef ONLINE_JUDGE
     freopen("in.txt","r",stdin);
     //freopen("out.txt","w",stdout);
#endif
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章