BestR #31

hdu 5178

求|a[i] - a[j]| <= k (i < j) <i,j>的对数,一开始认为数据不大就直接ans++了,后来结果出来才知道,啊啊啊,too young too simple。总之一个教训

思路:先排序,然后用二分查找寻找a[i] + k 在数组中的位置,然后 ans相加

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <stack>
#include <cctype>
#include <string>
#include <malloc.h>
#include <queue>
#include <map>

using namespace std;

const int INF = 0xffffff;
const double esp = 10e-8;
const double Pi = 4 * atan(1.0);
const int maxn = 100000+10;
const long long mod =  1000000007;
const int dr[] = {1,0,-1,0,-1,1,-1,1};
const int dc[] = {0,1,0,-1,1,-1,-1,1};
typedef long long LL;

LL gac(LL a,LL b){
    return b?gac(b,a%b):a;
}

long long a[maxn];

int main()
{
#ifndef ONLINE_JUDGE
    freopen("inpt.txt","r",stdin);
   // freopen("output.txt","w",stdout);
#endif
    int t;
    int n,k;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&k);
        memset(a,0,sizeof(a));
        for(int i = 0;i < n;i++){
            scanf("%I64d",&a[i]);
        }
        sort(a,a+n);
        long long ans = 0;
        for(int i = 0;i < n-1;i++){
            long long tmp = a[i] + k;
            int m = upper_bound(a,a+n,tmp)- a;
            ans += (m-i-1);
        }
        printf("%I64d
",ans);
    }
    return 0;
}
View Code

hdu 5179

题意是:一个数如果其低位小于等于高位,并且高位是相邻地位的整数倍则称为魅力的数,要求求出[L,R]范围内美丽数的个数

思路,dfs暴力。

做到了第二题啦~~

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <stack>
#include <cctype>
#include <string>
#include <malloc.h>
#include <queue>
#include <map>

using namespace std;

const int INF = 0xffffff;
const double esp = 10e-8;
const double Pi = 4 * atan(1.0);
const int maxn = 100000+10;
const long long mod =  2147483647;
const int dr[] = {1,0,-1,0,-1,1,-1,1};
const int dc[] = {0,1,0,-1,1,-1,-1,1};
typedef long long LL;

LL gac(LL a,LL b){
    return b?gac(b,a%b):a;
}

int L,R;
int a[20],b[20];
int arr[20];
int cnt;

long long get_num(int x){
    long long tmp = 0;
    for(int i = x-1;i > 0;i--){
        tmp = tmp * 10 + arr[i];
    }
   // cout << tmp << endl;
    return tmp;
}

void dfs(int x){
    long long tt = get_num(x);
    if(tt > R)
        return;
    if(tt >= L)
        cnt++;
    for(int i = 1;i < 10;i++){
        int tmp = arr[x-1] * i;
        if(tmp < 10){
            arr[x] = tmp;
            dfs(x+1);
        }
        arr[x] = 0;
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("inpt.txt","r",stdin);
#endif
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&L,&R);
        memset(arr,0,sizeof(arr));
        cnt = 0;
        arr[0] = 1;
        dfs(1);
        printf("%d
",cnt);
    }
    return 0;
}

第三题什么的就没做出来……

原文地址:https://www.cnblogs.com/hanbinggan/p/4306857.html