[2011山東省第二回ACM大学生プログラム設計コンテスト]


Mathmen
Time Limit:1000 ms Memory limit:65536 Kは疑問がありますか?ここを注文します
タイトル:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2168
テーマの説明
Mathmen love mathematmatiics,and they live on the number line.All the mathmen spend all their time on sososoliving matititititttttmatititititittttttttttmablical problblems and proving theorms.Likehhhhmenbeininininins,mashshn theininininins,mattttment t t t t t t t t liment t t t t t t lime me thethethethe lime me me me me me me me me me me me me me thethethethethethethethethethethethe thethethethethethethethethetheベルリンand xiLately、all the mathmen nationhave collaboratototed on a huagproblem、which involves all araras of mathematiics、and each mathmen country reponsible for soliving the party arargoodat.After.Afterhoshshshshshshshshshshshshshshat.Afterhohos s thethethethetheattttttttttttttttttttttttttworss s s s s thethethethethetheattttttttttttttttttttttwojobs.Now,the y need to collect the result in evermathmen country、and cobine the m togethe r to create the solution to the hution to proble.So the y decide to let one mathman collect all the relt and send it to country、where the the the the work.wigelwork。
As mathmen are devoted to solight.The refore,their legs has become very weak after milions of years of evolution.The refore,they have to ride match to travel on the number line.The ire arem typeatch of costs。(yes、you need to be very bravie to take mashps、you would never be able to get back the lost IQ)
The e e are two seasons in the mathmen world Positive and Negative.Now it is Positive、so all the mathships trvels on the number line from left to right.The refore、one man from country 1 must selected the the volustry。
The e is at one mathship of each type in evercountry.So,after picking the reults from country 1,the volunter needs to select one match with a limit of trveling distance at least the distance 1
Meaawhile、this trip will cot him the corerereponding amount of IQ.The n、he picks the rerelt from country 2、chose anotheheheattmashshshap(the old mashshshap will be mantained countrtry 2、andandandandand can countrtry 2、and and can can can can can can conconconconconconcontrtrtrtrtrtrtrtrtrininininininininstststststststststy 2、andt t ststststststststststststststststststy 2、andt t stststststststs country n.
Mathmen care about their IQ a lot、そしてthe volunter wants to minimize the total cost of his IQ.Could you please write program to help hit select the right mashps to tate?
入力
The input contains multiple test cases.The first line of the input is an integer C,indicating the number of test cases in the input.
Each test case begins with a line containing two integers,n(2<=n==10000)and m(1<=m==100000)、which is the number of mathmen countries and the number of mathtypes.The next line contrings^indicating the positions of the mathmen countries.The n,m lines follows,each containing two non-negative integers no more 2*109,which are the limit of trveling distance and the costs of IQ.
出力
For each test case,output one line containing the minimum cot of IQ.If the volunter can never reach country,output「Impossible」(without quot tation marks).
例の入力
2
3
0 3 10
1 0
6 1
10
2 1
0 1000
100
サンプル出力
11
Impossible
この問題は試合の時、私たちは通訳の問題がないのは唯一です。実はこの問題は簡単です。欲張りでいいです。
最初の都市から最後の都市に行くには、どの都市にもm種の交通手段があります。
すべての交通機関にガソリンを満タンにしてから、一番多く走る距離とガソリン量を提供します。一番目の都市から最後の都市までのガソリン消費は最低いくらですか?
(上記の油は、私が加えたものです。よく分かります。届かないなら、Imposibleを出力します。)
以下の点に注意してください。
①都市の座標を示し、小から大まで与える。
②各都市にはm種の交通手段がありますので、一つの交通手段は重複して利用できます。
③1つの交通手段が10キロを走ると仮定し、燃料消費は4であり、都市間の距離が10キロ未満であっても、燃料消費量は4で計算される。
④最小燃費を求める
この問題は、まず二つの隣の都市間の距離を求めて、大きいから小さいまで並べ替えてください。そして各交通機関の到達できる距離を大きいから小さいまで並べ替えてください。まず、交通機関が到達できる最大距離は隣の都市の最大距離より大きいかどうかを判断します。小さいなら、Imposibleです。
大きさが同じであれば、最後の都市に到達することができます。
最大の都市間の間隔から後ろへ回ると、この間隔に到達する交通手段が列に投げ込まれます。
ここのキューは優先順位で、油の量に応じて、燃費の少ないものが列のヘッダにあります。
このサイクルを例で説明します。
ここには2段の距離があります。1段目の距離は10、2段目の距離は4.
交通工は4つあります。
1番 15距離でガソリンを消費する10
二号 10距離でガソリンを消費する 4
3番 距離7  油を使い果たす 6
四号 距離3  油を使い果たす 1
まず第一段の道のりを判断します。
ここには2つの交通手段があります。1番のものと2番のものがあります。
一番目の工具と二番の工具を列に入れます。優先列は油の量によって並べられます。一番上にあるのは二番です。
全体の燃費をsum値で記録します。sum+=4(初期値は0)
第二段の道のりを判断します。距離は4です。この時、一号と二号は相変わらず題意を満たしています。
(ここは捜索中の剪定のようなものです。毎回最初の乗り物から判断する必要はありません。距離はどんどん短くなります。
ですから、前の要求に合う交通手段は後ろに必ず該当します。これらの交通手段は列をキャンセルする必要はありません。列の中に入れるだけです。
三番目のものも満足しています。四番目のものは満足できないので、三番目のものも列に捨てます。
列は油の量によって並べられています。最初の席は相変わらず2番です。sum+=4
(これも説明しています。あなたが行く距離ではなく、ガソリンの消費が少ないです。)
最後にsumを出力できます。
PS:最後に説明します。データタイプの保存はLong longを使います。そうでなければWA。
もう一つのポイントは、大データ量の入力にはscanfを使ったほうがいいです。cinで510 MSを使って、scanfで210 MSを消費します。違いはやはり大きいです。
long long scanf用%lld
#include <stdio.h>
#include <algorithm>
#include <queue>
using namespace std;
struct Tool
{
    long long distance;
    long long qi;
    friend bool operator <(Tool a,Tool b)
    {
        return a.qi>b.qi;
    }
}tool[100011];
long long n,m,stop[10011];

//    QI
void calculate(void)
{
    int i,j;
    long long sum;
    priority_queue <Tool> q;
    while(!q.empty())   {q.pop();}

    sum=j=0;
    for(i=0;i<n-1;++i)
    {
        while(stop[i]<=tool[j].distance && j<m)
        {
            q.push(tool[j]);
            ++j;
        }
        sum+=q.top().qi;
    }
    printf("%lld
",sum); } // tool stop bool cmp_s(int a,int b) { return a>b; } bool cmp_t(Tool a,Tool b) { if(a.distance==b.distance) return a.qi<b.qi; return a.distance>b.distance; } int main() { int i,test; scanf("%d",&test); while(test--) { scanf("%d%d",&n,&m); /* // , 20MS long long temp; scanf("%lld",&temp); for(i=0;i<n-1;++i) { scanf("%lld",&stop[i]); stop[i]-=temp; temp=stop[i]+temp; } */ for(i=0;i<n;++i) scanf("%lld",&stop[i]); for(i=0;i<n-1;++i) stop[i]=stop[i+1]-stop[i]; // tool qi for(i=0;i<m;++i) scanf("%lld%lld",&tool[i].distance,&tool[i].qi); // stop tool sort(stop,stop+n-1,cmp_s); sort(tool,tool+m,cmp_t); if( stop[0]>tool[0].distance ) printf("Impossible
"); else calculate(); } return 0; }