题解分享
题解分享简介
阿里巴巴与金币 - 题解
```cpp
// https://dashoj.com/p/77
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct g {
ll m; // 重量
ll u; // 价值
double x; // 每一重的价值
};
ll n, t;
vector<g> gs;
int main() {
cin >> n >> t;
for (int i = 0; i < n; i++) {
ll m, u;
cin >> m >> u;
gs.push_back({m, u});
gs[i].x = (double) u / (double) m;
}
sort(gs.begin(), gs.end(), [](g a, g b) {
return a.x > b.x; // 按价值 / 重量从大到小排序
});
double ans = 0; // 答案
for (int i = 0; i < n; ++i) {
if (t >= gs[i].m) {
t -= gs[i].m;
ans += gs[i].u;
} else {
ans += t * gs[i].x;
break;
}
}
printf("%.2lf", ans);
return 0;
}
```
查看全文
0
0
2
1
阿里巴巴与金币 - 题解
```
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Coin {
int weight;
int value;
double density;//价值密度
};
bool compareDensity(const Coin& a, const Coin& b) {
return a.density > b.density;
}
int main() {
int N, T;
cin >> N >> T;
vector<Coin> coins(N);
for (int i = 0; i < N; i++) {
cin >> coins[i].weight >> coins[i].value;
coins[i].density = (double)coins[i].value / coins[i].weight;
}
sort(coins.begin(), coins.end(), compareDensity);
double totalValue = 0.0;
int remainingWeight = T;
for (int i = 0; i < N && remainingWeight > 0; i++) {
if (coins[i].weight <= remainingWeight) {
totalValue += coins[i].value;
remainingWeight -= coins[i].weight;
} else {
totalValue += coins[i].density * remainingWeight;
remainingWeight = 0;
}
}
printf ("%.2f", totalValue);
}
```
查看全文
0
0
1
1
阿里巴巴与金币 - 题解
```
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct node
{
int m,v;
double x;
};
bool cmp(node &a,node &b)
{
if(a.x>b.x)
return 1;
return 0;
}
vector<node> vec;
int main()
{
int N,T,m,v;
double x;
cin>>N>>T;
for(int i=0;i<N;i++)
{
cin>>m>>v;
x=(double)v/(double)m;
vec.push_back({m,v,x});
}
double sum=0.0;
sort(vec.begin(),vec.end(),cmp);
for(int i=0;i<N;i++)
{
if(T==0)
break;
if(T>=vec[i].m)
{
sum+=vec[i].v;
T-=vec[i].m;
}
else{
sum+=T*vec[i].x;
T=0;
}
}
printf("%.2lf",sum);
}
```
查看全文
0
0
1
0
阿里巴巴与金币 - 题解
70%,不知道哪里错了,有佬能帮看看嘛
```
#include <bits/stdc++.h>
using namespace std;
struct Gold
{
int m;
int v;
double aver;
bool operator<(const Gold& other) const
{
return aver < other.aver;
}
};
int main()
{
int N, T;
int m, v;
cin >> N >> T;
double sum = 0;
priority_queue<Gold> q;
for(int i = 0; i < N; i++)
{
cin >> m >> v;
q.push({m, v, (double)v/m});
}
while(T > 0)
{
Gold g = q.top();
q.pop();
// 背包能装下现在的所有金币
if(T > g.m)
{
sum += g.v;
}
else
{
sum += T * g.aver;
}
T -= g.m;
}
printf("%.2lf", sum);
}
```
查看全文
0
0
0
3
阿里巴巴与金币 - 题解
```
#include <bits/stdc++.h>
using namespace std;
struct treasure{ //连锁数据排序
double m, v, rate;
};
bool cmp(treasure a, treasure b)
{
return a.rate>b.rate;
}
int main()
{
int N, T;
cin >> N >> T;
vector<treasure> treasures(N);
double sum=0;
for(int i=0; i<N; i++)
{
cin >> treasures[i].m >> treasures[i].v;
treasures[i].rate=treasures[i].v/treasures[i].m;
}
sort(treasures.begin(), treasures.end(), cmp);
for(int i=0; i<N; i++)
{
if(T>=treasures[i].m) //尽量不造成出现负数的情况
{
T-=treasures[i].m;
sum+=treasures[i].v;
}
else
{
sum+=treasures[i].rate*T;
T=0;
break;
}
}
printf("%.2lf", sum);
return 0;
}
```
查看全文
0
0
0
5
阿里巴巴与金币 - 题解
include
include
include
include
using namespace std;
struct gold
{
double value;
double weight;
double rate;
};
bool cmp(const gold& t1,const gold& t2)
{
return t1.rate > t2.rate;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, t;
double numt = 0; double numv=0;
vector
v;
cin >> n >> t; if (n
100 || t>1000)return 0;
v.resize(n);
for (int i = 0; i
> v[i].weight >> v[i].value; if (v[i].value
120)return 0;
v[i].rate = v[i].value / v[i].weight;
}
sort(v.begin(),v.end(),cmp);
int i = 0;
while (numt
t)
{
numv-=(numt - t) v[i-1].rate;
}
cout << fixed<<setprecision(2)<<numv;
return 0;
}
查看全文
0
0
0
1
阿里巴巴与金币 - 题解
```
#include<iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
const int N=102;
int n,zhilian;
struct gold{
double weight;
double worth;
double every;
}go[N];
bool compair(gold a,gold b){
return a.every>b.every;
}
int main(){
cin>>n>>zhilian;
int i=0;
int t=n;
while(t--){
int x1,x2;
scanf("%d%d",&x1,&x2);
go[i].weight=x1,go[i].worth=x2;
go[i].every=(double)x2/x1;
i++;
}
sort(go,go+n,compair);
double res=0;
for(int j=0;j<n;j++){
if(zhilian>go[j].weight) res+=go[j].worth,zhilian-=go[j].weight;
else {
res+=go[j].every*zhilian;
break;
}
}
printf("%.2lf",res);
return 0;
}
```
查看全文
0
0
0
1
阿里巴巴与金币 - 题解
/上天啊 难道你看不出我很爱她/
include
using namespace std;
struct Node{
double m,v;
double avg;
}a[110];
bool cmp(Node x,Node y)
{
return x.avg>y.avg;
}
int main()
{
int n,t;cin>>n>>t;
for(int i = 1;i
>a[i].m>>a[i].v;
a[i].avg = a[i].v/a[i].m;
}
sort(a+1,a+1+n,cmp);
double sum = 0;
for(int i = 1;i
=a[i].m)
{
sum = sum+a[i].v;
t = t-a[i].m;
}
else
{
sum = sum+t1.0a[i].avg;
break;
}
}
cout<<fixed<<setprecision(2)<<sum;
}
查看全文
0
0
0
1



