码迷,mamicode.com
首页 > 其他好文 > 详细

HDU - 6011 Lotus and Characters

时间:2017-07-15 14:00:51      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:print   out   tor   panel   one   output   rac   contain   scanf   

仔细读题,然后来一发大模拟!

Lotus has nn kinds of characters,each kind of characters has a value and a amount.She wants to construct a string using some of these characters.Define the value of a string is:its first character‘s value*1+its second character‘s value *2+...She wants to calculate the maximum value of string she can construct. 
Since it‘s valid to construct an empty string,the answer is always 0≥0。

InputFirst line is T(0T1000)T(0≤T≤1000) denoting the number of test cases. 
For each test case,first line is an integer n(1n26)n(1≤n≤26),followed by nn lines each containing 2 integers vali,cnti(|vali|,cnti100)vali,cnti(|vali|,cnti≤100),denoting the value and the amount of the ith character. 
OutputFor each test case.output one line containing a single integer,denoting the answer. 
Sample Input

2
2
5 1
6 2
3
-5 3
2 1
1 1

Sample Output

35
5
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        vector<int> vec;
        int n;
        scanf("%d",&n);
        int allnum=0;
        for(int i=0;i<n;i++)
        {
            int val,num;
            scanf("%d%d",&val,&num);
            allnum+=num;
            while(num--)
            {
                vec.push_back(val);
            }
        }
        //代码里最重要的是dissum这个数,代表着ipos位置后面的数字各取一个的和
        sort(vec.begin(),vec.end());
        int ipos=0;
        long long int disnum=0;
        //int flag=1;
        for(int i=0;i<allnum;i++)
        {
            if(vec[i]<0)
            {
                ipos=max(ipos,i);
            }
            else if(vec[i]>=0)
            {
                disnum+=vec[i];
            }
        }
        //cout<<ipos<<endl;
        long long int ans=0;
        if(ipos!=0)
        {for(int i=ipos+1;i<allnum;i++)
        {
            ans+=vec[i]*(i-ipos);
        }
        while((vec[ipos]+disnum)>0&&ipos>=0)
        {
            ans+=(vec[ipos]+disnum);
            disnum+=vec[ipos];//dissum是会改变的ipos位置变化会引起这个的变化
            ipos--;
        }}
        else
        for(int i=0;i<allnum;i++)
        {
            ans+=vec[i]*(i+1);
        }
        //cout<<ans<<endl;

        printf("%lld\n",ans);
    }
}

  

HDU - 6011 Lotus and Characters

标签:print   out   tor   panel   one   output   rac   contain   scanf   

原文地址:http://www.cnblogs.com/masterchd/p/7182319.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!