Problem D. T-shirts(icpc)

版权声明:文章全是博主转载, 请随意操作。 https://blog.csdn.net/ggqinglfu/article/details/82054987

Problem D. T-shirts
Program: tshirts.(c|cpp|java)
Input: tshirts.in
Balloon Color: Pink
It was the second day of IBM Chill Zone, and it was the time for distributing the prizes. Unfortunately
due to unknown reasons, the organizing committee can only buy T-shirts to the contestants or give them
D dollars in cash. The T-shirts factory only permitted them to order a single bulk of T-shirts of the same
size where a single T-shirt price was determined using this equation:
C × S
Where C is a given constant, and S is the size of the T-shirt.
There was only one limitation on the T-shirts they will give to the contestants, any contestant only accepts
to receive a T-shirt of greater or equal size but not smaller size.
The organizing committee decided to minimize the money they should pay . Please help them in
determining the amount they need to pay in order to give each contestant either a T-shirt or D dollars.
Input
T the number of test cases. For each test case there will be three integers N, D and C , then N integers
representing the size of each of the T-shirt of each of the contestants.
1 ≤ T ≤ 100
1 ≤ N ≤ 100, 000
1 ≤ D ≤ 10, 000
1 ≤ C ≤ 10, 000
1 ≤ t − shirtsize ≤ 100, 000
Output
For each test case print a single line containing: Case_x:_y
x is the case number starting from 1. y is is the required answer.
Replace the underscores with spaces.
Examples
tshirts.in Standard Output
1
5 100 1
35
70
75
90
110
Case 1: 425
Note
The optimal solution is to buy 3 tshirts of size 75 for the first 3 contestants, and give 100 dollars to the
last two
ACM International Collegiate Programming Contest, Syrian Collegiate Programming Contest (2014)
Syria, September, 23, 2014
think:
今天看代码时发现一句 long long ANS= 1LL* *num((1LL)n(n-1))/2; ,其中用了1LL;LL其实代表long long,*1LL是为了在计算时,把**int类型的变量转化为long long然后再赋值给long long类型的变量。代码中的ANS的定义为: long long ANS
ANS是long long类型的, ANS=1LL*num*((1LL)n(n-1))/2;
不至于后面计算溢出,*1LL 之后类型就转换为long long, num,n定义的是int类型的
*1ll 效果一样 .

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 1100000;
typedef long long LL;
int t, n, d, c, a[MAXN];
int main(){

    scanf("%d", &t);
    for(int k = 1; k <= t; k++){
        scanf("%d %d %d", &n, &d, &c);
        for(int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        sort(a + 1, a + n + 1);
        LL ans = 1ll * d * n; // 强制long long 型 转换, 初始化 为 已知最大边界值
        for(int i = 1; i <= n; i++){
            ans = min(ans, 1ll*a[i]*i*c + 1ll*(n-i)*d); 
        }
        printf("Case %d: %lld\n", k, ans);
    }
    return 0;
}

附加补漏知识点一关于%d %3d %-3d %03d 等的区别和列宽的设置方法
1:%nd:n代表的是列宽长度。

 (1)%-nd   -  代表的是左对齐。

 (2)%0nd   0(数字零)代表的是不足n位长度的左补齐0。

2:cout.width(n),设置列宽长度为n。

二、新增知识点二cin,cout的加速
cin慢是有原因的,其实默认的时候,cin与stdin总是保持同步的,也就是说这两种方法可以混用,而不必担心文件指针混乱,同时cout和stdout也一样,两者混用不会输出顺序错乱。正因为这个兼容性的特性,导致cin有许多额外的开销,如何禁用这个特性呢?只需一个语句std::ios::sync_with_stdio(false);,这样就可以取消cin于stdin的同步了。
( 二)、
如何检验是否是保留字
我们都知道,“包子皮”里面有这样一句:

using namespace std;

std是c艹里面的一个名字空间,c++里面有很多函数都是直接声明在std里面的,如果你需要用的话,必须要声明使用std空间才行。如果不用,我们的cin语句:

cin>>x;

就会繁琐地变成:

std::cin>>x;

只要用了using namespace std,就可以省略掉std::的操作。但是我们不知道std空间里面到底有什么函数,所以有时候保留字就会被我们占用导致CE。
如果要避免这个,我们有三种办法:

直接用std::的方法;这样会很麻烦啊;
用局部声明的方法:

using std::cin;
using std::cout;
using std::endl;
int x;
cin>>x;
cout<<x<<endl;

这样我们就是声明了,只使用cin和cout和endl三种,其他的就不用担心了。
3. 用试验的方法;这个很容易理解,就是先编译一个小程序,用using namespace后,把你的变量名当做一个函数名,然后如果报了错就没事,如果没报错就呵呵了。
4. 最简单求gcd的方法

int m,n;
m=2;
n=5;
int g=__gcd(m,n);

猜你喜欢

转载自blog.csdn.net/ggqinglfu/article/details/82054987