A
实际上我们for一遍就好。
坑点就是会爆int
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
long long ans1,ans2,a[maxn],b[maxn],c[maxn];
int n;
int main(){
while(cin>>n){
ans1 = -1;
for(int i=1;i<=n;i++){
cin>>a[i]>>b[i]>>c[i];
if(a[i]*b[i]*c[i]>ans1){
ans1 = a[i]*b[i]*c[i];
ans2 = i;
}
}
cout<<ans2<<endl;
}
}
B . 两数组找相同的元素
我们对于B的每一个元素,在A中进行二分判断是否存在即可。
也可以使用C++内带的.find函数
#include <cstdio>
#include <unordered_set>
#include <vector>
using namespace std;
int main(int argc, const char *argv[])
{
unordered_set<int> first_array;
int m, n;
scanf("%d %d", &m, &n);
int tmp;
for (int i = 0; i < m; i++) {
scanf("%d", &tmp);
first_array.emplace(tmp);
}
vector<int> ans;
for (int i = 0; i < n; i++) {
scanf("%d", &tmp);
if (first_array.find(tmp) != first_array.end()) {
ans.push_back(tmp);
}
}
for (int i = 0; i < ans.size(); ++i) {
printf("%d%c", ans[i], i + 1 == ans.size() ? '\n' : ' ');
}
return 0;
}
B . DAU统计
实际上我们对于每一个元素,如果这个元素之前并没有出现过,那么对答案的贡献就加一。
于是我们可以用hash每次O(1)的判断这个元素之前是否出现过即可。
也可以使用set去维护也可以。
// Copyright 2017 He Tianyi <hetianyi@bytedance.com>
// Compiler opts: g++ -g -std=c++11 -o dau -O2 -Wall -Werror dau.cc
#include <cstdint>
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <memory>
static const size_t kMergeThreshold = 170000;
class UserMap {
public:
UserMap() {
base_ = std::unique_ptr<std::vector<uint64_t>>(
new std::vector<uint64_t>());
}
void Merge() {
std::vector<uint64_t> *result = new std::vector<uint64_t>();
result->reserve(base_->size() + delta_.size());
auto base_it = base_->begin();
auto delta_it = delta_.begin();
while (base_it != base_->end() || delta_it != delta_.end()) {
if (delta_it == delta_.end()) {
result->emplace_back(*base_it);
base_it++;
} else if (base_it == base_->end()) {
result->emplace_back(*delta_it);
delta_it++;
} else {
if (*base_it < *delta_it) {
result->emplace_back(*base_it);
base_it++;
} else {
result->emplace_back(*delta_it);
delta_it++;
}
}
}
base_.reset(result);
delta_.clear();
}
bool PutIfAbsent(uint64_t uid) {
if (delta_.find(uid) != delta_.end()) {
return false;
}
if (std::binary_search(base_->begin(), base_->end(), uid)) {
return false;
}
delta_.insert(uid);
if (delta_.size() > kMergeThreshold) {
Merge();
}
return true;
}
private:
std::unique_ptr<std::vector<uint64_t>> base_;
std::set<uint64_t> delta_;
};
int main(int argc, char** argv) {
uint64_t uid;
UserMap m;
uint32_t result = 0;
do {
std::cin >> uid;
if (uid > 0 && m.PutIfAbsent(uid)) {
result++;
}
} while (uid != 0);
std::cout << result << std::endl;
return 0;
}
D . 形式化计算
题面分为两个部分,计算部分和输出部分。
计算部分比较简单,我们对于每个符号进行判断即可。
计算部分结束之后,我们要处理输出。
输出我们需要对每个数字/符号单独输出。可以观察到,每个字符实际上都是一个矩阵,所以我们可以预处理出每个字符矩阵的样子,然后输出即可。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int a,b;
char s[10];
char drawBoard[5][200];
void print1(int &startCol) {
drawBoard[0][startCol] = '*';
drawBoard[1][startCol] = '*';
drawBoard[2][startCol] = '*';
drawBoard[3][startCol] = '*';
drawBoard[4][startCol] = '*';
startCol = startCol + 3;
}
void print2(int &startCol) {
drawBoard[0][startCol] = '*';
drawBoard[0][startCol+1] = '*';
drawBoard[0][startCol+2] = '*';
drawBoard[1][startCol+2] = '*';
drawBoard[2][startCol+2] = '*';
drawBoard[2][startCol+1] = '*';
drawBoard[2][startCol] = '*';
drawBoard[3][startCol] = '*';
drawBoard[4][startCol] = '*';
drawBoard[4][startCol+1] = '*';
drawBoard[4][startCol+2] = '*';
startCol = startCol + 5;
}
void print3(int &startCol) {
drawBoard[0][startCol] = '*';
drawBoard[0][startCol+1] = '*';
drawBoard[0][startCol+2] = '*';
drawBoard[1][startCol+2] = '*';
drawBoard[2][startCol] = '*';
drawBoard[2][startCol+1] = '*';
drawBoard[2][startCol+2] = '*';
drawBoard[3][startCol+2] = '*';
drawBoard[4][startCol] = '*';
drawBoard[4][startCol+1] = '*';
drawBoard[4][startCol+2] = '*';
startCol = startCol + 5;
}
void print4(int &startCol) {
drawBoard[0][startCol] = '*';
drawBoard[0][startCol+2] = '*';
drawBoard[1][startCol] = '*';
drawBoard[1][startCol+2] = '*';
drawBoard[2][startCol] = '*';
drawBoard[2][startCol+1] = '*';
drawBoard[2][startCol+2] = '*';
drawBoard[3][startCol+2] = '*';
drawBoard[4][startCol+2] = '*';
startCol = startCol + 5;
}
void print5(int &startCol) {
drawBoard[0][startCol] = '*';
drawBoard[0][startCol+1] = '*';
drawBoard[0][startCol+2] = '*';
drawBoard[1][startCol] = '*';
drawBoard[2][startCol] = '*';
drawBoard[2][startCol+1] = '*';
drawBoard[2][startCol+2] = '*';
drawBoard[3][startCol+2] = '*';
drawBoard[4][startCol+2] = '*';
drawBoard[4][startCol+1] = '*';
drawBoard[4][startCol] = '*';
startCol = startCol + 5;
}
void print6(int &startCol) {
drawBoard[0][startCol] = '*';
drawBoard[0][startCol+1] = '*';
drawBoard[0][startCol+2] = '*';
drawBoard[1][startCol] = '*';
drawBoard[2][startCol] = '*';
drawBoard[2][startCol+1] = '*';
drawBoard[2][startCol+2] = '*';
drawBoard[3][startCol] = '*';
drawBoard[3][startCol+2] = '*';
drawBoard[4][startCol] = '*';
drawBoard[4][startCol+1] = '*';
drawBoard[4][startCol+2] = '*';
startCol = startCol + 5;
}
void print7(int &startCol) {
drawBoard[0][startCol] = '*';
drawBoard[0][startCol+1] = '*';
drawBoard[0][startCol+2] = '*';
drawBoard[1][startCol+2] = '*';
drawBoard[2][startCol+2] = '*';
drawBoard[3][startCol+2] = '*';
drawBoard[4][startCol+2] = '*';
startCol = startCol + 5;
}
void print8(int &startCol) {
drawBoard[0][startCol] = '*';
drawBoard[0][startCol+1] = '*';
drawBoard[0][startCol+2] = '*';
drawBoard[1][startCol] = '*';
drawBoard[1][startCol+2] = '*';
drawBoard[2][startCol] = '*';
drawBoard[2][startCol+1] = '*';
drawBoard[2][startCol+2] = '*';
drawBoard[3][startCol] = '*';
drawBoard[3][startCol+2] = '*';
drawBoard[4][startCol] = '*';
drawBoard[4][startCol+1] = '*';
drawBoard[4][startCol+2] = '*';
startCol = startCol + 5;
}
void print9(int &startCol) {
drawBoard[0][startCol] = '*';
drawBoard[0][startCol+1] = '*';
drawBoard[0][startCol+2] = '*';
drawBoard[1][startCol] = '*';
drawBoard[1][startCol+2] = '*';
drawBoard[2][startCol] = '*';
drawBoard[2][startCol+1] = '*';
drawBoard[2][startCol+2] = '*';
drawBoard[3][startCol+2] = '*';
drawBoard[4][startCol] = '*';
drawBoard[4][startCol+1] = '*';
drawBoard[4][startCol+2] = '*';
startCol = startCol + 5;
}
void print0(int &startCol) {
drawBoard[0][startCol] = '*';
drawBoard[0][startCol+1] = '*';
drawBoard[0][startCol+2] = '*';
drawBoard[1][startCol] = '*';
drawBoard[1][startCol+2] = '*';
drawBoard[2][startCol] = '*';
drawBoard[2][startCol+2] = '*';
drawBoard[3][startCol] = '*';
drawBoard[3][startCol+2] = '*';
drawBoard[4][startCol] = '*';
drawBoard[4][startCol+1] = '*';
drawBoard[4][startCol+2] = '*';
startCol = startCol + 5;
}
void printjia(int &startCol) {
drawBoard[1][startCol+1] = '*';
drawBoard[2][startCol] = '*';
drawBoard[2][startCol+1] = '*';
drawBoard[2][startCol+2] = '*';
drawBoard[3][startCol+1] = '*';
startCol = startCol + 5;
}
void printjian(int &startCol) {
drawBoard[2][startCol] = '*';
drawBoard[2][startCol+1] = '*';
drawBoard[2][startCol+2] = '*';
startCol = startCol + 5;
}
void printcheng(int &startCol) {
drawBoard[1][startCol] = '*';
drawBoard[1][startCol+2] = '*';
drawBoard[2][startCol+1] = '*';
drawBoard[3][startCol] = '*';
drawBoard[3][startCol+2] = '*';
startCol = startCol + 5;
}
void printchu(int &startCol) {
drawBoard[1][startCol+2] = '*';
drawBoard[2][startCol+1] = '*';
drawBoard[3][startCol] = '*';
startCol = startCol + 5;
}
void printequal(int &startCol) {
drawBoard[1][startCol] = '*';
drawBoard[1][startCol+1] = '*';
drawBoard[1][startCol+2] = '*';
drawBoard[1][startCol+3] = '*';
drawBoard[3][startCol] = '*';
drawBoard[3][startCol+1] = '*';
drawBoard[3][startCol+2] = '*';
drawBoard[3][startCol+3] = '*';
startCol = startCol + 6;
}
void printdian(int &startCol) {
drawBoard[3][startCol] = '*';
drawBoard[3][startCol+1] = '*';
drawBoard[4][startCol] = '*';
drawBoard[4][startCol+1] = '*';
startCol = startCol + 4;
}
void drawNum(double x,int &startCol) {
char s[100];
memset(s,0,sizeof(s));
if (x-int(x) == 0) sprintf(s,"%d",int(x));
else if (x*10-int(x*10)==0) sprintf(s,"%.1lf",x);
else sprintf(s,"%.2lf",x);
for (int i=0;i<strlen(s);++i) {
switch (s[i]) {
case '0':print0(startCol);break;
case '1':print1(startCol);break;
case '2':print2(startCol);break;
case '3':print3(startCol);break;
case '4':print4(startCol);break;
case '5':print5(startCol);break;
case '6':print6(startCol);break;
case '7':print7(startCol);break;
case '8':print8(startCol);break;
case '9':print9(startCol);break;
case '.':printdian(startCol);break;
case '-':printjian(startCol);break;
}
}
}
int main() {
scanf("%d %s %d",&a,s,&b);
for (int i=0;i<5;++i)
for (int j=0;j<200;++j)
drawBoard[i][j] = ' ';
double c=0;
int startCol=0;
drawNum(a,startCol);
switch (s[0]) {
case '+':c = a+b; printjia(startCol);break;
case '-':c = a-b; printjian(startCol);break;
case '*':c = a*b; printcheng(startCol);break;
case '/':c = 1.0*a/b; printchu(startCol);break;
}
drawNum(b,startCol);
printequal(startCol);
drawNum(c,startCol);
for (int i=0;i<5;++i,cout<<endl)
for (int j=0;j<startCol;++j)
cout<<drawBoard[i][j];
return 0;
}
E . 任务执行策略
动态规划题。
首先把三角形翻转一下,倒着来考虑每个元素。
dp[i][j][k]表示考虑到第(i,j)个,当前选取了k个元素的最大值。
前缀和维护最大值,就可以把转移优化到O(1)。
#include <algorithm>
#include <cassert>
#include <cstring>
#include <cstdio>
const int N = 60;
const int M = 500 + 10;
int dp[N][N][M], sum[N][N], a[N][N], n, m;
int main() {
assert(scanf("%d%d", &n, &m) == 2);
assert(1 <= n && n <= 50);
assert(1 <= m && m <= 500);
for (int i = 1; i <= n; ++ i) {
for (int j = 1; j <= i; ++ j) {
assert(scanf("%d", &a[i][j]) == 1);
assert(0 <= a[i][j] && a[i][j] <= 1000);
}
}
for (int i = 1; i <= n; ++ i) {
for (int j = 1; j <= i; ++ j) {
sum[i][j] = sum[i][j - 1] + a[n - j + 1][i - j + 1];
}
}
memset(dp, 200, sizeof(dp));
for (int i = 0; i <= n; ++ i) {
dp[i][0][0] = 0;
}
for (int i = 1; i <= n; ++ i) {
for (int j = i; j >= 0; -- j) {
for (int k = j; k <= m; ++ k) {
dp[i][j][k] = std::max(dp[i][j + 1][k],
dp[i - 1][std::max(0, j - 1)][k - j] + sum[i][j]);
}
}
}
printf("%d\n", dp[n][0][m]);
return 0;
}
喵哈哈村的魔法考试 Round 16 (Div.2) 比赛题解的更多相关文章
-
喵哈哈村的魔法考试 Round #7 (Div.2) 题解
喵哈哈村的魔法考试 Round #7 (Div.2) 注意!后四道题来自于周日的hihocoder offer收割赛第九场. 我建了个群:欢迎加入qscoj交流群,群号码:540667432 大概作为 ...
-
喵哈哈村的魔法考试 Round #2 (Div.2) 题解
喵哈哈村的魔法考试 Round #2 (Div.2) 题解 A.喵哈哈村的战争 题解: 这道题就是for一遍,统计每个村子的战斗力的和,然后统计哪个村子的战斗力和大一点就好了. 唯一的坑点,就是这道题 ...
-
喵哈哈村的魔法考试 Round #1 (Div.2) 题解
喵哈哈村的魔法考试 Round #1 (Div.2) 题解 特别感谢出题人,qscqesze. 也特别感谢测题人Xiper和CS_LYJ1997. 没有他们的付出,就不会有这场比赛. A 喵哈哈村的魔 ...
-
喵哈哈村的魔法考试 Round #1 (Div.2) 题解&;源码(A.水+暴力,B.dp+栈)
A.喵哈哈村的魔法石 发布时间: 2017年2月21日 20:05 最后更新: 2017年2月21日 20:06 时间限制: 1000ms 内存限制: 128M 描述 传说喵哈哈村有三种神 ...
-
喵哈哈村的魔法考试 Round #19 (Div.2) 题解
题解: 喵哈哈村的魔力源泉(1) 题解:签到题. 代码: #include<bits/stdc++.h> using namespace std; int main(){ long lon ...
-
喵哈哈村的魔法考试 Round #1 (Div.2)
比赛地址:http://qscoj.cn/contest/2/ 都是中文题,这里不在详述题意 A.喵哈哈村的魔法石 分析:暴力求解 #include<iostream> #include& ...
-
喵哈哈村的魔法考试 Round #1 (Div.2) ABCD
官方题解: http://www.cnblogs.com/qscqesze/p/6418555.html#3623453 喵哈哈村的魔法石 描述 传说喵哈哈村有三种神奇的魔法石:第一种魔法石叫做人铁石 ...
-
喵哈哈村的魔法考试 Round #4 (Div.2) 题解
有任何疑问,可以加我QQ:475517977进行讨论. A 喵哈哈村的嘟嘟熊魔法(1) 题解 这道题我们只要倒着来做就可以了,因为交换杯子是可逆的,我们倒着去模拟一遍就好了. 有个函数叫做swap(a ...
-
喵哈哈村的魔法考试 Round #20 (Div.2) 题解
题解: A 喵哈哈村的跳棋比赛 题解:其实我们要理解题意就好了,画画图看看这个题意.x<y,那么就交换:x>y,那么x=x%y. 如果我们经过很多次,或者y<=0了,那么就会无限循环 ...
随机推荐
-
考前预习(Ubuntu配备)
这几天考前预习,趁现在不想预习,写点之前就想写的东西吧. 贴一下个人认为有用的,在Ubuntu装机后的一些小事.不过挺杂的,主要是拿来给以后的自己看,以及让现在无聊的我有点事做. 首先,Ubuntu官 ...
-
不等高cell搭建(二)
一.commentView模块搭建 commentView样式分为两种 1.xib搭建界面 1.1 因为评论的样式大体上一样,我们可以用同一个xib来处理 1.2 最热评论 用 ...
-
设置JVM参数,查看堆大小
1.在eclipse设置JVM参数 打开eclipse-窗口-首选项-Java-已安装的JRE(对在当前开发环境中运行的java程序皆生效,也就是在eclipse中运行的java程序)编辑当前 ...
-
算法总结之欧拉函数&;中国剩余定理
算法总结之欧拉函数&中国剩余定理 1.欧拉函数 概念:在数论,对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目. 通式:φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)( ...
-
easyui常用控件及样式收藏
CSS类定义: div easyui-window window窗口样式 属性如下: 1) modal:是否生成模态窗口.tru ...
-
ViewPager 详解(二)---详解四大函数
前言:上篇中我们讲解了如何快速实现了一个滑动页面,但问题在于,PageAdapter必须要重写的四个函数,它们都各有什么意义,在上节的函数内部为什么要这么实现,下面我们就结合Android的API说明 ...
-
javascript判断键盘按键
window.document.onkeydown = disableRefresh; function disableRefresh(evt){ evt = (evt) ? evt : window ...
-
框架篇:Spring+SpringMVC+hibernate整合开发
前言: 最近闲的蛋疼,搭个框架写成博客记录下来,拉通一下之前所学知识,顺带装一下逼. 话不多说,我们直接步入正题. 准备工作: 1/ IntelliJIDEA的安装配置:jdk/tomcat等..(本 ...
-
关于warning: suggest parentheses around assignment used as truth value [-Wparentheses]|的解决方法
今天,在调试的时候一直出现warning: suggest parentheses around assignment used as truth value 代码如下: if(startTime== ...
-
IOT-SpringBoot-angular启动
1 D:\workspace_iot\iot-hub\src\main\angular cmd 启动 npm start 2 eclipse中启动springboot 3 local ...