HDU 4415 Assassin's Creed, hdu4415

Source: Internet
Author: User

HDU 4415 Assassin's Creed, hdu4415

It's hard to think, greedy. Others' ideas: Click to open the link

Problem DescriptionEzio Auditore is a great master as an assassin. now he has prowled in the enemies 'base successfully. he finds that the only weapon he can use is his cuff sword and the sword has durability m. there are n enemies he wants to kill and killing each enemy needs Ai durability. every time Ezio kills an enemy he can use the enemy's sword to kill any other Bi enemies without wasting his cuff sword's durability. then the enemy's sword will break. as a master, Ezio always want to do things perfectly. he decides to kill as incluenemies as he can using the minimum durability cost.
 
InputThe first line contains an integer T, the number of test cases.
For each test case:
The first line contains two integers, above mentioned n and m (1 <= n <= 10 ^ 5, 1 <= m <= 10 ^ 9 ).
Next n lines, each line contains two integers Ai, Bi. (0 <= Ai <= 10 ^ 9, 0 <= Bi <= 10 ).
 
OutputFor each case, output "Case X:" (X is the case number starting from 1) followed by the number of the enemies Ezio can kill and the minimum durability cost.
 
Sample Input
23 54 15 17 72 12 24 0
 
Sample Output
Case 1: 3 4Case 2: 0 0
 
Source2012 ACM/ICPC Asia Regional Hangzhou Online
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=1e5+10;struct node{    int w,s;}e[maxn];int visit[maxn];int t,n,s;int ans1,ans2,rs1,rs2;int cmp(node l1,node l2){    return l1.w<l2.w;}void solve_1(){    int i;    rs1=s;    ans1=0;    for(int i=0;i<n;i++)    {        if(e[i].s)  continue;        if(rs1>=e[i].w)        {            ans1++;            rs1-=e[i].w;        }        else   break;    }}void solve_2(){    int i;    rs2=s;    ans2=0;    memset(visit,0,sizeof(visit));    for(i=0;i<n;i++)    {        if(e[i].s)  break;    }//    cout<<"eeee  "<<i<<endl;    if(i>=n)   return ;    if(e[i].w>rs2)  return ;    int sum=0;    for(int i=0;i<n;i++)       sum+=e[i].s;//    cout<<"222  "<<endl;    if(sum+1>=n) {ans2=n;rs2-=e[i].w;return ;}//    cout<<"11111  "<<endl;    visit[i]=1;    ans2=sum+1;    rs2-=e[i].w;    for(i=n-1;i>=0;i--)    {        if(!sum)   break;        if(!visit[i])        {            visit[i]=1;            sum--;        }    }    for(i=0;i<n;i++)    {//        cout<<"fuck  "<<endl;        if(visit[i]) continue;        if(rs2<e[i].w)  break;        ans2++;        rs2-=e[i].w;    }}int main(){    int cas=0;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&s);        for(int i=0;i<n;i++)            scanf("%d%d",&e[i].w,&e[i].s);        sort(e,e+n,cmp);        solve_1();        solve_2();//        cout<<"fuck   "<<ans1<<" "<<rs1<<" "<<ans2<<" "<<rs2<<endl;        printf("Case %d: ",cas++);        if(ans1>ans2||(ans1==ans2&&rs1>rs2))            printf("%d %d\n",ans1,s-rs1);        else            printf("%d %d\n",ans2,s-rs2);    }    return 0;}



Help me explain the idea of HDU "Constructing Roads In JGShining's Kingdom"

This is the classic longest ascending subsequence.
N (1 ≤ n ≤ 500,000). The data volume is too large, and the normal n ^ 2 algorithm times out .. So we must use the nlogn algorithm ..
If the landlord doesn't, he can use Baidu's documents. There are a lot of materials on the Internet ..
Paste my code...
# Include <stdio. h>
# Include <string. h>
# Define N 500005
# Define MEN (a, B) memset (a, B, sizeof ())
# Define Max (a, B) (a)> (B )? (A) (B ))
# Define Min (a, B) (a) <(B )? (A) (B ))
Int a [N], B [N], len;
Int search (int I)
{
Int l, r, mid;
L = 1, r = len;
While (l <= r)
{
Mid = (l + r)/2;
If (a [I]> = B [mid])
L = mid + 1;
Else
R = mid-1;
}
Return l;

}
Intmain ()
{
Int n, I, r, p, pos, k = 1;
While (~ Scanf ("% d", & n ))
{
For (I = 0; I <n; I ++)
{
Scanf ("% d", & r, & p );
A [r] = p;
}
B [1] = a [1];
Len = 1;
For (I = 2; I <= n; I ++)
{
If (a [I]> B [len])
B [++ len] = a [I];
Else
{
Pos = search (I );
B [pos] = a [I];
}
}
Printf ("Case % d: \ n", k ++ );
If (len = 1)
Printf ("My king, at most 1 road can be built. \ n ");
Else
Printf ("My king, at most % d roads can be built. \ n", len );
}
Return0;
}... Remaining full text>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.