Sunday, June 15, 2014

C/C++ Int Conversion & Codeforces Round #251 (Div. 2), problem: (B) Devu, the Dumb Guy solution

Codeforces Round #251 (Div. 2), problem: (B) Devu, the Dumb Guy: http://codeforces.com/contest/439/problem/B

Codeforces Round #251 (Div. 2), problem: (B) Devu, the Dumb Guy editorial: http://codeforces.com/blog/entry/12545

Codeforces Round #251 (Div. 2), problem: (B) Devu, the Dumb Guy solution: http://ideone.com/41CElp

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

int c[100005];

int main() {
    long long int n, x, s=0;
    scanf("%I64d%I64d", &n, &x);
    for(int i=0; i<n; i++) scanf("%d", &c[i]);
    sort(c, c+n);
    for(int i=0; i<n; i++) { s+=c[i]*x; if(x>1) x--; }
    printf("%I64d", s);
    return 0;
}
note: i got AC with code shown above but i got WA with code shown below: 
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

int c[100005];

int main() {
    int n, x;
    long long int s=0;
    scanf("%d%d", &n, &x);
    for(int i=0; i<n; i++) scanf("%d", &c[i]);
    sort(c, c+n);
    for(int i=0; i<n; i++) { s+=c[i]*x; if(x>1) x--; }
    printf("%I64d", s);
    return 0;
}

and the reason of that i think is - well, lets open wider topic for that: 

C / C++ number conversion and Casting

i did arithmetic operations on int and assigned it to long long int, but it did not gave the expected result, so lets glance some C / C++ number type conversions and Casting: 

Conditions for Type Conversion
Conditions Met
Conversion
Either operand is of type long double.
Other operand is converted to type long double.
Preceding condition not met and either operand is of typedouble.
Other operand is converted to type double.
Preceding conditions not met and either operand is of typefloat.
Other operand is converted to type float.
Preceding conditions not met (none of the operands are of floating types).
Integral promotions are performed on the operands as follows:
  • If either operand is of type unsigned long, the other operand is converted to type unsigned long.
  • If preceding condition not met, and if either operand is of type long and the other of type unsigned int, both operands are converted to type unsigned long.
  • If the preceding two conditions are not met, and if either operand is of type long, the other operand is converted to type long.
  • If the preceding three conditions are not met, and if either operand is of type unsigned int, the other operand is converted to typeunsigned int.
  • If none of the preceding conditions are met, both operands are converted to type int.


n C++ operators (for POD types) always act on objects of the same type.
Thus if they are not the same one will be promoted to match the other.
The type of the result of the operation is the same as operands (after conversion).
If either is      long          double the other is promoted to      long          double
If either is                    double the other is promoted to                    double
If either is                    float  the other is promoted to                    float
If either is long long unsigned int    the other is promoted to long long unsigned int
If either is long long          int    the other is promoted to long long          int
If either is long      unsigned int    the other is promoted to long      unsigned int
If either is long               int    the other is promoted to long               int
if either is           unsigned int    the other is promoted to           unsigned int
If either is                    int    the other is promoted to                    int
Both operators are promoted to int


Skip to end of metadata
Go to start of metadata
Conversions can occur explicitly as the result of a cast or implicitly as required by an operation. Although conversions are generally required for the correct execution of a program, they can also lead to lost or misinterpreted data. Conversion of an operand value to a compatible type causes no change to the value or the representation.
The C integer conversion rules define how C compilers handle conversions. These rules include integer promotionsinteger conversion rank, and the usual arithmetic conversions. The intent of the rules is to ensure that the conversions result in the same numerical values and that these values minimize surprises in the rest of the computation. Prestandard C usually preferred to preserve signedness of the type.

Integer Promotions

Integer types smaller than int are promoted when an operation is performed on them. If all values of the original type can be represented as an int, the value of the smaller type is converted to anint; otherwise, it is converted to an unsigned int. Integer promotions are applied as part of the usual arithmetic conversions to certain argument expressions; operands of the unary +-, and ~operators; and operands of the shift operators. The following code fragment shows the application of integer promotions:
char c1, c2;
c1 = c1 + c2;
Integer promotions require the promotion of each variable (c1 and c2) to int size. The two int values are added, and the sum is truncated to fit into the char type. Integer promotions are performed to avoid arithmetic errors resulting from the overflow of intermediate values:
signed char cresult, c1, c2, c3;
c1 = 100;
c2 = 3;
c3 = 4;
cresult = c1 * c2 / c3;
In this example, the value of c1 is multiplied by c2. The product of these values is then divided by the value of c3 (according to operator precedence rules). Assuming that signed char is represented as an 8-bit value, the product of c1 and c2 (300) cannot be represented. Because of integer promotions, however, c1c2, and c3 are each converted to int, and the overall expression is successfully evaluated. The resulting value is truncated and stored in cresult. Because the final result (75) is in the range of the signed char type, the conversion from int back to signed char does not result in lost data.

Integer Conversion Rank

Every integer type has an integer conversion rank that determines how conversions are performed. The ranking is based on the concept that each integer type contains at least as many bits as the types ranked below it. The following rules for determining integer conversion rank are defined in the C Standard, subclause 6.3.1.1 [ISO/IEC 9899:2011]:
  • No two signed integer types shall have the same rank, even if they have the same representation.
  • The rank of a signed integer type shall be greater than the rank of any signed integer type with less precision.
  • The rank of long long int shall be greater than the rank of long int, which shall be greater than the rank of int, which shall be greater than the rank of short int, which shall be greater than the rank of signed char.
  • The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, if any.
  • The rank of any standard integer type shall be greater than the rank of any extended integer type with the same width.
  • The rank of char shall equal the rank of signed char and unsigned char.
  • The rank of _Bool shall be less than the rank of all other standard integer types.
  • The rank of any enumerated type shall equal the rank of the compatible integer type.
  • The rank of any extended signed integer type relative to another extended signed integer type with the same precision is implementation-defined but still subject to the other rules for determining the integer conversion rank.
  • For all integer types T1T2, and T3, if T1 has greater rank than T2 and T2 has greater rank than T3, then T1 has greater rank than T3.
The integer conversion rank is used in the usual arithmetic conversions to determine what conversions need to take place to support an operation on mixed integer types.

Usual Arithmetic Conversions

The usual arithmetic conversions are rules that provide a mechanism to yield a common type when both operands of a binary operator are balanced to a common type or the second and third operands of the conditional operator ( ? : ) are balanced to a common type. Conversions involve two operands of different types, and one or both operands may be converted. Many operators that accept arithmetic operands perform conversions using the usual arithmetic conversions. After integer promotions are performed on both operands, the following rules are applied to the promoted operands:
  1. If both operands have the same type, no further conversion is needed.
  2. If both operands are of the same integer type (signed or unsigned), the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
  3. If the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type is converted to the type of the operand with unsigned integer type.
  4. If the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, the operand with unsigned integer type is converted to the type of the operand with signed integer type.
  5. Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type. Specific operations can add to or modify the semantics of the usual arithmetic operations.



No comments:

Post a Comment