hdu 4883 區間選點

來源:互聯網
上載者:User

標籤:acm   貪心   

昨天比賽的時候沒有做出來,本來是想用貪心的,但是貪了好久都沒有招,

今天在網上搜瞭解題報告~好像說這是一類區間選點問題:

有一個好的做法:

(1)首先把題目中的時間全轉化為分鐘,那麼區間就在0-1440中間

 (2)對於n組人,有一個si 和一個ei,那麼開個數組, cnt[si]+=d;  cnt[ei]-=d;

  ( 3 ) 也就是說在數軸上講到達的這點加上d,離開的這點減去d;

附上代碼

#include <iostream>
#include <cstdio>
#include <stdio.h>
using namespace std;
int cnt[1445];


int main()
{
 int t,n,d,s1,e1,s2,e2;
 cin>>t;
 while(t--)
 {
   cin>>n;
   memset(cnt,0,sizeof(cnt));
   for(int i=0;i<n;i++)
   {
     scanf("%d%d:%d%d:%d",&d,&s1,&e1,&s2,&e2);
     s1=s1*60+e1;
     s2=s2*60+e2;
     cnt[s1]+=d;
     cnt[s2]-=d;  
   }
   int sum=0,ans=0;
   for(int i=0;i<=1440;i++)
   {
     sum+=cnt[i];
     if(sum>ans)ans=sum;  
   }
   cout<<ans<<endl;
 }
 return 0;   
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.