1140 Look-and-say Sequence

思路:模拟

#include<bits/stdc++.h>
using namespace std; typedef long long ll;
int d,n;
const int maxn = 10010;
int cnt[11];
vector<int> a;
vector<int> b; int main(){
scanf("%d%d",&d,&n);
a.push_back(d);
if(n == 1) {
printf("%lld",d);
return 0;
}
for(int i=2;i<=n;i++){
int j = 0;
b.clear();
int len = a.size();
while(j < len){
int curv = a[j];
int cnt = 0;
while(j<len && a[j] == curv){
cnt++;
j++;
}
b.push_back(curv);
b.push_back(cnt);
}
a.clear();
for(int j=0;j<b.size();j++) a.push_back(b[j]);
}
for(int i=0;i<a.size();i++){
printf("%d",a[i]);
}
return 0;
}

1141 PAT Ranking of Institutions

思路:结构体排序,map统计查询

#include<bits/stdc++.h>
#include<algorithm>
#include<string>
using namespace std; int n;
const int maxn = 1e5+10;
struct node{
string id;
double score;
string cap;
}; struct node stu[maxn];
map<string,int> mp;
map<string,double> scores; struct capNode{
string cap;
int score;
int nums;
capNode(string ss,double cc,int num){
cap = ss;
score = cc;
nums = num;
}
}; vector<capNode> vec; bool cmp(capNode x,capNode y){
if(x.score == y.score) {
if(x.nums == y.nums) return x.cap < y.cap;
return x.nums < y.nums;
}
return x.score > y.score;
} int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
string curCap;
cin>>stu[i].id>>stu[i].score>>curCap;
transform(curCap.begin(),curCap.end(),curCap.begin(),::tolower);
stu[i].cap = curCap;
mp[stu[i].cap]++;
if(stu[i].id[0] == 'T'){
scores[stu[i].cap] += stu[i].score*1.5;
}else if(stu[i].id[0] == 'A'){
scores[stu[i].cap] += stu[i].score;
}else{
scores[stu[i].cap] += stu[i].score*1.0/1.5;
}
}
map<string,int>::iterator it = mp.begin();
while(it!=mp.end()){
string name = it->first;
int num = it->second;
int sc = (int)scores[name];
vec.push_back(capNode(name,sc,num));
it++;
}
sort(vec.begin(),vec.end(),cmp);
int len = vec.size();
printf("%d\n",len);
if(len > 0){
int lastScore = vec[0].score;
int rank = 1;
for(int i=0;i<len;i++){
if(vec[i].score == lastScore){
cout<<rank<<" "<<vec[i].cap<<" "<<vec[i].score<<" "<<vec[i].nums<<endl;
}else{
rank = i+1;
lastScore = vec[i].score;
cout<<rank<<" "<<vec[i].cap<<" "<<vec[i].score<<" "<<vec[i].nums<<endl;
}
}
}
return 0;
}

1142 Maximal Clique

思路:图论,判断是否同一“集合”,按题目要求判断是否有边

#include<bits/stdc++.h>
using namespace std; const int maxn = 210;
int n,ne;
int m;
int g[maxn][maxn];
int a[maxn];
int vis[maxn];
int k; bool isClique(){
for(int i=1;i<=k;i++){
for(int j=1;j<=k;j++){
if(i!=j && g[a[i]][a[j]] == 0) return false;
}
}
return true;
} bool isMaxClique(){
for(int i=1;i<=n;i++) vis[i] = 0;
for(int i=1;i<=k;i++) vis[a[i]] = 1;
for(int i=1;i<=n;i++){
if(!vis[i]){
for(int p=1;p<=k;p++){
for(int q=1;q<=k;q++){
if(p != q || (k==1)){ //这里特判k==1的情况 如样例图中查询1 8:8号结点应该输出Not Maximal
if(g[i][a[p]]==1 && g[i][a[q]] == 1){
return false;
}
}
}
}
}
}
return true;
} int main(){
scanf("%d%d",&n,&ne);
for(int i=1;i<=ne;i++){
int u,v;
scanf("%d%d",&u,&v);
g[u][v] = g[v][u] = 1;
}
scanf("%d",&m);
for(int i=1;i<=m;i++){
scanf("%d",&k);
for(int j=1;j<=k;j++) scanf("%d",&a[j]);
bool flag1 = isClique();
bool flag2 = isMaxClique();
if(flag1 == false){
puts("Not a Clique");
}else{
if(flag2 == false){
puts("Not Maximal");
}else{
puts("Yes");
}
}
}
return 0;
}

