MemSQL Start[c]UP 2.0 - Round 2 - Online Round 题解

2024-05-05 12:38

本文主要是介绍MemSQL Start[c]UP 2.0 - Round 2 - Online Round 题解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题解:

A. Golden System
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Piegirl got bored with binary, decimal and other integer based counting systems. Recently she discovered some interesting properties about number , in particular that q2 = q + 1, and she thinks it would make a good base for her new unique system. She called it "golden system". In golden system the number is a non-empty string containing 0's and 1's as digits. The decimal value of expression a0a1...an equals to .

Soon Piegirl found out that this system doesn't have same properties that integer base systems do and some operations can not be performed on it. She wasn't able to come up with a fast way of comparing two numbers. She is asking for your help.

Given two numbers written in golden system notation, determine which of them has larger decimal value.

Input

Input consists of two lines — one for each number. Each line contains non-empty string consisting of '0' and '1' characters. The length of each string does not exceed 100000.

Output

Print ">" if the first number is larger, "<" if it is smaller and "=" if they are equal.

Sample test(s)
input
1000
111
output
<
input
00100
11
output
=
input
110
101
output
>
传送门: 点击打开链接

解题思路;

这题的关键就是这个公式q2 = q + 1。通过这个式子,我们可以发现,对于q进制数,相邻的两个低位如果都是1,就可以向高位进1,如011 = 100。我们先读入两个数组,取一个值n为二者长度的最大值+1(为方便处理11000种情况),不足n位的,在高位用0补齐,然后就来处理数组了,从高位往低位扫一遍,将可以进位的地方进位。最后直接比较两个字符串的大小即可。

代码:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;const int MAXN = 1e5 + 10;
char sa[MAXN], sb[MAXN];char *fun(char s[])
{int n = strlen(s);for(int i = 0; i < n-1; ++i){int x = i;while(x>=0 && (s[x]-'0') && (s[x+1]-'0')){s[x]--, s[x+1]--, s[x-1]++, x-=2;}}return s;
}int main()
{scanf("%s", sa);scanf("%s", sb);int na = strlen(sa);int nb = strlen(sb);int n = max(na, nb) + 1;for(int i = na; i >= 0; --i)sa[i+n-na] = sa[i];for(int i = 0; i < n-na; ++i)sa[i] = '0';for(int i = nb; i >= 0; --i)sb[i+n-nb] = sb[i];for(int i = 0; i < n-nb; ++i)sb[i] = '0';strcpy(sa, fun(sa));strcpy(sb, fun(sb));//puts(sa), puts(sb);int cmp = strcmp(sa, sb);switch(cmp){case -1:{printf("<\n");break;}case 0:{printf("=\n");break;}case 1:{printf(">\n");break;}}return 0;
}


B. Distributed Join
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Piegirl was asked to implement two table join operation for distributed database system, minimizing the network traffic.

Suppose she wants to join two tables, A and B. Each of them has certain number of rows which are distributed on different number of partitions. Table A is distributed on the first cluster consisting of mpartitions. Partition with index i has ai rows from A. Similarly, second cluster containing table B has npartitions, i-th one having bi rows from B.

In one network operation she can copy one row from any partition to any other partition. At the end, for each row from A and each row from B there should be a partition that has both rows. Determine the minimal number of network operations to achieve this.

Input

First line contains two integer numbers, m and n (1 ≤ m, n ≤ 105). Second line contains description of the first cluster with m space separated integers, ai (1 ≤ ai ≤ 109). Similarly, third line describes second cluster with n space separated integers, bi (1 ≤ bi ≤ 109).

Output

Print one integer — minimal number of copy operations.

Sample test(s)
input
2 2
2 6
3 100
output
11
input
2 3
10 10
1 1 1
output
6
传送门: 点击打开链接

大致题意:

A有m个分支,每个分支有ai组,B有n个分支,每个分支有bi组,要将A与B合并(A的每个分支都包含B的全部,或B的每个分支都包含A的全部),使得总花费最小。

解题思路:

贪心。策略:将a数组升序排列,b数组升序排列,求出a的和ma,b的和mb。一共就两种情况,要么将A合并到B,要要么将B合并到A,取两者的小值即可。如果是将B合并到A中,我们就将a数组遍历一下,a[i]>=mb就保留a[i],将mb合并到a[i]中,花费为mb,否则,将a[i]合并到A的其他组中,花费为a[i],注意一点如果是a的最后一项必须保留,哪怕是比mb小,也要保留下来,将mb合并进来,因为是将B合并到A,A中应该至少保留一项。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;typedef long long lint;
const int MAXN = 1e5 + 10;
int n, m,  a[MAXN], b[MAXN];int main()
{scanf("%d%d", &m, &n);lint ma = 0, mb = 0, ansa = 0, ansb = 0;for(int i = 0; i < m; ++i){scanf("%d", &a[i]);ma += a[i];}for(int i = 0; i < n; ++i){scanf("%d", &b[i]);mb += b[i];}sort(a, a+m);sort(b, b+n);for(int i = 0; i < m; ++i){if(a[i]>=mb || m-1==i)ansa += mb;elseansa += a[i];}for(int i = 0; i < n; ++i){if(b[i]>=ma || n-1==i)ansb += ma;elseansb += b[i];}printf("%I64d\n", min(ansa, ansb));return 0;
}


这篇关于MemSQL Start[c]UP 2.0 - Round 2 - Online Round 题解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/961739

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

GNSS CTS GNSS Start and Location Flow of Android15

目录 1. 本文概述2.CTS 测试3.Gnss Flow3.1 Gnss Start Flow3.2 Gnss Location Output Flow 1. 本文概述 本来是为了做Android 14 Gnss CTS 的相关环境的搭建和测试,然后在测试中遇到了一些问题,去寻找CTS源码(/cts/tests/tests/location/src/android/locat

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

Oracle Start With关键字

Oracle Start With关键字 前言 旨在记录一些Oracle使用中遇到的各种各样的问题. 同时希望能帮到和我遇到同样问题的人. Start With (树查询) 问题描述: 在数据库中, 有一种比较常见得 设计模式, 层级结构 设计模式, 具体到 Oracle table中, 字段特点如下: ID, DSC, PID; 三个字段, 分别表示 当前标识的 ID(主键), DSC 当

C - Word Ladder题解

C - Word Ladder 题解 解题思路: 先输入两个字符串S 和t 然后在S和T中寻找有多少个字符不同的个数(也就是需要变换多少次) 开始替换时: tips: 字符串下标以0开始 我们定义两个变量a和b,用于记录当前遍历到的字符 首先是判断:如果这时a已经==b了,那么就跳过,不用管; 如果a大于b的话:那么我们就让s中的第i项替换成b,接着就直接输出S就行了。 这样

笔记整理—内核!启动!—kernel部分(2)从汇编阶段到start_kernel

kernel起始与ENTRY(stext),和uboot一样,都是从汇编阶段开始的,因为对于kernel而言,还没进行栈的维护,所以无法使用c语言。_HEAD定义了后面代码属于段名为.head .text的段。         内核起始部分代码被解压代码调用,前面关于uboot的文章中有提到过(eg:zImage)。uboot启动是无条件的,只要代码的位置对,上电就工作,kern