题目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. Spring中通过Annotation来实现AOP

    一.Spring配置文件 <!--通过@AspectJ注解的形式来使用Spring AOP,强制使用CGLIB代理--> <aop:aspectj-autoproxy proxy-t ...

  2. 使用ThreadLocal来实现一个本地缓存

    大家应该知道,用户从发起请求,到服务器响应的这个过程中,在服务器中是在一个线程中的.如果我们吧查询出来的对象放到这个线程自己的缓存中,到用户请求结束时,把这些东西清理掉,应该是一个不错的cache方案 ...

  3. 初始cfx开发webservice, 简单实例应用

    项目结构图: 步骤一: 添加maven 依赖包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...

  4. android中webView加载H5,JS不能调用问题的解决

    使用了html5 页面,使用webView加载后发现 超链接的锚点不可以用 为webView设置下面两句就好了: mWebView.getSettings().setDomStorageEnabled ...

  5. [原创]茗洋AaronYang的 jquery.myselect.js 我的一次前端突破[上]

    [评论,楼层数为30的倍数的,我送你我自己的博客园的皮肤,该博客参与活动]   介绍 myselect 自己原创的js插件   这是一个可以可以支持ie6+的所有浏览器的,适应很多场景和需求的一个下拉 ...

  6. Socket网络编程--简单Web服务器(1)

    这一次的Socket系列准备讲Web服务器.就是编写一个简单的Web服务器,具体怎么做呢?我也不是很清楚流程,所以我找来了一个开源的小的Web服务器--tinyhttpd.这个服务器才500多行的代码 ...

  7. 用H5上传文件

    //1,第一步读取用户选中的文件 <input type="file" accept="image/*" onchange = "selecte ...

  8. Cublas矩阵加速运算

    前言 编写 CUDA 程序真心不是个简单的事儿,调试也不方便,很费时.那么有没有一些现成的 CUDA 库来调用呢? 答案是有的,如 CUBLAS 就是 CUDA 专门用来解决线性代数运算的库. 本文将 ...

  9. python实现合并两个文件并打印输出

    # python实现合并两个文件并打印输出 import fileinput file_Path1 = input("请输入第一个合并文件:") file_Path2 = inpu ...

  10. Oracle的NVL函数用法

    从两个表达式返回一个非 null 值. 语法 NVL(eExpression1, eExpression2) 参数eExpression1, eExpression2 如果 eExpression1 ...