当前位置:网站首页>Basic greedy summary

Basic greedy summary

2022-04-23 15:44:00 MC happy bitter little fear

/* Basic greed - - The core part of the */

/*
bool cmp ( const Wvs &x, const Wvs &y )
{
return x.v>y.v ;
}

for( int i = 1 ;i <= n; i++ )
{
cin >> wvs[i].w >> wvs[i].v ;
wvs[i].v /= wvs[i].w;
}

sort ( wvs + 1 ,wvs + n + 1 ,cmp ) ;

for( int i = 1 ;i <= n; i++ )
{

int x = min ( C, wvs[i].w);
ans += x * wvs[i].v;
C-=x;
if ( C<=0 ) break ;

}

cout << ans;

Some backpacks __
Strategy : Sort by cost performance , Then take away the parts that can be taken away and put them into the backpack
*/

/*
for ( int i = 1; i <= n; i++ )cin >> v[i] ;
sort ( v + 1, v + n + 1 ) ;

for ( int i = 1; i <= n; i++ )
{
if( c >= v[i] ) k++, c-=v[i];
else break;
}

cout << k;

______ Loading problem
Strategy : Try to pack the smaller weight .
*/

/*

int n, C; cin >> n >> C ;
for ( int i = 1; i <= n; i++ ) cin >> v[i] ;
sort ( v + 1, v + n + 1 ) ;
int i = 1, j = n, ans = 0 ;
while ( i <= j )
{
ans ++;
if( v[i] + v[j]<=C ) i++, j-- ;
else j-- ;
}
cout << ans ;
___ The question of taking a boat
Strategy : Match the heaviest person with the lightest person , If you can't match, let the heavy man make a boat alone .

*/

/*

bool used [N] ;

bool cmp ( const Rlg &r, const Rlg &l )
{
return r.money > l.money ;
}

sort ( rlg + 1, rlg + n + 1, cmp ) ;

for ( int i = 1; i <= n; i++ )
{

bool oks=true;
for ( int r = rlg[i].t; r >= 1; r-- ) 
{
	if ( !used[r] ){ used[r] = true; oks = false; break; }
}
if ( oks ) ans += rlg[i].money ;

}

Task scheduling problem ______
Strategy : Sort by amount of penalty , Then simulate a calendar tag array , Try to schedule back , Leave time for tasks that need to be completed earlier .
/*

版权声明
本文为[MC happy bitter little fear]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231531525348.html