3 条题解
-
2
看题是以为是动态规划,结果是贪心
#include <bits/stdc++.h> using namespace std; int n, t; double ans; struct Node { int w, v; }a[105]; bool cmp(Node x,Node y) { return x.v * y.w > x.w * y.v; } int main() { cin >> n >> t; for (int i = 1; i <= n; i++) cin >> a[i].w >> a[i].v; sort (a + 1, a + n + 1, cmp); for (int i = 1; i <= n; i++) { if (a[i].w <= t) { ans += a[i].v; t -= a[i].w; } else { ans += a[i].v * t * 1.0 / (a[i].w * 1.0); break; } } cout << fixed << setprecision(2) << ans << endl; return 0; }
-
0
#include<bits/stdc++.h> int n,T; double ans; struct node { int m,v; }a[101]; bool cmp(node &x,node &y) { return x.v*y.m>=x.m*y.v; } int main() { std::cin>>n>>T; for(int i=1;i<=n;i+=1)std::cin>>a[i].m>>a[i].v; std::sort(a+1,a+n+1,cmp); for(int i=1;i<=n;i+=1) { int x=std::min(T,a[i].m); ans+=(double)x/a[i].m*a[i].v; T-=x; } printf("%.2f\n",ans); }
-
0
#include <iostream> #include <cstdio> #include <algorithm> struct coin{int m,v;}coins[105]; int yasuo(coin a,coin b){ return a.v*b.m>b.v*a.m;} int main(){ int n,t;double x,ans=0; std::cin>>n>>t; for(int i=0;i<n;i++)std::cin>>coins[i].m>>coins[i].v; std::sort(coins,coins+n,yasuo); for(int i=0;i<n;i++){ x=std::min(t,coins[i].m); ans+=x*coins[i].v/coins[i].m; t-=x;} printf("%.2f",ans); return 0;}
- 1
信息
- ID
- 554
- 时间
- 1000ms
- 内存
- 125MiB
- 难度
- 6
- 标签
- 递交数
- 1099
- 已通过
- 373
- 上传者