1143 Lowest Common Ancestor 29/30

思路:给先序序列建树,找最近公共祖先

一个点段错误,有时间再补。。

#include<bits/stdc++.h>
using namespace std; const int maxn = 10010;
int m,n;
struct node{
int v;
node *l;
node *r;
};
int a[maxn];
set<int> se;
int deep[maxn];
int fa[maxn]; void getDeep(node *root,int depth,int father){
if(root == NULL) return;
deep[root->v] = depth;
fa[root->v] = father;
if(root->l != NULL) getDeep(root->l,depth+1,root->v);
if(root->r != NULL) getDeep(root->r,depth+1,root->v);
} void build(int pos,node *root){
if(root == NULL) return;
node* fa = root;
int v = a[pos];
if(v < fa->v){
if(fa->l == NULL){
node *cur = new node();
cur->v = v;
cur->l = NULL;
cur->r = NULL;
fa->l = cur;
return;
}
build(pos,fa->l);
}else{
if(fa->r == NULL){
node *cur = new node();
cur->v = v;
cur->l = NULL;
cur->r = NULL;
fa->r = cur;
return;
}
build(pos,fa->r);
}
return;
} int main(){
scanf("%d%d",&m,&n);
for(int i=1;i<=n;i++) {
scanf("%d",&a[i]);
se.insert(a[i]);
}
node *Root = new node();
Root->l = NULL;
Root->r = NULL;
if(n>=1) Root->v = a[1];
else Root->v = 0;
if(n>=2) for(int i=2;i<=n;i++) build(i,Root);
getDeep(Root,1,0);
for(int i=1;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
bool visu = true;
bool visv = true;
if(se.find(u) == se.end()) visu = false;
if(se.find(v) == se.end()) visv = false;
if(visu == false || visv == false){
if(visu==false && visv==false) printf("ERROR: %d and %d are not found.\n",u,v);
else if(visu == false) printf("ERROR: %d is not found.\n",u);
else printf("ERROR: %d is not found.\n",v);
}else{
if(deep[u] < deep[v]){
int p = u;
int q = v;
while(deep[q] > deep[p]) q = fa[q];
if(q == p) printf("%d is an ancestor of %d.\n",u,v);
else{
while(p != q){
p = fa[p];
q = fa[q];
}
printf("LCA of %d and %d is %d.\n",u,v,p);
}
}else{
int p = u;
int q = v;
while(deep[p] > deep[q]) p = fa[p];
if(p == q) printf("%d is an ancestor of %d.\n",v,u);
else{
while(p != q){
p = fa[p];
q = fa[q];
}
printf("LCA of %d and %d is %d.\n",u,v,p);
}
}
}
}
return 0;
}

