题目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. Python实现邮件的批量发送

    Python实现邮件的批量发送 1 发送文本信息 '''加密发送文本邮件''' def sendEmail(from_addr,password,to_addr,smtp_server): try: ...

  2. unix环境高级编程-3.10-文件共享(转)

    unix系统支持在不同进程间共享打开的文件. 内核使用三种数据结果表示打开的文件. (1)每个进程在进程表中都有一个记录项,记录项中包含有一张打开文件的描述符表,可将其视为一个矢量,每个描述符占用一项 ...

  3. AaronYang WCF教程目录

    ============================原创,讲究实践===================== 1. 那天有个小孩教我WCF[一][1/3]     基本搭建      阅读    ...

  4. 把linux的man手册转化为windows下可读的格式

    原文链接: http://www.linux521.com/2009/system/200904/1542.html 把linux的man手册转化为windows下可读的格式 我也是一个Linux学习 ...

  5. openssl - 数字证书的编程解析

    原文链接: http://www.cangfengzhe.com/wangluoanquan/37.html 这篇文章主要介绍PKI公钥体系中非常核心元素——数字证书的编程解析.在SSL,SET等安全 ...

  6. IOS 简单的 加减分 动画

    使用 shapeLayer 当动画层  其实以前有写过 类似的了 github: https://github.com/li6185377/AddScore self.pregress = [[CAS ...

  7. lua -- 物品的配置文件,表的形式保存

    do local goodsprop= { RECORDS={ []={ BuyGold= , RecoverPrice= , Value= , Param2= , UseTimesPerDay= , ...

  8. 100种不同图片切换效果插件pageSwitch

    分享100种不同图片切换效果插件pageSwitch.这是一款适用于全屏切换场景,即一切一屏,并且实现了超过一百种切换效果,支持自定义切页动画.效果图如下: 在线预览   源码下载 实现的代码. ht ...

  9. 项目中遇到的IE8浏览器访问页面过慢问题

    我目前所做的项目,由于一些控件的特殊需求,建议客户使用IE8浏览器,在测试一段时间之后,客户突然提出,IE8访问我们的系统时,界面加载非常缓慢.排查过服务器性能.网络连接等情况之后,在360浏览器访问 ...

  10. Repeater数据控件的两个重要事件ItemDataBound 和 ItemCommand

    1 ItemDataBound:数据绑定的时候(正在进行时)发生. 2 ItemCommand :用来响应Item模板中的控件的事件. 如下代码 aspx代码: [html] view plain c ...