题目1013 Battle Over Cities

思路:城市数也就1000, 对于每次询问暴力bfs一下看一下有多少连通块就行了。答案就是联通块数减一。

 #include<stdio.h>
#include<stdlib.h>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<queue> #define inf 0x7fffffff
using namespace std;
typedef long long LL;
typedef pair<int, int> pr; int n, m, k;
const int maxm = 1e6 + ;
const int maxn = ;
struct edge{
int u, v, nxt;
}e[maxm * ];
int head[maxn], tot = ; void addedge(int u, int v)
{
e[tot].u = u;
e[tot].v = v;
e[tot].nxt = head[u];
head[u] = tot++;
e[tot].u = v;
e[tot].v = u;
e[tot].nxt = head[v];
head[v] = tot++;
} int vis[maxn];
void bfs(int c, int city)
{
queue<int>que;
que.push(c);
vis[c] = true;
while(!que.empty()){
int now = que.front();que.pop();
for(int ed = head[now]; ed != -; ed = e[ed].nxt){
if(e[ed].v != city && !vis[e[ed].v]){
vis[e[ed].v] = true;
que.push(e[ed].v);
}
}
}
return ;
} int main()
{
scanf("%d%d%d", &n, &m, &k);
memset(head, -, sizeof(head));
for(int i = ; i < m; i++){
int u, v;
scanf("%d%d", &u, &v);
addedge(u, v);
}
for(int i = ; i < k; i++){
int city;
scanf("%d", &city);
memset(vis, , sizeof(vis));
int cnt = ;
for(int i = ; i <= n; i++){
if(!vis[i] && i != city){
bfs(i, city);
cnt++;
}
}
printf("%d\n", cnt - );
}
return ;
}

题目1014 Waiting in Line

思路:大模拟。我好菜系列。

用队列模拟每个窗口排队的人。没满的时候就是从左到右排就行了,满了之后就是哪个队伍先有人走哪个队伍就先进去。这里用优先队列模拟。

坑点是,如果在五点之前被服务了,服务时间超过五点也没关系,但是如果开始服务时间就已经超过五点了就要Sorry。

还有一个地方写错了是刚开始所有人都排进去就直接结束了,但是队列里其实还有人。

 #include<stdio.h>
#include<stdlib.h>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<queue> #define inf 0x7fffffff
using namespace std;
typedef long long LL;
typedef pair<int, int> pr; int n, m, k, q;
const int maxn = ;
int process[maxn], total[maxn];
queue<int>que[];
int t[]; struct node{
int time, line;
node(){
}
node(int t, int l)
{
time = t;
line = l;
}
bool operator < (const node& a)const{
if(time == a.time)return line > a.line;
return time > a.time;
}
}; int main()
{
scanf("%d%d%d%d", &n, &m, &k, &q);
for(int i = ; i <= k; i++){
scanf("%d", &process[i]);
} int id = ;
priority_queue<node>lineque;
for(int i = ; i <= m; i++){
for(int j = ; j <= n; j++){
que[j].push(id++);
if(id > k)break;
}
if(id > k)break;
} //que[1].push(id++); // for(int i = 1; i <= n; i++){
// lineque.push(node(0, i));
// }
int sum = k;
while(id <= k){
//int linetime = 10000, line;
for(int i = ; i <= n; i++){
if(!que[i].empty()){
int peo = que[i].front();que[i].pop();
sum--;
t[i] += process[peo];
total[peo] = t[i];
lineque.push(node(total[peo], i));
}
// if(linetime > t[i]){
// linetime = t[i];
// line = i;
// }
}
// que[line].push(id++); node l = lineque.top();lineque.pop();
que[l.line].push(id++);
} while(sum){
for(int i = ; i <= n; i++){
if(!que[i].empty()){
int peo = que[i].front();que[i].pop();
sum--;
t[i] += process[peo];
total[peo] = t[i];
}
}
} for(int i = ; i < q; i++){
int p;
scanf("%d", &p);
int h = total[p] / ;
int min = total[p] % ;
//printf("%d:%d\n",p,total[p]);
if(h + >= && min != && total[p] - process[p] >= ){
printf("Sorry\n");
}
else{
printf("%02d:%02d\n", h + , min);
}
}
return ;
}

题目1015 Reversible Primes

思路:感觉PAT的题意每次讲的都好不清楚啊【远没有ACM表述清晰严谨】。

题目的意思是一个数10进制下是质数,然后转换成D进制再逆序之后的字符串当成10进制也是质数。

 #include<stdio.h>
