SUSE fourth homework

One: Game
topic explanation link: NOIP2012 improved group rematch king game (greedy solution + high-precision calculation)

Two: LCS
topic explanation link: HDU 1159

Three: Five
: Catalan sequence Catalan sequence template: Catalan sequence

Four:
Save how much can be reduced after compression, sort the difference, record the memory sum that is not compressed, compare this value with the size of the hard disk, if it is larger, reduce the previously recorded difference, and reduce the result by one time+ 1. If all the reductions do not reach the goal, output -1

Code

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

int a[1010];

int main()
{
    
    
    int n, m;
    cin >> n >> m;
    int sum = 0;
	
	for(int i = 0; i < n; i++)
	{
    
    
		int x, y;
		cin >> x >> y;
		sum += x;
		a[i] = x - y;	
	 } 
	 sort(a, a + n);
	 reverse(a, a + n);
	 int ans = 0;
	 int i = 0;
	 while(sum > m)
	 {
    
    
	 	ans++;
	 	sum -= a[i++];
	 	if(i > n)
	 	{
    
    
	 		cout << -1 <<endl;
	 		return 0;
		 }
	 } 
	 cout <<ans << endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45432665/article/details/108054997