pat1111-1120
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的更多相关文章
- CSU 1120 病毒(DP)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1120 解题报告:dp,用一个串去更新另一个串,递推方程是: if(b[i] > a ...
- AxureRP8实战手册(基础11-20)
本文目录 基础11. 设置文本框输入为密码 基础12. 设置打开选择文件窗口 基础13. 限制文本框输入字符位数 基础14. 设置文本框提示文字 基础15. 设置文本框回车触发事件 基础16. 设置元 ...
- csuoj 1120: 病毒
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1120 1120: 病毒 Time Limit: 3 Sec Memory Limit: 128 ...
- 九度OJ 1120 全排列 -- 实现C++STL中next_permutation()
题目地址:http://ac.jobdu.com/problem.php?pid=1120 题目描述: 给定一个由不同的小写字母组成的字符串,输出这个字符串的所有全排列. 我们假设对于小写字母有'a' ...
- PAT 1120 Friend Numbers
1120 Friend Numbers (20 分) Two integers are called "friend numbers" if they share the sa ...
- 【Codeforces Round 1120】Technocup 2019 Final Round (Div. 1)
Codeforces Round 1120 这场比赛做了\(A\).\(C\)两题,排名\(73\). \(A\)题其实过的有点莫名其妙...就是我感觉好像能找到一个反例(现在发现我的算法是对的... ...
- 日常英语---十三、MapleStory/Monsters/Level 11-20(邪恶之眼)
日常英语---十三.MapleStory/Monsters/Level 11-20(邪恶之眼) 一.总结 一句话总结: evil ['ivl] A stronger version of Evil E ...
- 1120 Friend Numbers (20 分)
1120 Friend Numbers (20 分) Two integers are called "friend numbers" if they share the same ...
- PAT甲级 1120. Friend Numbers (20)
1120. Friend Numbers (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Two in ...
- PAT 1120 Friend Numbers[简单]
1120 Friend Numbers (20 分) Two integers are called "friend numbers" if they share the same ...
随机推荐
- 浅析Xilinx 三速以太网MAC IP核
之前在使用Altera的三速以太网MAC IP的基础上,完成了UDP协议数据传输.此次为了将设计移植到xilinx FPGA上,需要用到xilinx的三速以太网MAC IP核,当然也可以自己用HDL编 ...
- python装饰器探究与参数的领取
首先上原文, 现在,假设我们要增强now()函数的功能,比如,在函数调用前后自动打印日志,但又不希望修改now()函数的定义,这种在代码运行期间动态增加功能的方式,称之为"装饰器" ...
- 用java代码发送http请求
//发送post请求 PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL ...
- 时间序列数据库rrd启动
然后执行启动定时任务目录:etc/crontab SHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=rootHOME=/ # For de ...
- wordpress配置固定链接nginx访问404问题解决方法
WordPress支持使用固定链接,但是在ngnix环境下,访问页面后出现404, 其实官方是有文档说明的,需要单独写配置, 我这边配置的示例代码如下: server { listen 8 ...
- 在Swift项目中使用OC,在OC项目中使用Swift
几天前,我开始新的App的开发了.终于有机会把swift用在实战中了,也学到了之前纯学语法时没有机会获得的知识. 这篇博文中,我就如何使用swift.OC混编做一个介绍. OC中使用Swift 首先, ...
- dedecms在任意页面调用任意栏目文章
dedecms在任意页面调用任意栏目文章,我们用arclist标签即可实现.如果是调用多个栏目文章可以给typeid多个值. 我们以调用ID为1和ID为30的两个栏目下5篇文章为例: {dede:ar ...
- Android查缺补漏(IPC篇)-- 进程间通讯基础知识热身
本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8479282.html 在Android中进程间通信是比较难的一部分,同时又非常 ...
- C/C++语言的特点
一.支持数据封装和数据隐藏 在C++中,类是支持数据封装的工具,对象则是数据封装的实现.C++通过建立用户定义类支持数据封装和数据隐藏. 在面向对象的程序设计中,将数据和对该数据进行合法操作的函数封装 ...
- 基于Jquery+Ajax+Json+存储过程 高效分页
在做后台开发中,都会有大量的列表展示,下面给大家给大家分享一套基于Jquery+Ajax+Json+存储过程高效分页列表,只需要传递几个参数即可.当然代码也有改进的地方,如果大家有更好的方法,愿留下宝 ...