PAT甲级1013-1014-1015
思路:城市数也就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 ;
}
思路:大模拟。我好菜系列。
用队列模拟每个窗口排队的人。没满的时候就是从左到右排就行了,满了之后就是哪个队伍先有人走哪个队伍就先进去。这里用优先队列模拟。
坑点是,如果在五点之前被服务了,服务时间超过五点也没关系,但是如果开始服务时间就已经超过五点了就要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 ;
}
思路:感觉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的更多相关文章
- PAT甲级1013. Battle Over Cities
PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...
- 图论 - PAT甲级 1013 Battle Over Cities C++
PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...
- pat甲级1013
1013 Battle Over Cities (25)(25 分) It is vitally important to have all the cities connected by highw ...
- 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 ...
- PAT甲级1013题解——并查集+路径压缩
题目分析: 本题初步浏览题目就知道是并查集的模板题,数据输入范围N为1~1000,则M的范围为0~1000^2,通过结构体记录每一对连线的关系,p[]数组记录每个节点的跟,对于k次查询,每次都要重新维 ...
- 【PAT甲级】1014 Waiting in Line (30 分)(队列维护)
题面: 输入四个正整数N,M,K,Q(N<=20,M<=10,K,Q<=1000),N为银行窗口数量,M为黄线内最大人数,K为需要服务的人数,Q为查询次数.输入K个正整数,分别代表每 ...
- PAT甲级1014. Waiting in Line
PAT甲级1014. Waiting in Line 题意: 假设银行有N个窗口可以开放服务.窗前有一条黄线,将等候区分为两部分.客户要排队的规则是: 每个窗口前面的黄线内的空间足以包含与M个客户的一 ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级考前整理(2019年3月备考)之一
转载请注明出处:https://www.cnblogs.com/jlyg/p/7525244.html 终于在考前,刷完PAT甲级131道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...
- 【转载】【PAT】PAT甲级题型分类整理
最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...
随机推荐
- python3用BeautifulSoup抓取id='xiaodeng',且正则包含‘elsie’的标签
# -*- coding:utf-8 -*- #python 2.7 #XiaoDeng #http://tieba.baidu.com/p/2460150866 #使用多个指定名字的参数可以同时过滤 ...
- Centos7安装SVN服务器
1.关闭防火墙systemctl stop firewalld.service 2.安装svn服务器yum install subversion 3.创建放置项目project的地方mkdir /sv ...
- Xilinx 常用模块汇总(verilog)【04】
作者:桂. 时间:2018-05-15 13:07:02 链接:http://www.cnblogs.com/xingshansi/p/9040472.html 前言 Xilinx 常用模块汇总(v ...
- Atitit 快速开发体系建设路线图
Atitit 快速开发体系建设路线图 1.1. 项目类型划分 哑铃型 橄榄型 直板型(可以立即实行)1 1.2. 解决方案知识库 最佳实践库 最佳流程优化(已成,需要一些整理)2 1.3. 功能模板 ...
- Android Studio Prettify 插件
1.功能:能够一键声明layout文件中的所有注明id的控件,节省时间 2.github地址 https://github.com/Haehnchen/idea-android-studio-plug ...
- 【Spark 深入学习 -09】Spark生态组件及Master节点HA
----本节内容------- 1.Spark背景介绍 2.Spark是什么 3.Spark有什么 4.Spark部署 4.1.Spark部署的2方面 4.2.Spark编译 4.3.Spark St ...
- ES6,Array.find()和findIndex()函数的用法
ES6为Array增加了find(),findIndex函数. find()函数用来查找目标元素,找到就返回该元素,找不到返回undefined. findIndex()函数也是查找目标元素,找到就返 ...
- Android Launcher分析和修改4——初始化加载数据
上面一篇文章说了Launcher是如何被启动的,Launcher启动的过程主要是加载界面数据然后显示出来, 界面数据都是系统APP有关的数据,都是从Launcher的数据库读取,下面我们详细分析Lau ...
- Git 重命名操作
截至目前,Tome 和Jerry 都使用手动命令来编译自己的项目.Jerry 决定为他们的项目创建 Makefile,并给予适当的名称来命名“string.c” 文件. [jerry@CentOS p ...
- Java知多少(90)菜单
有两种类型的菜单:下拉式菜单和弹出式菜单.本章只讨论下拉式菜单编程方法.菜单与JComboBox和JCheckBox不同,它们在界面中是一直可见的.菜单与JComboBox的相同之处是每次只可选择一个 ...