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

相关文章

C++ | Leetcode C++题解之第91题解码方法

题目: 题解: class Solution {public:int numDecodings(string s) {int n = s.size();// a = f[i-2], b = f[i-1], c = f[i]int a = 0, b = 1, c;for (int i = 1; i <= n; ++i) {c = 0;if (s[i - 1] != '0') {c += b

Golang | Leetcode Golang题解之第92题反转链表II

题目: 题解: func reverseBetween(head *ListNode, left, right int) *ListNode {// 设置 dummyNode 是这一类问题的一般做法dummyNode := &ListNode{Val: -1}dummyNode.Next = headpre := dummyNodefor i := 0; i < left-1; i++ {

C语言 | Leetcode C语言题解之第92题反转链表II

题目: 题解: struct ListNode *reverseBetween(struct ListNode *head, int left, int right) {// 因为头节点有可能发生变化,使用虚拟头节点可以避免复杂的分类讨论struct ListNode *dummyNode = malloc(sizeof(struct ListNode));dummyNode->val =

Golang | Leetcode Golang题解之第91题解码方法

题目: 题解: func numDecodings(s string) int {n := len(s)// a = f[i-2], b = f[i-1], c = f[i]a, b, c := 0, 1, 0for i := 1; i <= n; i++ {c = 0if s[i-1] != '0' {c += b}if i > 1 && s[i-2] != '0' && ((s[i-2

青少年CTF练习平台Crypto题解

四重加密 下载附件后,得到一个rar文件,发现被加密,无法解压 使用Bandizip打开 注释中有段编码OFZW4Y3UMY====== CyberChef base64解码得到第一层压缩包密码 qsnctf 打开后有一个文本文档 内容如下 &#122;&#99;&#121;&#101;&#123;&#109;&#120;&#109;&#101;&#109;&#1

math.round()理解

先简单理解成四舍五入      math.round(4.5) = 5;      math.round(5.3) = 5;      math.round(0.1) = 0; 这很好理解,对吧,小学生都很容易掌握的四舍五入。但是当round()中的值为负数的时候就容易犯错了   先看 math.round(-10.6) = -11 math.round(-10.5) = -10

第 397 场 LeetCode 周赛题解

A 两个字符串的排列差 模拟:遍历 s s s 记录各字符出现的位置,然后遍历 t t t 计算排列差 class Solution {public:int findPermutationDifference(string s, string t) {int n = s.size();vector<int> loc(26);for (int i = 0; i < n; i++)l

Math.Round()函数说明

Math.Round()并不是严格意义上的是四舍五入函数。它默认的执行的是“银行家舍入”算法,即四舍六入五取偶。概括为:四舍六入五考虑、五后非零就进一,五后皆零看奇偶,五前为偶应舍去、五前为奇要进一。         当为5时,取离着最近的偶数。见下图:                 测试代码如下: using System; using System.Collections.Gener

2024第十五届蓝桥杯C++大学A组压轴题解:封印宝石

题目:第十五届蓝桥杯C++大学A组省赛压轴题 题目传送门 题意:将n个数放在n个位置上,每个数只能放在它自己之前的位置上,且离自己多远就花费多少代价,可以有没放的数,给出最大代价要求最后放的数排成的字典序最大。 字典序最大带来的就必定是贪心,必须每次都把能放最大的一个值放到前面,同时为了节省体力,需要选相同的这个最大值最前面的一个。 对于当前位置i,也就是求i到i+k(当前体力)最大且最靠

Java Math floor round ceil 函数

有道Java相关的题,给大家分享一下: 1. System.out.println(Math.floor(-2.1)); 词句打印会是什么结果? [java]  view plain copy class Test {       public static void main(String[] args) {           System.out.println