Codeforces Round #502 (Div. 1 + Div. 2)

Contests 链接:Codeforces Round #502 (Div. 1 + Div. 2)
过题数:4
排名:765/7399

A. The Rank

题意

一个班上有 n n 个学生,每个学生都有一个 id 和四门课的成绩,每个学生的 id i d 1 1 n 之间的整数,每个学生的 id i d 都不相同,将所有学生按总分从高到低排序,若总分相同则按 id i d 从小到大排序,问 id i d 1 1 的学生最终排在第几名。

输入

第一行为一个整数 n (1n1000),接下去 n n 行每行 4 个整数 ai,bi,ci,di (0ai,bi,ci,di100) a i , b i , c i , d i   ( 0 ≤ a i , b i , c i , d i ≤ 100 ) ,第 i i 行的 4 个整数表示 id i d 号为 i i 的学生四门课的成绩。

输出

输出最终 id 1 1 的学生的排名。

样例

输入
5
100 98 100 100
100 100 100 100
100 100 99 99
90 99 90 100
100 98 60 99
输出
2
提示
5 个学生的总分分别为 398,400,398,379,357 398 , 400 , 398 , 379 , 357 id i d 1 1 的学生的分数和 id 3 3 的分数相等,但是他的 id 值更小,所以他排在第 2 2 名。
输入
6
100 80 90 99
60 60 60 60
90 60 100 60
60 100 60 80
100 100 0 100
0 0 0 0
输出
1
提示
6 个学生的总分分别为 369,240,310,300,300,0 369 , 240 , 310 , 300 , 300 , 0 id i d 1 1 的学生分数最高,所以他是第 1 名。

题解

按照总分为主关键字, id i d 为次关键字排序,然后 O(n) O ( n ) 扫一遍找到 id i d 1 1 的学生的排名。

过题代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <functional>
#include <iomanip>
using namespace std;

#define LL long long
const int maxn = 1000 + 100;
struct Node {
    int sum, id;
};

bool operator<(const Node &a, const Node &b) {
    return a.sum == b.sum? a.id < b.id: a.sum > b.sum;
}

int n, num;
Node node[maxn];

int main() {
    #ifdef LOCAL
    freopen("test.txt", "r", stdin);
//    freopen("test1.out", "w", stdout);
    #endif // LOCAL
    ios::sync_with_stdio(false);

    while(scanf("%d", &n) != EOF) {
        for(int i = 1; i <= n; ++i) {
            node[i].sum = 0;
            node[i].id = i;
            for(int j = 0; j < 4; ++j) {
                scanf("%d", &num);
                node[i].sum += num;
            }
        }
        sort(node + 1, node + 1 + n);
        int ans = 0;
        for(int i = 1; i <= n; ++i) {
            if(node[i].id == 1) {
                ans = i;
                break;
            }
        }
        printf("%d\n", ans);
    }

    return 0;
}

B. The Bits

题意

给定由 n 01 01 位组成的二进制数 a a b,问对 a a 的所有 01 位有多少种交换不同位的方式,使得 a or b a   o r   b (按位或)的值发生改变。

输入

第一行为一个整数 n (2n105) n   ( 2 ≤ n ≤ 10 5 ) ,第 2 2 行为 n 01 01 字符,表示 a a 的所有二进制位,第 3 行为 n n 01 字符,表示 b b 的二进制位。

输出

输出方案数。

样例

输入
5
01011
11001
输出
4
提示
a 中可以交换的 01 01 位为: (1,4),(2,3),(3,4),(3,5) ( 1 , 4 ) , ( 2 , 3 ) , ( 3 , 4 ) , ( 3 , 5 )

输入
6
011000
010011
输出
6
提示
a a 中可以交换的 01 位为: (1,2),(1,3),(2,4),(3,4),(3,5),(3,6) ( 1 , 2 ) , ( 1 , 3 ) , ( 2 , 4 ) , ( 3 , 4 ) , ( 3 , 5 ) , ( 3 , 6 )
题解

枚举所有 a a b 01 01 位的对应情况:

  1. a a 0 的位对应的是 b b 0 的位,与另一个 a a 1 的位交换,当前位置上的或值将会从 0 0 变为 1
  2. a a 0 的位对应的是 b b 1 的位,与另一个 a a 1 b b 0 的位交换,那么被交换的位置上的或值将会从 1 1 变为 0
  3. a a 1 的位对应的是 b b 0 的位,与另一 a a 0 的位交换,当前位置上的或值将会从 1 1 变为 0
  4. a a 1 的位对应的是 b b 1 的位,与另一个 a a 0 b b 0 的位交换,被交换的位置上的或值将会从 0 0 变为 1