PTA 1140 1141 1142 1143的更多相关文章

  1. PAT甲级满分攻略|记一次考试经历

    一次考试经历 今天是"大雪",很冷. 来到隔壁的学校考试,记得上一次来河中医是两年前大一刚开学吧,那天晚上印象比较深刻,6个室友骑车到处闲逛.当时还不会Hello world. 很 ...

  2. Bzoj索引

    1001 : http://ideone.com/4omPYJ1002 : http://ideone.com/BZr9KF1003 : http://ideone.com/48NJNh1004 : ...

  3. Hsql中In没有1000的限制

    SELECT * FROM user , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...

  4. C++版-剑指offer 面试题6:重建二叉树(Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal) 解题报告

    剑指offer 重建二叉树 提交网址: http://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6?tpId=13&tq ...

  5. 何为编码 GBK 和 UTF8编码?GBK,GB2312与区位码有何关系?

    何为GBK,何为GB2312,与区位码有何渊源? 区位码是早些年(1980)中国制定的一个编码标准,如果有玩过小霸王学习机的话,应该会记得有个叫做“区位”的输入法(没记错的话是按F4选择).就是打四个 ...

  6. 自动化安装smokeping-2.6.11脚本

    自动化安装Smokeping-2.6.11脚本 一.目的 1.1 监控目的 为方便监测各数据中心网络状况,自定义全国各节点,从而发现网络异常,判断网络故障. 1.2 本文目的 快速部署Smokepin ...

  7. CPictureEx类

    CPictueEx类不仅可以显示GIF(包括GIF动画),还可以显示JPEG.BMP.WMF.ICO.CUR等. 参考:https://www.codeproject.com/Articles/142 ...

  8. 一个封装比较完整的FTP类——clsFTP

    前几天,看见园子里面的博友写了一个支持断点续传的FTP类,一时技痒,干脆写了个更完整的clsFtp类.只是我写这个clsFtp不是支持断点续传的目的,而是为了封装FTP几个基本常用的操作接口. 功能 ...

  9. python多线程同步实例分析

    进程之间通信与线程同步是一个历久弥新的话题,对编程稍有了解应该都知道,但是细说又说不清.一方面除了工作中可能用的比较少,另一方面就是这些概念牵涉到的东西比较多,而且相对较深.网络编程,服务端编程,并发 ...

随机推荐

  1. [考试反思]0814NOIP模拟测试21

    前两名是外校的240.220.kx和skyh拿到了190的[暴力打满]的好成绩. 我第5是170分,然而160分就是第19了. 在前一晚上刚刚爆炸完毕后,心态格外平稳. 想想前一天晚上的挣扎: 啊啊啊 ...

  2. MySQL集群搭建详解(三种结点分离)

    本文将搭建一个最简化的MySQL Cluster系统,配置方法中的所有命令都是以root账户运行.这个MySQL Cluster包含一个管理结点.两个数据结点.两个SQL结点,这五个结点会分别安装在五 ...

  3. 2684亿!阿里CTO张建锋:不是任何一朵云都撑得住双11

    2019天猫双11 成交额2684亿! "不是任何一朵云都能撑住这个流量.中国有两朵云,一朵是阿里云,一朵叫其他云."11月11日晚,阿里巴巴集团CTO张建锋表示,"阿里 ...

  4. iOS蓝牙--CoreBluetooth基本使用

    蓝牙使用步骤: 1. 扫描外设 2. 连接外设 3. 连上外设后,获取指定外设的服务 4. 获取服务后,遍历服务的特征,得到可读,可写等特征,然后与中心管理者进行数据交互 附上代码 一:导入框架 #i ...

  5. MyBatis动态语句if与choose的区别

    if(通过“title”和“author”两个参数进行可选搜索): <select id="findActiveBlogLike" resultType="Blog ...

  6. 矢量图形(Vector Picture, SVG, PDF)转TiKZ代码

    在使用LaTeX的过程中,我们需要往往需要使用一些图片,譬如,在样式文件中,但是如果在样式文件中使用外部的图片,总感觉不是那么地舒服「请原谅强迫症」.因此,想办法将图形内嵌入LaTeX文件. 首先,我 ...

  7. C语言程序设计100例之(14):丑数

    例14   丑数 问题描述 丑数是其质因子只可能是2,3或5的数.前10个丑数分别为1, 2, 3, 4, 5, 6, 8, 9, 10, 12.输入一个正整数n,求第n个丑数. 输入格式 每行为一个 ...

  8. hdu 1171 Big Event in HDU (01背包, 母函数)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  9. ArcGIS API For Javascript :如何在地图上做出点位脉冲闪烁的效果

    日常地图表达中我们通常使用的地图符号多是静态地图符号,时间久了会造成视觉审美疲劳,也没有现代感. 在这种背景下,对现有地图符号进行简单处理,即可得到色彩鲜艳,对比度强烈,活灵活现的地图表达形式. 灵感 ...

  10. SpringBoot学习(三)—— springboot快速整合swagger文档

    目录 MyBatis 简介 引入mybatis组件 代码实战 MyBatis @ 简介 优点 最大的优点是SQL语句灵活,适合调优情景,业务复杂情景 劣势 最大的劣势是不同数据库之间的迁移 引入myb ...