A 1148 Werewolf - Simple Version

  思路比较直接:模拟就行。因为需要序列号最小的两个狼人,所以以狼人为因变量进行模拟。

 #include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector> using namespace std;
int N;
vector<int> stateVec;
vector<int> inputVec;
bool judgeRight(int a, int b)
{
int liarCnt = , wereLiarCnt = , tmpNum;
fill(stateVec.begin(), stateVec.end(), );
stateVec[a] = stateVec[b] = ;
for(int i = ; i <= N; ++ i)
{
tmpNum = inputVec[i];
if((tmpNum > && stateVec[tmpNum] == ) || (tmpNum < && stateVec[-tmpNum] == ))
{
liarCnt ++;
if(i == a || i == b)
wereLiarCnt ++;
}
}
if(liarCnt == && wereLiarCnt == )
return true;
else
return false;
}
int main()
{
cin >> N;
inputVec.resize(N+, );
stateVec.resize(N+, );
for(int i = ; i <= N; ++ i)
cin >> inputVec[i];
for(int i = ; i < N; ++i)
{
for(int j = i+; j <= N; ++ j)
{
if(judgeRight(i,j))
{
cout << i << " " << j;
return ;
}
}
}
cout << "No Solution";
return ;
}

A 1149 Dangerous Goods Packaging

  这个最开始使用图+二重for循环直接判,发现超时很严重。后来改用邻接表存储,每个物品更新一下与其互斥物品的标志。成功通过。

 #include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
