1111 比较麻烦的最短路

#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 505;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y) struct Node{
int to, nx, di, ti;
}E[N*N*2];
int head[N], tot;
void add(int fr, int to, int di, int ti) {
E[tot].to = to; E[tot].di = di; E[tot].ti = ti; E[tot].nx = head[fr];
head[fr] = tot ++;
}
vector<int> ans1; int Ans1;
vector<int> ans2; int Ans2; struct Hode{
int po, di, ti;
Hode(int a=0, int b=0, int c=0):po(a), di(b), ti(c){}
bool operator < (const Hode & T) const {
if(di != T.di) return di > T.di;
else return ti > T.ti;
}
};
struct Tode{
int po, di, ti;
Tode(int a=0, int b=0, int c=0):po(a), di(b), ti(c){}
bool operator < (const Tode & T) const {
if(ti != T.ti) return ti > T.ti;
else return di > T.di;
}
}; int pre[N], vis[N];
int dis[N], tim[N];
void dfs1(int x, int tag) {
if(x == tag) return;
dfs1(pre[x], tag);
ans1.push_back(x);
}
void dfs2(int x, int tag) {
if(x == tag) return;
dfs2(pre[x], tag);
ans2.push_back(x);
} void dij1(int s, int t) {
priority_queue<Hode> Q;
memset(dis, INF, sizeof(dis));
memset(tim, INF, sizeof(tim));
dis[s] = 0; tim[s] = 0;
memset(vis, 0, sizeof(vis));
Q.push(Hode(s, dis[s], tim[s])); while(!Q.empty()) {
int po = Q.top().po; Q.pop();
if(vis[po]) continue;
vis[po] = 1;
for(int i = head[po]; ~i; i = E[i].nx) {
int to = E[i].to;
if(dis[to] > dis[po] + E[i].di) {
dis[to] = dis[po] + E[i].di;
tim[to] = tim[po] + E[i].ti;
pre[to] = po;
Q.push(Hode(to, dis[to], tim[to]));
}else if(dis[to] == dis[po] + E[i].di && tim[to] > tim[po] + E[i].ti) {
tim[to] = tim[po] + E[i].ti;
pre[to] = po;
Q.push(Hode(to, dis[to], tim[to]));
}
}
}
// printf("Distance = %d: %d", dis[t], s);
ans1.push_back(s); Ans1 = dis[t];
dfs1(t, s);
// printf("\n");
}
void dij2(int s, int t) {
priority_queue<Tode> Q;
memset(dis, INF, sizeof(dis));
memset(tim, INF, sizeof(tim));
dis[s] = 0; tim[s] = 0;
memset(vis, 0, sizeof(vis));
Q.push(Tode(s, dis[s], tim[s])); while(!Q.empty()) {
int po = Q.top().po; Q.pop();
if(vis[po]) continue;
vis[po] = 1;
for(int i = head[po]; ~i; i = E[i].nx) {
int to = E[i].to;
if(tim[to] > tim[po] + E[i].ti) {
dis[to] = dis[po] + 1;
tim[to] = tim[po] + E[i].ti;
pre[to] = po;
Q.push(Tode(to, dis[to], tim[to]));
}else if(tim[to] == tim[po] + E[i].ti && dis[to] > dis[po] + 1) {
dis[to] = dis[po] + 1;
pre[to] = po;
Q.push(Tode(to, dis[to], tim[to]));
}
}
}
ans2.push_back(s); Ans2 = tim[t];
dfs2(t, s);
}
int main() {
int n, m;
while(~scanf("%d %d", &n, &m)) {
ans1.clear(); ans2.clear();
memset(head, -1, sizeof(head));
tot = 0; for(int i = 0; i < m; ++i) {
int a, b, c, d, e; scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
add(a, b, d, e);
if(!c) add(b, a, d, e);
}
int s,t; scanf("%d %d", &s, &t); dij1(s, t);
dij2(s, t);
// if(s == t) while(1); int fl = 1;
for(int i = 0; i < min(ans1.size(), ans2.size()); ++i) {
if(ans1[i] != ans2[i]) {
fl = 0; break;
}
} if(fl) {
printf("Distance = %d; Time = %d: ", Ans1, Ans2);
for(int i = 0; i < ans1.size(); ++i) {
if(i) printf(" -> ");
printf("%d", ans1[i]);
} printf("\n");
}else {
printf("Distance = %d: ", Ans1);
for(int i = 0; i < ans1.size(); ++i) {
if(i) printf(" -> ");
printf("%d", ans1[i]);
}
printf("\nTime = %d: ", Ans2);
for(int i = 0; i < ans2.size(); ++i) {
if(i) printf(" -> ");
printf("%d", ans2[i]);
}
printf("\n");
}
}
return 0;
}

