6 条题解

  • 1
    @ 2025-1-15 10:25:57
    #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);
    }
    
    
    
    • 1
      @ 2024-12-28 20:08:53
      // 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
        @ 2024-4-12 4:03:35
        #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
          @ 2024-4-11 14:22:52

          #include<iostream> #include<vector> #include<algorithm> #include<iomanip> 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<gold> v; cin >> n >> t; if (n < 0 || t < 0 || n>100 || t>1000)return 0; v.resize(n); for (int i = 0; i < n; i++) { cin >> v[i].weight >> v[i].value; if (v[i].value < 1 || v[i].weight < 1 || 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 && i < n) { numt += v[i].weight;//可能会大于 numv += v[i].value; i++; } if (numt > t) { numv-=(numt - t)* v[i-1].rate; } cout << fixed<<setprecision(2)<<numv; return 0; }

          • 0
            @ 2024-4-8 14:57:09
            #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
              @ 2024-4-8 10:41:33

              /上天啊 难道你看不出我很爱她/ #include<bits/stdc++.h> 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<=n;i++) { cin>>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<=n;i++) { if(t>=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; }

              • 1

              信息

              ID
              77
              时间
              1000ms
              内存
              256MiB
              难度
              7
              标签
              递交数
              396
              已通过
              90
              上传者