题目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. C# Chart使用总结 2 ----属性

    默认显示如图所示,Series的名称显示在右边,它会将下方空间挤掉,使图表只能显示在左侧,而右侧大部分地方都是空白的.当图很宽的时候看着会很不舒服.   可以设置Legends 集合中的Docking ...

  2. eclipse preference plugin development store and get

    eclipse plugin development: E:\workspaces\Eclipse_workspace_rcp\.metadata\.plugins\org.eclipse.pde.c ...

  3. Swift udp实现根据端口号监听广播数据(利用GCDAsyncUdpSocket实现)

    有个小需求,app需要监听pc广播的数据: 代码实现思路: 使用三方库:CocoaAsyncSocket 1.开启udp监听: udpSocket.beginReceiving() 2.读取udp的数 ...

  4. 文件批量上传-统一附件管理器-在线预览文件(有互联网和没有两种)--SNF快速开发平台3.0

    实际上在SNF里使用附件管理是非常简单的事情,一句代码就可以搞定.但我也要在这里记录一下统一附件管理器能满足的需求. 通用的附件管理,不要重复开发,调用尽量简洁. 批量文件上传,并对每个文件大小限制, ...

  5. Socket网络编程--聊天程序(9)

    这一节应该是聊天程序的最后一节了,现在回顾我们的聊天程序,看起来还有很多功能没有实现,但是不管怎么说,都还是不错的.这一节我们将讲多服务器问题(高大上的说法就是负载问题了.)至于聊天程序的文件发送(也 ...

  6. idea 导入 android项目

    1. 2. 主要是勾选上面选项. next next 导入即可

  7. ubuntu下使用golang、qml与ubuntu sdk开发桌面应用 (简单示例)

    找了很长时间go的gui库,试了gtk,准备试qt的时候发现了这个qml库,试了下很好用. ##准备工作 **1.Go 1.2RC1** go的版本应该不能低于这个,我是在1.2RC发布当天升级后发现 ...

  8. 网络协议TCP/IP、IPX/SPX、NETBEUI简介

    网络中不同的工作站,服务器之间能传输数据,源于协议的存在.随着网络的发展,不同的开发商开发了不同的通信方式.为了使通信成功可靠,网络中的所有主机都必须使用同一语言,不能带有方言.因而必须开发严格的标准 ...

  9. hibernate 查询全部数据的三种方法

    1.Query对象 使用Query对象需要写hql语句,使用hql语句操作的是实体类和属性. 用于查询全部的hql语句:from 实体类名称   例:String hql = "from U ...

  10. 完全卸载MySQL

    1.先停止mysql服务,cmd模式下输入net stop mysql; 或者在图形服务面板停止mysql服务 2.进入控制面板卸载mysql; 3.windows+R运行“regedit”文件,打开 ...