1112 gcd一下

#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 505;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y) char s[1005];
map<char, int> mp; int tot;
char table[40];
map<char, int> suc;
vector<int> vc[40]; int gcd(int a, int b) {
if(b == 0) return a;
else return gcd(b, a%b);
}
int main() {
int n;
tot = 0;
for(char i = 'a'; i <= 'z'; ++i) mp[i] = ++tot, table[tot] = i;
for(char i = '0'; i <= '9'; ++i) mp[i] = ++tot, table[tot] = i;
mp['_'] = ++tot; table[tot] = '_'; while(~scanf("%d", &n)) {
suc.clear(); for(int i = 0; i < 40; ++i) vc[i].clear();
scanf("%s", s);
int len = strlen(s);
int tmp = 0;
for(int i = 0; i <= len; ++i) {
if(i && s[i] != s[i-1]) {
vc[mp[s[i-1]]].push_back(tmp);
// printf("%c %d\n", s[i-1], tmp);
tmp = 0;
}
tmp ++;
} for(int i = 1; i <= tot; ++i) {
if(vc[i].size() > 0) {
// printf("%c ", table[i]); for(int j = 0; i < vc[i].size(); ++j) printf("%d ", vc[i][j]); printf("\n");
int tt = vc[i][0];
for(int j = 1; j < vc[i].size(); ++j) {
tt = gcd(tt, vc[i][j]);
} if(tt % n == 0) {
// printf("%c\n",table[i]);
suc[table[i]] = 1;
}
}
} tmp = 0;
for(int i = 0; i < len; ++i) {
if(suc[s[i]] > 0) {
suc[s[i]] = -1;
printf("%c", s[i]);
}
}
printf("\n"); for(int i = 0; i <= len; ++i) {
if(i && s[i] != s[i-1]) {
if(suc[s[i-1]] == -1) {
for(int j = 0; j < tmp/n; ++j) printf("%c", s[i-1]);
}
else for(int j = 0; j < tmp; ++j) printf("%c", s[i-1]);
tmp = 0;
}
tmp ++;
}
printf("\n");
}
return 0;
}

1113

#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1e5+5;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y) int A[N]; ll Abs(ll x) {
if(x < 0) return -x;
else return x;
}
int main() {
int n;
while(~scanf("%d", &n)) {
ll all = 0;
for(int i = 0; i < n; ++i) {
scanf("%d", &A[i]);
all += A[i];
}
sort(A, A+n);
ll tmp = 0;
for(int i = 0; i < n/2; ++i) {
tmp += A[i];
}
ll ans = Abs(tmp - (all-tmp));
if(n % 2) {
tmp += A[n/2];
ans = max(ans, Abs(tmp - (all-tmp)) );
} printf("%d %lld\n", n%2, ans); }
return 0;
}

1114 题意不好理解 其实就是并查集

