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. id 和 class的区别

    id 选择器 ID 只能被指定单个元素使用,无法多个元素使用.像你的身份证号,是唯一的,id 选择器以 “#” 来定义.id选择器的优先级高于class选择器的优先级的 # userid { text ...

  2. Linux远程上传文件

    #对拷文件夹 (包括文件夹本身) scp -r /home/slk root@192.168.1.5:/home # 对拷文件并重命名 scp /home/a.txt root@192.168.1.5 ...

  3. 10. Regular Expression Matching正则表达式匹配

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  4. Go 函数与闭包

    函数 1.函数与闭包 func adder() func (value int){ sum := 0 return func(value int) int{ sum += value return s ...

  5. awk&sed

    sed BRE       awk  ERE sed 不能采用?   awk可以 sed 在匹配的任何时候可以用^,$ awk必须除了在行头和行尾 其他地方必须转义

  6. leetcode303 Range Sum Query - Immutable

    """ Given an integer array nums, find the sum of the elements between indices i and j ...

  7. 吴裕雄--天生自然java开发常用类库学习笔记:Arrays

    import java.util.* ; public class ArraysDemo{ public static void main(String arg[]){ int temp[] = {3 ...

  8. B. Email from Polycarp

    B. Email from Polycarp time limit per test 3 seconds memory limit per test 256 megabytes input stand ...

  9. Golang的标识符命名规则

    Golang的标识符命名规则 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.关键字 1>.Go语言有25个关键字 Go语言的25个关键字如下所示: break,defau ...

  10. 小程序分享报错 Cannot read property 'apply' of null;at page XXX onShareAppMessage function

    Cannot read property 'apply' of null;at page XXX onShareAppMessage function 然后看了下自己的代码,分享按钮在子组件里, at ...