#include<stdlib.h>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue> #define inf 0x7fffffff
using namespace std;
typedef long long LL;
typedef pair<int, int> pr; int n, d;
const int maxn = 1e6 + ;
bool p[maxn]; void isprime()
{
p[] = true;
p[] = true;
for(int i = ; i <= maxn; i++){
if(p[i])continue;
int j = ;
while(j * i <= maxn){
p[j * i] = true;
j++;
}
}
} //stack<int>sss;
int getnum(int k)
{
int res = , r = ;
int tmp = n;
while(tmp){
res += r * (tmp % k);
//sss.push(tmp % k);
tmp /= k;
r *= k;
}
return res;
} int getreverse(int k)
{
int res = , tmp = n;
while(tmp){
res = res * k + tmp % k;
tmp /= k;
}
return res;
} int main()
{
isprime();
while(scanf("%d", &n) != EOF && n >= ){
scanf("%d", &d);
//cout<<getnum()<<endl<<getreverse()<<endl;
if(!p[getnum()] && !p[getreverse(d)]){
printf("Yes\n");
}
else{
printf("No\n");
}
} return ;
}

PAT甲级1013-1014-1015的更多相关文章

  1. PAT甲级1013. Battle Over Cities

    PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...

  2. 图论 - PAT甲级 1013 Battle Over Cities C++

    PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...

  3. pat甲级1013

    1013 Battle Over Cities (25)(25 分) It is vitally important to have all the cities connected by highw ...

  4. PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)

    1013 Battle Over Cities (25 分)   It is vitally important to have all the cities connected by highway ...

  5. PAT甲级1013题解——并查集+路径压缩

    题目分析: 本题初步浏览题目就知道是并查集的模板题,数据输入范围N为1~1000,则M的范围为0~1000^2,通过结构体记录每一对连线的关系,p[]数组记录每个节点的跟,对于k次查询,每次都要重新维 ...

  6. 【PAT甲级】1014 Waiting in Line (30 分)(队列维护)

    题面: 输入四个正整数N,M,K,Q(N<=20,M<=10,K,Q<=1000),N为银行窗口数量,M为黄线内最大人数,K为需要服务的人数,Q为查询次数.输入K个正整数,分别代表每 ...

  7. PAT甲级1014. Waiting in Line

    PAT甲级1014. Waiting in Line 题意: 假设银行有N个窗口可以开放服务.窗前有一条黄线,将等候区分为两部分.客户要排队的规则是: 每个窗口前面的黄线内的空间足以包含与M个客户的一 ...

  8. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  9. PAT甲级考前整理(2019年3月备考)之一

       转载请注明出处:https://www.cnblogs.com/jlyg/p/7525244.html 终于在考前,刷完PAT甲级131道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...

  10. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

随机推荐

  1. tmux手册中文翻译

    man tmux可以看到最详细的tmux介绍,本文翻译自tmux手册. tmux全名叫"terminal multiplexer",终端多路复用器. tmux的命令格式为: tmu ...

  2. iOS 自动移除KVO观察者

    对NSObject写一个分类: #import <Foundation/Foundation.h> @interface NSObject (FMObserverHelper) - (vo ...

  3. spring-mybatis代码生成插件,与实例展示

    前段时间看了张开涛写的代码生成插件,感觉思路很好,通过连接库然后获取数据库表信息,然后用户在界面中勾选要映射的策略,映射的字段,然后可以自动生成业务代码. 基于开涛的思路,自己写了一个简易插件,去掉了 ...

  4. 【转】Java异常总结和Spring事务处理异常机制浅析

    异常的概念和Java异常体系结构 异常是程序运行过程中出现的错误.本文主要讲授的是Java语言的异常处理.Java语言的异常处理框架,是Java语言健壮性的一个重要体现. Thorwable类所有异常 ...

  5. Effective Java 第三版——53. 明智而审慎地使用可变参数

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...

  6. Java多线程并发最佳实践

    使用本地变量 尽量使用本地变量,而不是创建一个类或实例的变量. 使用不可变类 String.Integer等.不可变类可以降低代码中需要的同步数量. 最小化锁的作用域范围:S=1/(1-a+a/n) ...

  7. android开发的童鞋们 你该学点C++

    更多关于C++的知识点,请关注android开发应该学点C++(索引贴)android开发应该学点C++(其他) (*android开发论坛----android开发学习----android开发*) ...

  8. Socket网络编程--网络爬虫(3)

    上一小节我们实现了从博客园的首页获取一些用户的用户名,并保存起来.接下来的这一小节我将对每个用户名构建一个用户的博客主页,然后从这个主页获取所有能获取到的网页,网页的格式现在是http://www.c ...

  9. FOR XML PATH 可以将查询结果根据行输出成XML格式

    SELECT CAST(OrderID AS varchar)+',' as OrderNo FROM Product CAST函数用于将某种数据类型的表达式显式转换为另一种数据类型 SELECT C ...

  10. FiDDLER教程

    FiDDLER教程 摘自:林猪猪的部落格 的 前端工具 1 FIDDLER的使用方法及技巧总结(连载一)FIDDLER快速入门及使用场景 2 FIDDLER的使用方法及技巧总结(连载二)FIDDLER ...