#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1e5+5;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y) int f[N];
int find(int x) { return x == f[x]? x : f[x] = find(f[x]); }
void merge(int a, int b) {
if(b == -1 || a == -1) return;
int t1 = find(a); int t2 = find(b); if(t1 != t2) {
if(t1 > t2) f[t1] = t2; else f[t2] = t1;
}
}
int has[N];
int sett[N], area[N];
int cnt[N]; double avg_set[N], avg_area[N];
int ord[N];
int cmp(int x, int y) {
if(avg_area[x] != avg_area[y]) return avg_area[x] > avg_area[y];
else return x < y;
}
int main() {
int n;
while(~scanf("%d", &n)) {
for(int i = 0; i < 1e5; ++i) f[i] = i; for(int i = 0; i < n; ++i) {
int a,b,c; scanf("%d %d %d", &a, &b, &c);
has[a] ++; has[b] ++; has[c] ++;
merge(a, b); merge(a, c);
int d; scanf("%d", &d);
for(int j = 0; j < d; ++j) {
int l; scanf("%d", &l);
has[l] ++;
merge(a, l);
}
scanf("%d %d", &sett[a], &area[a]);
}
// printf("hh\n"); for(int i = 0; i < 1e5; ++i) {
if(has[i]) {
int t1 = find(i);
cnt[t1] ++; avg_set[t1] += sett[i]; avg_area[t1] += area[i];
}
}
int tot = 0;
for(int i = 0; i < 1e5; ++i) {
if(cnt[i]) {
avg_set[i] /= cnt[i]; avg_area[i] /= cnt[i];
ord[tot ++] = i;
}
}
sort(ord, ord + tot, cmp);
printf("%d\n", tot);
for(int i = 0; i < tot; ++i) {
printf("%04d %d %.3f %.3f\n", ord[i], cnt[ord[i]], avg_set[ord[i]], avg_area[ord[i]]);
}
}
return 0;
}

1115

#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1e3+5;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y) int num[N];
int L[N];
int R[N];
int root = 0;
void insert(int x, int nw) {
if(root == -1) {
root = x; return;
}
if(num[x] <= num[nw]) {
if(L[nw]) insert(x, L[nw]);
else L[nw] = x;
}else {
if(R[nw]) insert(x, R[nw]);
else R[nw] = x;
}
}
int cnt[N];
int Deep;
void dfs(int x, int dep) {
Deep = max(Deep, dep);
cnt[dep] ++; if(L[x]) dfs(L[x], dep+1);
if(R[x]) dfs(R[x], dep+1);
}
int main() {
int n;
while(~scanf("%d", &n)) {
root = -1;
for(int i = 0; i < n; ++i) {
scanf("%d", &num[i]);
insert(i, root);
}
Deep = -1;
dfs(root, 0); printf("%d + %d = %d\n", cnt[Deep], cnt[Deep-1], cnt[Deep] + cnt[Deep-1]);
}
return 0;
}

1116

#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1e5+5;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y) int Rank[N];
int has[N];
int prime(int x) {
if(x == 1) return 0;
if(x == 2) return 1;
for(int i = 2; i <= sqrt(x); ++i) {
if(x % i == 0)
return 0;
}
return 1;
}
int main() {
int n;
while(~scanf("%d", &n)) {
memset(has, 0, sizeof(has));
for(int i = 0; i < n; ++i) {
int a; scanf("%d", &a);
Rank[a] = i+1;
}
int k; scanf("%d", &k);
while(k--) {
int a; scanf("%d", &a);
if(has[a]) printf("%04d: Checked\n", a);
else if(Rank[a] == 0) printf("%04d: Are you kidding?\n", a);
else if(Rank[a] == 1) printf("%04d: Mystery Award\n", a);
else if(prime(Rank[a])) printf("%04d: Minion\n", a);
else printf("%04d: Chocolate\n", a);
if(Rank[a]) has[a] = 1;
}
}
return 0;
}

1117这题题意也是扯淡啊,,,,

#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1e5+5;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y) int a[N];
int main() {
int n;
while(~scanf("%d", &n)) {
for(int i = 0; i < n; ++i) scanf("%d", &a[i]);
sort(a, a+n);
a[n] = INF; int ans = -1;
for(int i = 0; i < n; ++i) {
if(a[i] != a[i+1]) {
ans = max(ans, min(n-i-1, a[i+1]-1) );
}
}
ans = max(ans, min(n, a[0]-1));
printf("%d\n", ans);
}
return 0;
}

1118

