UVA 11039 Building designing 貪心

來源:互聯網
上載者:User

標籤:

題目連結:UVA - 11039

題意描述:建築師設計房子有兩條要求:第一,每一層樓的大小一定比此層樓以上的房子尺寸要大;第二,用藍色和紅色為建築染色,每相鄰的兩層樓不能染同一種顏色。現在給出樓層數量和每層樓的尺寸(樓層尺寸的大小沒有按照順序給出),求出滿足這樣要求的最大樓層數。

演算法分析:把樓層尺寸按照從大到小排序,然後遍曆一次的同時記錄相鄰樓層所染顏色不同,把不滿足要求的樓層去掉即可。

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<cmath> 6 #include<algorithm> 7 #define inf 0x7fffffff 8 using namespace std; 9 const int maxn=500000+10;10 11 int n,an[maxn];12 13 int cmp(int i,int j)14 {15     i=abs(i) ;j=abs(j) ;16     return i>j;17 }18 19 int main()20 {21     int t;scanf("%d",&t);22     while (t--)23     {24         scanf("%d",&n);25         for (int i=0 ;i<n ;i++) scanf("%d",&an[i]);26         sort(an,an+n,cmp);27         int cnt=1,flag= an[0]>0 ? 1 : -1 ;28         for (int i=1 ;i<n ;)29         {30             int f= an[i]>0 ? 1 : -1 ;31             while (f==flag && i<n)32             {33                 i++;34                 f= an[i]>0 ? 1 : -1 ;35             }36             if (f != flag && i<n)37             {38                 cnt ++ ;39                 flag=f;40                 i ++ ;41             }42         }43         printf("%d\n",cnt);44     }45     return 0;46 }

 

UVA 11039 Building designing 貪心

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.