[蓝桥杯2018初赛]递增三元组与螺旋折线与日志统计

 递增三元组

给定三个整数数组
A = [A1, A2, ... AN], 
B = [B1, B2, ... BN], 
C = [C1, C2, ... CN],
请你统计有多少个三元组(i, j, k) 满足:
1. 1 <= i, j, k <= N  
2. Ai < Bj < Ck  

输入格式

第一行包含一个整数N。
第二行包含N个整数A1, A2, ... AN。
第三行包含N个整数B1, B2, ... BN。
第四行包含N个整数C1, C2, ... CN。
1 <= N <= 100000 0 <= Ai, Bi, Ci <= 100000 

输出格式

一个整数表示答案

输入样例

3
1 1 1
2 2 2
3 3 3

输出样例

27
#include <bits/stdc++.h>

#define ll long long

using namespace std;

const int maxn = 100010; 

int a[maxn],b[maxn],c[maxn];
int main() 
{ 
    
    int n;    
    cin >> n;
    for (int i = 0; i < n; i++) scanf("%d", &a[i]);
    for (int i = 0; i < n; i++) scanf("%d", &b[i]);
    for (int i = 0; i < n; i++) scanf("%d", &c[i]);
    
    
    sort(a, a + n);    
    sort(b, b + n);    
    sort(c, c + n);
    ll ans = 0;
    for (int i = 0; i < n; i++) 
    { 
        ll x = lower_bound(a, a + n, b[i]) - a;

        ll y = n - (upper_bound(c, c + n, b[i]) - c);

        ans += y * x;
    }
    cout << ans << endl;
}

 螺旋折线

如图所示的螺旋折线经过平面上所有整点恰好一次。  
对于整点(X, Y),我们定义它到原点的距离dis(X, Y)是从原点到(X, Y)的螺旋折线段的长度。  
例如dis(0, 1)=3, dis(-2, -1)=9  
给出整点坐标(X, Y),你能计算出dis(X, Y)吗?

输入格式

X和Y,数据在int范围以内。

扫描二维码关注公众号,回复: 14174880 查看本文章

输出格式

输出dis(X, Y)  

输入样例

0 1

输出样例

3
#include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;

int main()
{
    long long X,Y;
    cin >> X >> Y;

    long long  n = max(abs(X), abs(Y));//获取正方形
    long long t ;  //判断(X,Y)与(-n,-n)的距离
    if(Y == -n)
    {  //底边
        t =   X-(-n);
    }else if(X==n )
    {//在右边
        t = 2 * n + Y - (-n);
    }else if(Y == n)
    {  //上边
        t = 4 * n + (n - X);
    }else{  //左边
        t = 6 * n + n - Y;
    }
    cout << 4 * n * (n + 1) - t;

    return 0;
}

日志统计

小明维护着一个程序员论坛。现在他收集了一份"点赞"日志,日志共有N行。
其中每一行的格式是:ts id。表示在ts时刻编号id的帖子收到一个"赞"。  
现在小明想统计有哪些帖子曾经是"热帖"。
如果一个帖子曾在任意一个长度为D的时间段内收到不少于K个赞,小明就认为这个帖子曾是"热帖"。  
具体来说,如果存在某个时刻T满足该帖在[T, T+D)这段时间内(注意是左闭右开区间)收到不少于K个赞,该帖就曾是"热帖"。  
给定日志,请你帮助小明统计出所有曾是"热帖"的帖子编号。  

输入格式

第一行包含三个整数N、D和K。  
以下N行每行一条日志,包含两个整数ts和id。 
1 <= K <= N <= 100000 0 <= ts <= 100000 0 <= id <= 100000  

输出格式

按从小到大的顺序输出热帖id。每个id一行。

输入样例 复制

7 10 2  
0 1  
0 10    
10 10  
10 1  
9 1
100 3  
100 3 

输出样例 复制

1
3
#include<iostream>
#include<string>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<string.h>
using namespace std;
int cpp[100005];
int bqq[100005];
struct tie
{
    int id=-1;
    int t=-1;
}arr[100005],a;
int cmp(tie x, tie y) { return x.t < y.t; }
int main()
{
    int n, d, k;
    scanf("%d%d%d", &n, &d, &k);
    for (int i = 0;i < n;i++)
    {
        scanf("%d%d", &arr[i].t, &arr[i].id);
    }
    sort(arr, arr + n,cmp);
    for (int i = 0, j = 0;i < n;i++) {
        bqq[arr[i].id]++;
        while (arr[i].t - arr[j].t >= d) {
            bqq[arr[j].id]--;
            j++;
        }
        if (bqq[arr[i].id] >= k)
            cpp[arr[i].id] = 1;
    }
    for (int i = 0;i < 100005;i++)
    {
        if (cpp[i] == 1)
        {
            cout << i << endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/aasd23/article/details/124946822