题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4544
#include<iostream> #include<stdio.h> #include<cstring> #include<algorithm> #include<queue> using namespace std; struct date { int val,harm; bool operator <( const date &a )const { return val < a.val; } }node[112345],temp; int arr[112345]; struct date2 { int sum,lt,rt; }cnt[412345]; void build_tree( int lt,int rt,int t ) { cnt[t].lt = arr[lt]; cnt[t].rt = arr[rt]; cnt[t].sum = rt - lt + 1; if( lt == rt )return; int mid = ( lt + rt )>>1; build_tree( lt,mid,t<<1 ); build_tree( mid+1,rt,t<<1|1 ); } bool query( int harm,int t ) { if( cnt[t].lt == cnt[t].rt ){ cnt[t].sum--;return true; } if( cnt[t<<1|1].sum && cnt[t<<1|1].lt <= harm ) { bool fell = query( harm,t<<1|1 ); if( fell ) { cnt[t].sum--; return fell; } } if( cnt[t<<1].sum && cnt[t<<1].lt <= harm ) { bool fell = query( harm,t<<1 ); if( fell ) { cnt[t].sum--; return fell; } } return false; } int main( ) { int N,M,i,j; while( scanf("%d%d",&N,&M) != EOF ) { for( i = 1; i <= N; i++ ) scanf( "%d",&arr[i] ); sort( &arr[1],&arr[1] + N ); build_tree( 1,N,1 ); for( i = 1; i <= M; i++ ) scanf( "%d",&node[i].harm ); for( i = 1; i <= M; i++ ) scanf( "%d",&node[i].val ); sort( &node[1],&node[1]+M ); long long val = 0;int ans = 0; if( N > M ) { printf("No\n"); continue; } for( i = 1; i <= M; i++ ) { if( query( node[i].harm,1 ) ) { val += (long long)node[i].val; ans++; if( ans == N ) break; } } if( ans == N )printf("%I64d\n",val); else printf("No\n"); } return 0; }