1. 程式人生 > 其它 >P1475 智力大沖浪

P1475 智力大沖浪

小偉報名參加中央電視臺的智力大沖浪節目。本次挑戰賽吸引了眾多參賽者,主持人為了表彰大家的勇氣,先獎勵每個參賽者 m 元。先不要太高興!因為這些錢還不一定都是你的?!接下來主持人宣佈了比賽規則:首先,比賽時間分為 n 個時段 (n≤500),它又給出了很多小遊戲,每個小遊戲都必須在規定期限 ti 前完成 (1≤ti≤n)。如果一個遊戲沒能在規定期限前完成,則要從獎勵費 m 元中扣去一部分錢 wi,wi 為自然數,不同的遊戲扣去的錢是不一樣的。當然,每個遊戲本身都很簡單,保證每個參賽者都能在一個時段內完成,而且都必須從整時段開始。主持人只是想考考每個參賽者如何安排組織自己做遊戲的順序。作為參賽者,小偉很想贏得冠軍,當然更想贏取最多的錢!注意:比賽絕對不會讓參賽者賠錢!

輸入描述

多組輸入,每組共 4 行,

第 1 行為 m,表示一開始獎勵給每位參賽者的錢;

第 2 行為 n,表示有 n 個小遊戲;

第 3 行有 n 個數,分別表示遊戲 1 到 n 的規定完成期限;

第 4 行有 n 個數,分別表示遊戲 1 到 n 不能在規定期限前完成的扣款數。

輸出描述

對於每組輸入,僅 1 行,表示小偉能贏取最多的錢

樣例輸入

Copy to Clipboard
10000
7
4 2 4 3 1 4 6
70 60 50 40 30 20 10

樣例輸出

Copy to Clipboard
9950


思路:將time,cost寫成結構體;用sort排序,cost最高的先放,每次放都儘量往後放.
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct game{
    int time;
    int cost;
};
int m,n,s[501]={0},sum1=0,sum2=0,maxtime=0;
struct game a[501];
bool cmp(game a,game b){
    return a.cost>b.cost;
}
int main()
{
    while(cin>>m){ 
        memset(s,
0,sizeof(s)); sum1=0; maxtime=0; sum2=0; cin>>n; for(int i=1;i<=n;i++){ cin>>a[i].time; if(a[i].time>maxtime) maxtime=a[i].time;} for(int i=1;i<=n;i++){ cin>>a[i].cost; sum1+=a[i].cost;} sort(a+1,a+1+n,cmp); for(int i=1;i<=n;i++) { for(int j=a[i].time;j>=1;j--) { if(s[j]==0){ s[j]=a[i].cost; break;} } } for(int i=1;i<=maxtime;i++) sum2+=s[i]; //cout<<"sum1:"<<sum1<<"sum2:"<<sum2<<endl; cout<<m-(sum1-sum2)<<endl; } return 0; }