因此按照所有对应位置上 ab a b 01 01 状态求前缀和或者后缀和,最后 O(n) O ( n ) 扫一遍就能得到答案。

过题代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <functional>
#include <iomanip>
using namespace std;

#define LL long long
const int maxn = 100000 + 100;
int n;
int sum[maxn][4];
char a[maxn], b[maxn];

int id(int i) {
    return (a[i] - '0') * 2 +  b[i] - '0';
}

int main() {
    #ifdef LOCAL
    freopen("test.txt", "r", stdin);
//    freopen("test1.out", "w", stdout);
    #endif // LOCAL
    ios::sync_with_stdio(false);

    while(scanf("%d", &n) != EOF) {
        memset(sum, 0, sizeof(sum));
        scanf("%s%s", a + 1, b + 1);
        for(int i = n; i > 0; --i) {
            for(int j = 0; j < 4; ++j) {
                sum[i][j] = sum[i + 1][j];
            }
            ++sum[i][id(i)];
        }
        LL ans = 0;
        for(int i = 1; i < n; ++i) {
            switch(id(i)) {
                case 0: ans += sum[i + 1][2] + sum[i + 1][3]; break;
                case 1: ans += sum[i + 1][2]; break;
                case 2: ans += sum[i + 1][0] + sum[i + 1][1]; break;
                case 3: ans += sum[i + 1][0]; break;
                default: break;
            }
        }
        printf("%I64d\n", ans);
    }

    return 0;
}

C. The Phone Number

题意

构造一个 1 1 n 的排列,使得这个排列中最长上升子序列的长度加上最长下降子序列的长度最小。

输入

输入为一个整数 n (1105) n   ( 1 ≤ 10 5 )

输出

输出 n n 个整数,为 1 n n 的排列,如果有多解输出任意一组。

样例

输入
4
输出
3 4 1 2
提示
最长上升子序列的长度为 2,最长下降子序列的长度为 2 2 ,总和为 4,是所有排列中最小的,但不是唯一的答案。

输入
2
输出
2 1
提示
最长上升子序列的长度为 1 1 ,最长下降子序列的长度为 2,总和为 3 3 ,是所有排列中最小的,但不是唯一的答案,序列 1 2 也是合法的答案。
题解

n n 分成 m 块3,其中每一块中的数字是递增的,各个块之间是递减的,则最长上升子序列的长度和最长下降子序列的长度和最小,为 m+nm m + ⌈ n m ⌉ ,由基本不等式可知,当 m m n m+nm m + ⌈ n m ⌉ 的值最小。

过题代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <functional>
#include <iomanip>
using namespace std;

#define LL long long
const int maxn = 100000 + 100;
int n;
int num[maxn];

int main() {
    #ifdef LOCAL
    freopen("test.txt", "r", stdin);
//    freopen("test1.out", "w", stdout);
    #endif // LOCAL
    ios::sync_with_stdio(false);

    while(scanf("%d", &n) != EOF) {
        int Index = sqrt(n);
        int tmp = 1;
        int Min = n;
        for(int i = n - Index; i >= 0; i -= Index) {
            Min = i;
            for(int j = 0; j < Index; ++j) {
                num[i + j] = tmp++;
            }
        }
        for(int i = 0; i < Min; ++i) {
            num[i] = tmp++;
        }
        for(int i = 0; i < n; ++i) {
            if(i != 0) {
                printf(" ");
            }
            printf("%d", num[i]);
        }
        printf("\n");
    }

    return 0;
}

D. The Wu

题意

有一个元素个数为 m m 01 串集合,集合内的 01 01 串可以重复出现,且每一个串的长度都为 n n ,对于两个 01 s s t,它们产生的价值为 vs,t=ni=1(si==ti?wi:0) v s , t = ∑ i = 1 n ( s i == t i ? w i : 0 ) ,现在有 q q 次询问,每次询问由一个长度为 n 01 01 s s 和一个数 k 组成,询问集合中满足条件 vs,tk v s , t ≤ k 的字符串个数。

输入