include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1e4+5;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y) int fa[N];
int cnt[N];
int find(int x) { return x == fa[x]? x : fa[x] = find(fa[x]); } int main() {
int n;
while(~scanf("%d", &n)) {
memset(cnt, 0, sizeof(cnt));
for(int i = 1; i <= 1e4; ++i) fa[i] = i; int m = -1;
for(int i = 1; i <= n; ++i) {
int a; scanf("%d", &a);
int pre;
if(a) {
scanf("%d", &pre);
m = max(m, pre);
}
for(int j = 1; j < a; ++j) {
int b; scanf("%d", &b);
m = max(m, b);
int t1 = find(pre); int t2 = find(b);
if(t1 != t2) fa[t2] = t1;
}
}
for(int i = 1; i <= m; ++i) {
cnt[find(i)] ++;
}
int all = 0; int maxx = -1;
for(int i = 1; i <= m; ++i) {
if(cnt[i]) {
all ++;
maxx = max(maxx, cnt[i]);
}
}
printf("%d %d\n", all, m); int k; scanf("%d", &k);
for(int i = 0; i < k; ++i) {
int a; int b; scanf("%d %d", &a, &b);
int t1 = find(a); int t2 = find(b);
if(t1 == t2) printf("Yes\n");
else printf("No\n");
} }
return 0;
}

1119 主要对于先后序的概念需要掌握

#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1e5+5;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y) int pre[N];
int post[N];
int L[N], R[N];
int root; int fl = 1; void solve(int _l, int _r, int l, int r, int rt) {
// printf("%d %d %d %d %d\n", _l,_r,l,r,rt);
if(l > r) return; int ro = pre[_l];
int pos = 0;
for(int i = l; i <= r; ++i) {
if(post[i] == ro) {
pos = i; break;
}
} if(pos == l && pos != r) {
L[rt] = post[pos];
R[rt] = pre[_l+1];
solve(_l+2, _r, l+1, r-1, pre[_l+1]);
}else if(pos == r){
fl = 0;
L[rt] = post[pos];
solve(_l+1, _r, l, r-1, post[pos]);
}else {
// printf("hh %d\n", rt);
L[rt] = post[pos];
int _pos = (pos-l) + _l;
// printf("%d\n", _pos, pre[_pos]);
R[rt] = pre[_pos+1];
solve(_l+1, _pos, l, pos-1, post[pos]);
solve(_pos+2, _r, pos+1, r-1, pre[_pos+1]);
} } int nn = 0;
void dfs(int x) {
if(L[x]) dfs(L[x]);
if(!nn) nn = 1; else printf(" ");
printf("%d", x);
if(R[x]) dfs(R[x]);
}
int main() {
int n;
while(~scanf("%d", &n)) {
memset(L, 0, sizeof(L));
memset(R, 0, sizeof(R));
nn = 0; for(int i = 1; i <= n; ++i) {
scanf("%d", &pre[i]);
}
for(int i = 1; i <= n; ++i) {
scanf("%d", &post[i]);
}
root = pre[1];
solve(2, n, 1, n-1, root); if(fl) printf("Yes\n");
else printf("No\n");
dfs(root);
printf("\n");
}
return 0;
}

1120

#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1e4+5;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y) map<int, int> mp;
map<int, int>::iterator it; void solve(int x) {
int tt = 0;
while(x) {
tt += x%10;
x /= 10;
}
mp[tt] ++;
}
int main() {
int n;
while(~scanf("%d", &n)) {
mp.clear(); for(int i = 0; i < n; ++i) {
int a; scanf("%d", &a);
solve(a);
} int cnt = 0;
for(it = mp.begin(); it != mp.end(); ++it) {
if(it->second > 0) {
cnt ++;
}
}
printf("%d\n", cnt);
int fl = 0;
for(it = mp.begin(); it != mp.end(); ++it) {
if(it->second > 0) {
if(!fl) fl = 1; else printf(" ");
printf("%d", it->first);
}
} printf("\n"); }
return 0;
}

