The topic is described to an array {a}, which defines H (A,B) as the difference between A + B and a in decimal, and the number of digits 0 is 1.
Enter a description:
The first line reads a positive integer n (1 <= n <= 105).
The second line reads n nonnegative integers, and the first one represents A[i] (0 <= A[i] <= 108). Output Description:
One line indicates the answer.
Example 1
input
0 1 2 3 4 5 6 7 8 9
Output
20
Ideas:
Consider lowering the constants.
The problem is simple, we just need to think about how much more than he has in the back of each number and can carry it. To observe the range of data, we just have to 10-a "I", 100-a "I", 1000-a "I" ... and so on. In turn, the minimum number of values needed to carry a "I" is computed, and then the tree array is done.
The problem is not difficult, in the beginning will be too many discretization led to timeout tat ...
AC Code:
#include <bits/stdc++.h> using namespace std;
/***********************************/int n;
int tree[1000800];
int lowbit (int x) {return x& (-x);} int sum (int x) {int sum=0;
while (x>0) {sum+=tree[x];
X-=lowbit (x);
return sum;
} void Add (int x,int c) {while (x<=n) {tree[x]+=c;
X+=lowbit (x); }/***********************************/struct Node {int val,pos;}
A[100050],B[100050];
int pos[100050];
int CMP (node A,node b) {return a.val<b.val;} int cmp2 (node A,node b) {return a.pos<b.pos;} int main () {
while (~SCANF ("%d", &n)) {for (int i=1;i<=n;i++) {scanf ("%d", &a[i].val);
A[i].pos=i;
B[i].val=a[i].val;
B[i].pos=i;
Sort (a+1,a+1+n,cmp);
for (int i=1;i<=n;i++) pos[a[i].pos]=i;
Sort (A+1,A+1+N,CMP2);
Sort (b+1,b+1+n,cmp);
memset (tree,0,sizeof (tree)); Long long int ans=0;
for (int i=n;i>=1;i--) {int temp=1;
for (int j=1;j<=9;j++) {temp*=10;
if (temp<=a[i].val) continue;
else {int p=-1;
int l=1;
int r=n;
int flag=0;
while (r-l>=0) {int mid= (L+R)/2;
if (b[mid].val>=temp-a[i].val) {r=mid-1;
P=mid;
else l=mid+1;
} if (p==-1) continue;
ans+= (sum (n)-sum (pos[b[p].pos]-1));
} add (pos[i],1);
printf ("%lld\n", ans); }
}