第一行为 3 3 个整数 n,m,q (1n12,1q,m5×105)。第二行为 n n 个整数 w1,w2,,wn (0wi100),接下去 m m 行每行一个长度为 n 01 01 字符串,接下去 q q 行每行由一个长度为 n 01 01 字符串和 k (0k100) k   ( 0 ≤ k ≤ 100 ) 组成。

输出

对于每次询问,输出答案。

样例

输入
2 4 5
40 20
01
01
10
11
00 20
00 40
11 20
11 40
11 60
输出
2
4
2
3
4
输入
1 2 4
100
0
1
0 0
0 100
1 0
1 100
输出
1
2
1
2
题解

先将字符串转化为对应数字( 0 0 2n1),对于集合中的所有数字 t t ,统计每个数字出现的次数 cntt,接着对所有可能的询问 s s 0 2n1 2 n − 1 )都预处理出对应的价值 vs,t v s , t ,将价值为 vs,t v s , t 的集合中的数字 t t 的个数记录在 sum[s][vs,t] 中,即 sum[s][vs,t]=cntt s u m [ s ] [ v s , t ] = c n t t ,由于询问中的 k k 最大值为 100,所以 sum s u m 数组的第 2 2 维只需要开到 100,大于 100 100 的可以不用处理,最后对 sum s u m 的每一次询问 s s 都做一遍前缀和,就能得到询问为 s 时,价值小于等于 k k 的所有满足条件的 t 的个数。预处理和查询的时间复杂度为 O(m+2n×2nn+100×2n+qn) O ( m + 2 n × 2 n n + 100 × 2 n + q n )

过题代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <functional>
#include <iomanip>
using namespace std;

#define LL long long
int n, m, k, q;
char str[20];
int num[20], cnt[4096];
int sum[4096 + 100][100 + 20];

int main() {
    #ifdef LOCAL
    freopen("test.txt", "r", stdin);
//    freopen("test1.out", "w", stdout);
    #endif // LOCAL
    ios::sync_with_stdio(false);

    while(scanf("%d%d%d", &n, &m, &q) != EOF) {
        memset(cnt, 0, sizeof(cnt));
        memset(sum, 0, sizeof(sum));
        for(int i = 0; i < n; ++i) {
            scanf("%d", &num[i]);
        }
        for(int i = 0; i < m; ++i) {
            scanf("%s", str);
            int tmp = 0;
            for(int j = 0; j < n; ++j) {
                tmp |= (str[j] - '0') << j;
            }
            ++cnt[tmp];
        }
        for(int i = 0; i < (1 << n); ++i) {
            for(int j = 0; j < (1 << n); ++j) {
                int tmp = 0;
                for(int k = 0; k < n; ++k) {
                    if(((i >> k) & 1) == ((j >> k) & 1)) {
                        tmp += num[k];
                    }
                }
                if(tmp > 100) {
                    continue;
                }
                sum[i][tmp] += cnt[j];
            }
        }
        for(int i = 0; i < (1 << n); ++i) {
            for(int j = 1; j <= 100; ++j) {
                sum[i][j] += sum[i][j - 1];
            }
        }
        for(int i = 0; i < q; ++i) {
            scanf("%s%d", str, &k);
            int tmp = 0;
            for(int j = 0; j < n; ++j) {
                tmp |= (str[j] - '0') << j;
            }
            printf("%d\n", sum[tmp][k]);
        }
    }

    return 0;
}

E. The Supersonic Rocket

题意

有两个点集,点的个数分别为 n n m,问两个点集分别构成的凸包是否可以通过旋转、平移使得两个凸包完全重合。

输入

第一行包含两个整数 n,m (3n,m105) n , m   ( 3 ≤ n , m ≤ 10 5 ) ,接下去 n n 行每行两个整数 xi,yi,表示第一个点集中每个点的坐标,最后 m m 行每行两个整数 xi,yi,表示第二个点集中每个点的坐标,其中 0xi,yi108 0 ≤ x i , y i ≤ 10 8

输出

如果可以通过旋转与平移使两个凸包完全重合,输出 YES Y E S ,否则输出 NO N O ,大小写任意。

样例

输入
3 4
0 0
0 2
2 0
0 2
2 2
2 0
1 1
输出
YES
提示
点集在平面直角坐标系上的位置为:



将第一个点集旋转 180° 180 ° 后:



再将第二个点集平移 (2,2) ( − 2 , − 2 ) 后:

输入
3 4
0 0
0 2
2 0
0 2
2 2
2 0
0 0
输出
NO
提示
不论如何平移旋转,两个凸包都不会重合。
题解

先分别跑出两个点集的凸包,判定两个凸包可以旋转平移重合的条件是:每条对应边的长度相等且对应角的大小相等,对应边直接求边长平方,对应角可以用叉积。将其中一个点集在凸包上所有点都复制到最后,让另一个凸包的所有点在这上面跑一遍 kmp k m p kmp k m p 能够往后跑的条件就是对应边与对应角都相等。

过题代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <functional>
#include <iomanip>
using namespace std;

#define LL long long
const int maxn = 100000 + 100;
struct Point {
    LL x, y;
    Point() {}
    Point(LL xx, LL yy) {
        x = xx;
        y = yy;
    }
};

LL operator*(const Point &a, const Point &b) {
    return a.x * b.x + a.y * b.y;
}

bool operator<(const Point &a, const Point &b) {
    return a.x == b.x? a.y < b.y: a.x < b.x;
}

LL operator^(const Point &a, const Point &b) {
    return a.x * b.y - a.y * b.x;
}

Point operator-(const Point &a, const Point &b) {
    return Point(a.x - b.x, a.y - b.y);
}

struct Cross_len {
    LL cross, len;
};

bool operator==(const Cross_len &a, const Cross_len &b) {
    return a.cross == b.cross && a.len == b.len;
}

bool operator!=(const Cross_len &a, const Cross_len &b) {
    return !(a == b);
}

int n[2], top[2];
Point point[2][maxn], sta[2][maxn << 1];
int Next[maxn];
Cross_len cross_len[2][maxn << 1];

void ConvexHull(Point *p, int n, int &top, Point *sta) {
    top = 0;
    sort(p + 1, p + 1 + n);
    for(int i = 1; i <= n; ++i) {
        while(top > 1 && ((sta[top - 1] - sta[top - 2]) ^ (p[i] - sta[top - 2])) <= 0) {
            --top;
        }
        sta[top++] = p[i];
    }
    int ttop = top;
    for(int i = n; i > 0; --i) {
        while(top > ttop && ((sta[top - 1] - sta[top - 2]) ^ (p[i] - sta[top - 2])) <= 0) {
            --top;
        }
        sta[top++] = p[i];
    }
    --top;
}

void Change_to_Cross_len(Point *p, int n, Cross_len *c) {
    Point p0 = p[0];
    for(int i = 0; i < n - 1; ++i) {
        p[i] = p[i + 1] - p[i];
        c[i].len = p[i] * p[i];
    }
    p[n - 1] = p0 - p[n - 1];
    c[n - 1].len = p[n - 1] * p[n - 1];
    for(int i = 0; i < n; ++i) {
        c[i].cross = p[i] ^ p[(i + 1) % n];
    }
}

void get_next(Cross_len *str, int n) {
    int j = -1;
    Next[0] = -1;
    for(int i = 1; i < n; ++i) {
        while(j != -1 && str[j + 1] != str[i]) {
            j = Next[j];
        }
        if(str[i] == str[j + 1]) {
            ++j;
        }
        Next[i] = j;
    }
}

bool kmp(Cross_len *str1, int n, Cross_len *str2, int m) {
    int j = -1;
    for(int i = 0; i < n; ++i) {
        while(j != -1 && str1[i] != str2[j + 1]) {
            j = Next[j];
        }
        if(str1[i] == str2[j + 1]) {
            ++j;
        }
        if(j == m - 1) {
            return true;
        }
    }
    return false;
}

int main() {
    #ifdef LOCAL
    freopen("test.txt", "r", stdin);
//    freopen("test1.out", "w", stdout);
    #endif // LOCAL
    ios::sync_with_stdio(false);

    while(scanf("%d%d", &n[0], &n[1]) != EOF) {
        memset(point, 0, sizeof(point));
        for(int i = 0; i < 2; ++i) {
            for(int j = 1; j <= n[i]; ++j) {
                scanf("%I64d%I64d", &point[i][j].x, &point[i][j].y);
            }
            ConvexHull(point[i], n[i], top[i], sta[i]);
            if(i == 0) {
                for(int j = 0; j < top[i]; ++j) {
                    sta[i][j + top[i]] = sta[i][j];
                }
                top[i] *= 2;
            }
            Change_to_Cross_len(sta[i], top[i], cross_len[i]);
        }
        get_next(cross_len[1], top[1]);
        if(kmp(cross_len[0], top[0], cross_len[1], top[1])) {
            printf("Yes\n");
        } else {
            printf("No\n");
        }
    }

    return 0;
}