using namespace std;
int N, M;
typedef long long LL;
unordered_map<int, vector<int>> goodsMap;
vector<int> goodsVec;
bool judgeRight(void)
{
int tmpNum;
int len = goodsVec.size();
unordered_map<int, int> incomFlagMap;
for(int i = ; i < len; ++ i)
{
tmpNum = goodsVec[i];
if(incomFlagMap[tmpNum] == )
return false;
for(auto it = goodsMap[tmpNum].begin(); it != goodsMap[tmpNum].end(); ++it)
incomFlagMap[*it] = ;
}
return true;
}
int main()
{
cin >> N >> M;
int tmpNum2, tmpNum1;
while(N--)
{
cin >> tmpNum1 >> tmpNum2;
goodsMap[tmpNum1].push_back(tmpNum2);
goodsMap[tmpNum2].push_back(tmpNum1);
}
while(M--)
{
cin >> N;
goodsVec.resize(N,);
while(N--)
cin >> goodsVec[N];
if(judgeRight())
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return ;
}

A 1150 Travelling Salesman Problem

  这个理解题意就行。需要注意的地方:

              1.不能通过的路径,距离输出为NA

              2.最小距离是在TS cycle 和 TS simple cycle中选出

              3.TS cycle 需要最后一个城市为出发城市

 #include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int N, M, K;
const int INF = 0x7f7f7f7f;
int route[][]={};
int main()
{
cin >> N >> M;
int tmpSt, tmpEnd, tmpDis, tmpNum, lastCity, nowCity, minDis = INF, minIndex;
while(M--)
{
cin >> tmpSt >> tmpEnd >> tmpDis;
route[tmpSt][tmpEnd] = tmpDis;
route[tmpEnd][tmpSt] = tmpDis;
}
cin >> K;
for(int i = ; i <= K; ++i)
{
tmpDis = ;
cin >> tmpNum;
cin >> tmpSt;
lastCity = tmpSt;
vector<int> flagVec(N+,);
bool simpleFlag = true;
int travelCityCnt = ;
for(int j = ; j < tmpNum; j++)
{
cin >> nowCity;
if(flagVec[nowCity] == )
{
flagVec[nowCity] = ;
travelCityCnt ++;
}
else
simpleFlag = false;
if(tmpDis >= && route[lastCity][nowCity] > )
tmpDis += route[lastCity][nowCity];
else
tmpDis = -;
lastCity = nowCity;
}
if(lastCity == tmpSt && simpleFlag && tmpDis >= && travelCityCnt == N)
printf("Path %d: %d (TS simple cycle)\n", i, tmpDis);
else if(tmpDis >= && lastCity == tmpSt && travelCityCnt >= N)
printf("Path %d: %d (TS cycle)\n", i, tmpDis);
else
{
tmpDis >= ? printf("Path %d: %d (Not a TS cycle)\n", i, tmpDis) : printf("Path %d: NA (Not a TS cycle)\n", i);
tmpDis = -;
}
if(tmpDis > && tmpDis < minDis)
{
minDis = tmpDis;
minIndex = i;
}
}
printf("Shortest Dist(%d) = %d", minIndex, minDis);
return ;
}

A 1151 LCA in a Binary Tree

  只得到了22分,应该是2^10000太大了,所以这个方法行不通。更好的方法确实是和1143一样的思路去做。在输入时,保存一下中序遍历key和位置的关系。

 #include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
using namespace std;
int N, M, K;
vector<int> preOrderVec, inOrderVec;
unordered_map<int, int> valToIndexMap;
unordered_map<int, int> indexToValMap;
void insertVal(int inL, int inR, int preL, int preR, int index)
{
if(inL > inR || preL > preR)
return;
int tmpNum = preOrderVec[preL];
int tmpIndex = inL;
while(inOrderVec[tmpIndex] != tmpNum)
tmpIndex++;
//treeNodeVec[index] = tmpNum;
valToIndexMap[tmpNum] = index;
indexToValMap[index] = tmpNum;
insertVal(inL, tmpIndex-, preL+, preL+tmpIndex-inL, index*);
insertVal(tmpIndex+, inR, preR-inR+tmpIndex+,preR, index*+);
return;
}
void getNodeInfo(int a, int b)
{
int tmpIndex1 = valToIndexMap[a], tmpIndex2 = valToIndexMap[b];
if(tmpIndex1 == && tmpIndex2 == )
printf("ERROR: %d and %d are not found.\n", a, b);
else if(tmpIndex1 == || tmpIndex2 == )
tmpIndex1 == ? printf("ERROR: %d is not found.\n", a) : printf("ERROR: %d is not found.\n", b);
else
{
while(tmpIndex1 != tmpIndex2)
tmpIndex1 > tmpIndex2 ? tmpIndex1 /= : tmpIndex2 /= ;
int tmpNum = indexToValMap[tmpIndex1];
if(tmpNum == a || tmpNum == b)
tmpNum == a ? printf("%d is an ancestor of %d.\n", a, b) : printf("%d is an ancestor of %d.\n", b, a);
else
printf("LCA of %d and %d is %d.\n", a, b, tmpNum);
}
}
int main()
{
cin >> M >> N;
int tmpNum1, tmpNum2;
preOrderVec.resize(N);
inOrderVec.resize(N);
for(int i = ; i < N; ++i)
cin >> inOrderVec[i];
for(int i = ; i < N; ++ i)
cin >> preOrderVec[i];
insertVal(, N-, , N-, );
while(M--)
{
cin >> tmpNum1 >> tmpNum2;
getNodeInfo(tmpNum1, tmpNum2);
}
return ;
}

PAT 2018 秋的更多相关文章

  1. 美团Java工程师面试题(2018秋招)

    第一次面试 1.小数是怎么存的 2.算法题:N二进制有多少个1 3.Linux命令(不熟悉 4.JVM垃圾回收算法 5.C或者伪代码实现复制算法 6.volatile 7.树的先序中序后序以及应用场景 ...

  2. PAT 2019 秋

    考试的还行.不过略微有点遗憾,但是没想到第一题会直接上排序和dfs,感觉这次的题目难度好像是倒着的一样.哈哈哈哈. 第一题实在是搞崩心态,这道题给我的感觉是,可以做,但事实上总是差点啥. 第二题,第三 ...

  3. PAT 2018 春

    A 1140 Look-and-say Sequence 简单模拟.可能要注意字符串第一个字符和最后一个字符的处理. #include <cstdio> #include <iost ...

  4. 2018秋寒假作业6—PTA编程总结3

    1.实验代码 7-1 抓老鼠啊~亏了还是赚了? (20 分) 某地老鼠成灾,现悬赏抓老鼠,每抓到一只奖励10元,于是开始跟老鼠斗智斗勇:每天在墙角可选择以下三个操作:放置一个带有一块奶酪的捕鼠夹(T) ...

  5. 2018秋寒假作业5—PTA编程总结2

    1.实验代码: 7-1 币值转换 (20 分) 输入一个整数(位数不超过9位)代表一个人民币值(单位为元),请转换成财务要求的大写中文格式.如23108元,转换后变成"贰万叁仟壹百零捌&qu ...

  6. 2018秋寒假作业4—PTA编程总结1

    7-1 打印沙漏 (20 分) 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个"*",要求按下列格式打印 所谓"沙漏形状",是指每行输出奇数个符 ...

  7. 2018秋寒假作业6- -PTA编程总结3

    PTA3抓老鼠啊~亏了还是赚了?思路: 首先定义变量并初始化为零,然后用if-else语句判断其关系和计算奶酪数量及盈利情况.

  8. 2018秋寒假作业4- -PTA编程总结1

    PTA1打印沙漏.打印沙漏中的“沙漏形状”,就是每行输出的奇数符号与各行符号中心对齐:相邻两行符号数相差2:符号数从大到小递减到1,再从小到大递增.在做的时候出了几次错,编译发先是几个小地方出错了.以 ...

  9. 2018秋C语言程序设计(初级)作业- 第3次作业

    7-1 找出最小值 #include<stdio.h> int main() { int min,i,n,count; scanf("%d",&n); for( ...

随机推荐

  1. flutter如何使用配置文件pubspec.yaml(位于项目根目录)来管理第三方依赖包

    官方文档 在软件开发中,很多时候有一些公共的库或SDK可能会被很多项目用到,因此,将这些代码单独抽到一个独立模块,然后哪个项目需要使用时再直接集成这个模块,便可大大提高开发效率.很多编程语言或开发工具 ...

  2. Docker + Maven + Docker-compose

    前言: docker:容器化管理 maven:支持docker-maven的插件,通过 mvn clean -Dmaven.test.skip package dockerfile:build 打包命 ...

  3. 123.ModelForm的使用

    ModelForm 在我们的实例中,需要通过models.py中定义相关的模型字段,之后在forms.py中同样需要定义每个字段进行相应的验证,这样的话,我们会需要重复定义,这样的话,就相对比较麻烦, ...

  4. PHPExcel方法总结

    下面是总结的几个使用方法include 'PHPExcel.php';include 'PHPExcel/Writer/Excel2007.php';//或者include 'PHPExcel/Wri ...

  5. win上java1.7和1.8版本修改环境变量无效.md

    网上找了很多办法都没用. 解决办法: 看看自己 "系统环境变量" 中是不是有 "C:\ProgramData\Oracle\Java\javapath" 这项配 ...

  6. ACM-最优配餐

    题目描述: 最优配餐  时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 栋栋最近开了一家餐饮连锁店,提供外卖服务.随着连锁店越来越多,怎么合理的给客户送餐成为了一个急需解决的问 ...

  7. uni-app开发小程序-使用uni.switchTab跳转后页面不刷新的问题

    uni.switchTab({ url: '/pages/discover/discover', success: function(e) { var page = getCurrentPages() ...

  8. 滑条滚动发请求要用Debounce

    import debounce from 'lodash.debounce'; this.deboucedFunc = debounce(this.viewModel.v_onHomeworkRequ ...

  9. 【LeetCode】合并两个有序数组

    [问题] 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明:初始化 nums1 和 nums2 的元素数量分别为 m ...

  10. 洛谷 三月月赛 B

    搞出每一位与前一位的差,然后区间修改只是会影响区间的端点,所以只修改一下端点的值就好. %%%高一神犇线段树 #include<bits/stdc++.h> #define N 10000 ...