zoukankan      html  css  js  c++  java
  • 家庭作业 (Standard IO)

    Description

      老师在开学第一天就把所有作业都布置了,每个作业如果在规定的时间内交上来的话才有学分。每个作业的截止日期和学分可能是不同的。例如如果一个作业学分为10,要求在6天内交,那么要想拿到这10学分,就必须在第6天结束前交。
      每个作业的完成时间都是只有一天。例如,假设有7次作业的学分和完成时间如下:
      作业号 1 2 3 4 5 6 7
      期限 1 1 3 3 2 2 6
      学分 6 7 2 1 4 5 1
      最多可以获得15学分,其中一个完成作业的次序为2,6,3,1,7,5,4,注意可能d还有其他方法。
      你的任务就是找到一个完成作业的顺序获得最大学分。

    Input

      第一行一个整数N,表示作业的数量。接下来N行,每行包括两个整数,第一个整数表示作业的完成期限,第二个数表示该作业的学分。

    Output

      输出一个整数表示可以获得的最大学分。保证答案不超过longint范围。

    题解

    就是不停的排序取最大值。

    代码

    #include <stdio.h>    
    #include <iostream>    
    #include <algorithm>    
    #include <sstream>    
    #include <stdlib.h>    
    #include <string.h>    
    #include <limits.h>    
    #include <string>    
    #include <time.h>    
    #include <math.h>    
    #include <queue>    
    #include <stack>    
    #include <map>    
    using namespace std;
    const int maxn=1000005,maxm=700005;
    struct arr{
           int x,y;
    }a[maxn];
    int n,ans,f[maxm],bo[maxm];
    
    int find(int x){
        if(bo[x]==0 || x==0) return x;
        f[x]=find(f[x]);
        return f[x];
    }
    
    int cmp(arr a,arr b){
        return a.y>b.y;
    }
    
    int main()
    {
        cin>>n;
        for(int i=1;i<=n;i++)
                scanf("%ld%ld",&a[i].x,&a[i].y);
        for(int i=1;i<=maxm;i++)
                f[i]=i-1;
        sort(a+1,a+n+1,cmp);
        for(int i=1;i<=n;i++){
                int p=find(a[i].x);
                if(p>0) bo[p]=1,ans+=a[i].y;
        }
        cout<<ans;
        return 0;
    }
  • 相关阅读:
    1月10日 TextView
    1月9日 布局2
    30 Adapter适配器
    29 个人通讯录列表(一)
    28 ListView控件
    27 登录模板
    26 Activity的启动模式
    25 Activity的生命周期
    24 得到Activity返回的数据
    23 Activity的传值2(bundle)
  • 原文地址:https://www.cnblogs.com/zyx-crying/p/9319610.html
Copyright © 2011-2022 走看看