F. The Neutral Zone

题意

注意本题内存限制为 16MB 16 M B
定义函数:

f(x)=Ax3+Bx2+Cx+Dexlogf(1)=0exlogf(pa11pa22pakk)=a1f(p1)+a2f(p2)+akf(pk) f ( x ) = A x 3 + B x 2 + C x + D e x l o g f ( 1 ) = 0 e x l o g f ( p 1 a 1 p 2 a 2 ⋯ p k a k ) = a 1 f ( p 1 ) + a 2 f ( p 2 ) + ⋯ a k f ( p k )
n=pa11pa22pakk n = p 1 a 1 p 2 a 2 ⋯ p k a k ,其中的每个 p p n 的质因数, a a 值是质因数对应的指数。
给定整数 n 和参数 A,B,C,D A , B , C , D 的值,计算以下公式:
i=1nexlogf(i) ∑ i = 1 n e x l o g f ( i )

输入

输入包含 4 4 个整数 n,A,B,C,D (1n3×108,0A,B,C,D106)

输出

将计算结果对 232 2 32 取模后输出。

样例

输入
12 0 0 1 0
输出
63
输入
4 1 2 3 4
输出
136
题解

1 1 n 内每个质数 p p 对答案的贡献为 np+np2+npk,其中 k k 为满足 pkn 的最大值,因此最后的答案就是:

i=1nf(pi)(npi+np2i+npki) ∑ i = 1 n f ( p i ) ( ⌊ n p i ⌋ + ⌊ n p i 2 ⌋ + ⋯ ⌊ n p i k ⌋ )
232 2 32 取模,就是用 unsigned int 进行计算并让它们自然溢出。
最后是内存限制,如果我们用一个线性筛,就需要存每个数字是否是质数,如果用 bool 数组, 3×108 3 × 10 8 bool 数组需要 286MB 286 M B ,用 bitset 压位需要 286835.75MB 286 8 ≈ 35.75 M B ,如果用埃氏筛,就可以在筛的过程中跳过 2 2 3 的倍数,我们将删去 2 2 3 的倍数后连续的数字用对应的 1,2,...,n 1 , 2 , . . . , n 表示:

5 5 7 11 11 13 13 17 17 19 19 23 23 25 25 29 29 31 31 35 35
1 1 2 3 3 4 5 5 6 7 7 8 9 9 10 11 11

可以发现数字与下标之间是 Index=n3 I n d e x = ⌊ n 3 ⌋ 的关系,因此如果跳过 2 2 3 的倍数,可以减少 23 2 3 的空间,最后只需要 35.75311.9MB 35.75 3 ≈ 11.9 M B

过题代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <functional>
#include <iomanip>
using namespace std;

#define LL long long
unsigned n, a, b, c, d;
unsigned ans;
bitset<100000001> bit;

unsigned f(unsigned x) {
    return a * x * x * x + b * x * x + c * x + d;
}

unsigned Get(unsigned x) {
    unsigned ret = f(x);
    unsigned tmp = 0;
    for(unsigned i = 1; i <= n / x; i *= x) {
        tmp += n / (i * x);
    }
    return ret * tmp;
}

int main() {
    #ifdef LOCAL
    freopen("test.txt", "r", stdin);
//    freopen("test1.out", "w", stdout);
    #endif // LOCAL
    ios::sync_with_stdio(false);

    while(cin >> n >> a >> b >> c >> d) {
        ans = Get(2) + Get(3);
        bit.reset();
        for(unsigned i = 5; i <= n; ++i) {
            if(i % 2 == 0 || i % 3 == 0) {
                continue;
            }
            if(bit[i / 3] == 0) {
                ans += Get(i);
                for(unsigned j = i; j <= n / i; ++j) {
                    if(i * j % 2 == 0 || i * j % 3 == 0) {
                        continue;
                    }
                    bit[i * j / 3] = 1;
                }
            }
        }
        cout << ans << endl;
    }

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值