贪心求助
  • 板块学术版
  • 楼主TARGETMINE
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/7/29 19:39
  • 上次更新2024/7/29 20:46:17
查看原帖
贪心求助
935263
TARGETMINE楼主2024/7/29 19:39

https://www.luogu.com.cn/problem/P2240题目链接

#include <iostream>
#include <cstring> 
#include <algorithm>
#include <iomanip> 

using namespace std;

int N, T;
int m[105], v[105];
double p[105]; 
int p_under[105]; 
double ans;
 
void bubbleSort(double arr[], int n, int p_under[]) {
    for (int i = 0; i < n - 1; i++) {  // 循环n-1次
        for (int j = 0; j < n - i - 1; j++) {  // 每次循环比较n-i-1次
            if (arr[j] > arr[j+1]) {  // 如果当前位置比下一个位置大,则交换它们的值
            	int temp = p_under[j];
            	p_under[j] = p_under[j+1];
            	p_under[j+1] = temp;
//                int temp = arr[j];
//                arr[j] = arr[j+1];
//                arr[j+1] = temp;
            }
        }
    }
}


int main()
{
	double theend;
	cin >> N >> T;
	for (int i=0;i<N;i++)
	{
		cin >> m[i] >> v[i];
		p[i] = v[i] / m[i];
	}
	for (int i=0;i<N;i++)
	{
		p_under[i] = i;
	}
	bubbleSort(p, N, p_under);

	
	for (int i=N-1;i>=0;i--)
	{
		if (T-m[p_under[i]]>=0)
		{
			ans+=v[p_under[i]];
			T-=m[p_under[i]];
		}
		else{
			double numerator = v[p_under[i]] * (m[p_under[i]]-T);
			double denominator = m[p_under[i]];
			theend = numerator / denominator;
			ans+=theend;
//			cout << numerator << " " << denominator << " " << theend << endl;
			break;
		}
	}
	cout << fixed << setprecision(2) << ans;
	
	return 0;
 } 

只对了一个 发现还没有输入样例下载 求大佬帮忙

2024/7/29 19:39
加载中...