题目描述:
B. Carries
Time Limit: 1000ms
Memory Limit: 65536KB
64-bit integer IO format: %lld Java class name: Main
Submit Status
frog has n integers a1,a2,…,an, and she wants to add them pairwise.
Unfortunately, frog is somehow afraid of carries (进位). She defines \emph{hardness} h(x,y) for adding x and y the number of carries involved in the calculation. For example, h(1,9)=1,h(1,99)=2.
Find the total hardness adding n integers pairwise. In another word, find
∑1≤i
题解:
当时真是傻..
首先肯定是按照位来计算,然后麻烦的是进位的计算.
但是但是!! 进位是一个很好搞的东西,排个序,那么比一个特定数大的值都可以进位!!!! 之后就很好写了呗. 可以每次排序,然后二分找,也可以用两个指针o(n)搞
重点:
想到了进位不会,那么继续探索怎么搞定进位?大于一个数就会进位,那么后面的一段都会进位
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <ctype.h>
#include <limits.h>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
#define CLR(a) memset(a, 0, sizeof(a))
#define REP(i, a, b) for(ll i = a;i < b;i++)
#define REP_D(i, a, b) for(ll i = a;i <= b;i++)
typedef long long ll;
using namespace std;
const ll maxn = 1e5 + 100;
struct info
{
ll number, weight;
info(ll _number = 0, ll _weight = 0)
{
number = _number;
weight = _weight;
}
bool operator < (const info b) const
{
return weight < b.weight;
}
};
info a[maxn];
ll b[maxn], n;
ll num[maxn][10];
ll p10[20];
void solve()
{
ll ans = 0;
for(ll s = 1;s<=9;s++)
{
for(ll i = 1;i<=n;i++)
{
a[i].number = (b[i]/p10[s-1])%10;
a[i].weight = b[i]%p10[s-1];
}
sort(a+1, a+1+n);
// printf("----\n");
// for(ll j = 1;j<=n;j++)
// printf("%d ", a[j].weight);
// printf("\n");
for(ll i = 0;i<=9;i++)
num[n+1][i] = 0;
for(ll i = n;i>=1;i--)
{
for(ll j = 0;j<=9;j++)
num[i][j] = num[i+1][j];
num[i][a[i].number]++;
}
for(ll i = 1;i<=n;i++)
{
for(ll j = 10 - a[i].number;j<=9;j++)
ans += num[i+1][j];
// printf("----\n");
// for(ll j = 1;j<=n;j++)
// printf("%d ", a[j].weight);
// printf("\n");
info tmp;
tmp.weight = p10[s-1]-a[i].weight;
ll pos = lower_bound(a+1, a+1+n, tmp) - a;
pos = max(pos, i+1);
ans += num[pos][9-a[i].number];
}
}
printf("%lld\n", ans);
}
int main()
{
freopen("2Bin.txt", "r", stdin);
//freopen("3Bout.txt", "w", stdout);
p10[0] = 1;
for(ll i = 1;i<=9;i++)
p10[i] = p10[i-1]*10;
while(scanf("%lld", &n) != EOF)
{
for(ll i = 1;i<=n;i++)
scanf("%lld", &b[i]);
solve();
}
return 0;
}