pat1111-1120的更多相关文章

  1. CSU 1120 病毒(DP)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1120 解题报告:dp,用一个串去更新另一个串,递推方程是: if(b[i] > a ...

  2. AxureRP8实战手册(基础11-20)

    本文目录 基础11. 设置文本框输入为密码 基础12. 设置打开选择文件窗口 基础13. 限制文本框输入字符位数 基础14. 设置文本框提示文字 基础15. 设置文本框回车触发事件 基础16. 设置元 ...

  3. csuoj 1120: 病毒

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1120 1120: 病毒 Time Limit: 3 Sec  Memory Limit: 128 ...

  4. 九度OJ 1120 全排列 -- 实现C++STL中next_permutation()

    题目地址:http://ac.jobdu.com/problem.php?pid=1120 题目描述: 给定一个由不同的小写字母组成的字符串,输出这个字符串的所有全排列. 我们假设对于小写字母有'a' ...

  5. PAT 1120 Friend Numbers

    1120 Friend Numbers (20 分)   Two integers are called "friend numbers" if they share the sa ...

  6. 【Codeforces Round 1120】Technocup 2019 Final Round (Div. 1)

    Codeforces Round 1120 这场比赛做了\(A\).\(C\)两题,排名\(73\). \(A\)题其实过的有点莫名其妙...就是我感觉好像能找到一个反例(现在发现我的算法是对的... ...

  7. 日常英语---十三、MapleStory/Monsters/Level 11-20(邪恶之眼)

    日常英语---十三.MapleStory/Monsters/Level 11-20(邪恶之眼) 一.总结 一句话总结: evil ['ivl] A stronger version of Evil E ...

  8. 1120 Friend Numbers (20 分)

    1120 Friend Numbers (20 分) Two integers are called "friend numbers" if they share the same ...

  9. PAT甲级 1120. Friend Numbers (20)

    1120. Friend Numbers (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Two in ...

  10. PAT 1120 Friend Numbers[简单]

    1120 Friend Numbers (20 分) Two integers are called "friend numbers" if they share the same ...

随机推荐

  1. 无线渗透测试之wifi密码破解

    [声明]:本文仅供个人学习使用,请勿违法破解他人wifi 测试工具: 1.CDlinux启动盘:(请参照https://my.oschina.net/u/3112136/blog/800713) 2. ...

  2. 洛谷 [P2756] 飞行员配对方案问题

    二分图匹配裸题 可以用匈牙利做,简单高效 输出具体的匹配路径时 ,直接输出match数组即可 #include <iostream> #include <cstdio> #in ...

  3. BZOJ 3697: 采药人的路径 [点分治] [我想上化学课]

    传送门 题意: 路径有$-1,1$两种权值,求有多少路径满足权值和为$0$且有一个点将路径分成权值和为$0$的两段 第四节课本来想去上化学,然后快上课了这道题还没调出来.....可恶我想上化学 昨天两 ...

  4. js实现点击切换显示隐藏,点击其它位置再隐藏

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. 新版Azure Automation Account 浅析(三) --- 用Runbook管理AAD Application Key

    新版Azure Automation Account 浅析(三) --- 用Runbook管理AAD应用的Key 前篇讲过有一个面向公众的Runbook库,社区和微软一直往其中加入新的Runbook, ...

  6. qt程序启动画面

  7. 【翻译】CSS Animations VS the Web Animations API:案例学习

    原文地址:CSS Animations vs the Web Animations API: A Case Study May 03, 2017 css, javascript 上周我写了我如何使用C ...

  8. [Python Study Notes]批量将ppt转换为pdf v1.0

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  9. 框架学习笔记之Maven简介和配置

    一.什么是Maven?★Maven可翻译为“知识的积累”.“专家”.“内行”,它是一个跨平台的项目管理工具.★Maven提供了开发人员构建一个完整的生命周期框架,开发团队可以自动完成项目的基础工具建设 ...

  10. Mongodb基础与入门

    一:基本了解                1. 特点                        基于分布式文件存储的NoSql数据库.能为WEB应用提供可扩展的高性能数据存储解决方案.      ...