pat1041-1050
没想到半天就做完了10题 = =,这几题太简单了,基本10分钟一题
1041
#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#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];
map<int, int> mp;
int main() {
int n;
while(~scanf("%d", &n)) {
mp.clear();
for(int i = 1; i <= n; ++i) scanf("%d", &A[i]), mp[A[i]] ++;
int fl = 0;
for(int i = 1; i <= n; ++i) {
if(mp[A[i]] == 1) {
printf("%d\n", A[i]); fl = 1;
break;
}
}
if(!fl) printf("None\n");
}
return 0;
}
1042
#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 4e4+5;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y)
int card[2][N]; int fl;
void put(int x) {
int t1 = (x-1)/13;
int t2 = (x-1)%13;
t2 ++;
if(t1 == 4) printf("J%d", t2);
else if(t1 == 3) printf("D%d", t2);
else if(t1 == 2) printf("C%d", t2);
else if(t1 == 1) printf("H%d", t2);
else printf("S%d", t2);
}
int Swap[60];
int main() {
int K;
while(~scanf("%d", &K)) {
fl = 0;
for(int i = 1; i <= 54; ++i) card[fl][i] = i;
for(int i = 1; i <= 54; ++i) scanf("%d", &Swap[i]);
for(int i = 0; i < K; ++i) {
for(int j = 1; j <= 54; ++j) {
card[fl^1][Swap[j]] = card[fl][j];
}
fl ^= 1;
}
for(int i = 1; i <= 54; ++i) {
if(i != 1) printf(" ");
put(card[fl][i]);
}
printf("\n");
}
return 0;
}
1043 这题还是花了一点时间。这是一个反证思想,虽然你按照这个序列插入的树,其他序列可能插入也是这样,但是你反过来验证一下不就行了= =
#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#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 n;
int A[N];
int lef[N], righ[N];
int root;
void find(int x, int id) {
if(A[x] <= A[id]) {
if(righ[x] == 0) righ[x] = id;
else find(righ[x], id);
}else {
if(lef[x] == 0) lef[x] = id;
else find(lef[x], id);
}
}
void _find(int x, int id) {
if(A[x] > A[id]) {
if(righ[x] == 0) righ[x] = id;
else _find(righ[x], id);
}else {
if(lef[x] == 0) lef[x] = id;
else _find(lef[x], id);
}
}
int output[N]; int cnt = 0;
void preorder(int x) {
++cnt; output[cnt] = x;
if(lef[x]) preorder(lef[x]);
if(righ[x]) preorder(righ[x]);
}
void postorder(int x) {
if(lef[x]) postorder(lef[x]);
if(righ[x]) postorder(righ[x]);
++cnt; output[cnt] = x;
}
int main() {
while(~scanf("%d", &n)) {
memset(lef, 0, sizeof(lef));
memset(righ, 0, sizeof(righ));
for(int i = 1; i <= n; ++i) {
scanf("%d", &A[i]);
}
root = 1;
for(int i = 2; i <= n; ++i) {
find(root, i);
}
cnt = 0;
preorder(root);
int flag = 1;
for(int i = 1; i <= n; ++i) {
if(output[i] != i) {
flag = 0; break;
}
}
if(flag) {
cnt = 0;
printf("YES\n");
postorder(root);
for(int i = 1; i <= n; ++i) {
if(i != 1) printf(" ");
printf("%d", A[output[i]]);
}
printf("\n");
continue;
}
memset(lef, 0, sizeof(lef));
memset(righ, 0, sizeof(righ));
root = 1;
for(int i = 2; i <= n; ++i) {
_find(root, i);
}
cnt = 0;
preorder(root);
flag = 1;
for(int i = 1; i <= n; ++i) {
if(output[i] != i) {
flag = 0; break;
}
}
if(flag) {
cnt = 0;
printf("YES\n");
postorder(root);
for(int i = 1; i <= n; ++i) {
if(i != 1) printf(" ");
printf("%d", A[output[i]]);
}
printf("\n");
}else printf("NO\n");
}
return 0;
}
1044 二分一下就行,比较简单
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
#include<ctime>
#include<cstdlib>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5+5;
typedef long long ll;
int value[N];
int suf[N];
int main() {
int n, m;
while(~scanf("%d %d", &n, &m)) {
int fl = 0;
for(int i = 1; i <= n; ++i) {
scanf("%d", &value[i]);
// if(value[i] <= 0) while(1);
}
suf[0] = 0;
int pre = 0;
int minn = INF;
for(int i = 1; i <= n; ++i) {
pre += value[i];
suf[i] = pre;
// printf("%d ", suf[i]);
// if(i == 1) continue;
int id = lower_bound(suf, suf+i, pre-m) - suf;
if(suf[id] == pre-m) {
printf("%d-%d\n", id+1, i);
fl = 1;
}else {
if(id == 0) continue;
int tt = suf[i] - suf[id-1];
if(tt > m) minn = min(minn, tt);
}
}
if(!fl) {
pre = 0;
for(int i = 1; i <= n; ++i) {
pre += value[i];
suf[i] = pre;
// if(i == 1) continue;
int id = lower_bound(suf, suf+i, pre - minn) - suf;
if(suf[id] == pre - minn) {
printf("%d-%d\n", id+1, i);
// fl = 1;
}
}
}
}
return 0;
}
1045 这题dp,每次讨论一个点,找到此时,小于等于该物品喜欢程度为结尾的物品的最大值,当然我用树状数组加速了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
#include<ctime>
#include<cstdlib>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5+5;
typedef long long ll;
int fav[205];
int fav_[205];
int stripe[10005];
int tree[205];
int n, m, len;
void add(int pos, int num) {
for(int i = pos; i <= m; i += i&-i) {
tree[i] = max(tree[i], num);
}
}
int sum(int pos) {
int ans = 0;
for(int i = pos; i; i -= i&-i) {
ans = max(ans, tree[i]);
}
return ans;
}
int main() {
while(~scanf("%d", &n)) {
memset(fav_, 0, sizeof(fav_));
memset(tree, 0, sizeof(tree));
scanf("%d", &m);
for(int i = 1; i <= m; ++i) {
scanf("%d", &fav[i]);
fav_[fav[i]] = i;
}
scanf("%d", &len);
for(int i = 1; i <= len; ++i) {
scanf("%d", &stripe[i]);
}
for(int i = 1; i <= len; ++i) {
if(fav_[stripe[i]] == 0) continue;
int id = fav_[stripe[i]];
int tt = sum(id)+1;
add(id, tt);
}
printf("%d\n", sum(m));
}
return 0;
}
1046
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
#include<ctime>
#include<cstdlib>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5+5;
typedef long long ll;
int d[N];
ll suf[N];
int main() {
int n, m;
while(~scanf("%d", &n)) {
suf[0] = 0;
for(int i = 1; i <= n; ++i) {
scanf("%d", &d[i]);
suf[i] = suf[i-1] + d[i];
}
scanf("%d", &m);
for(int i = 1; i <= m; ++i) {
int a, b; scanf("%d %d", &a, &b);
if(a > b) swap(a, b);
ll tmp = suf[b-1] - suf[a-1];
ll ans = min(tmp, suf[n] - tmp);
printf("%lld\n", ans);
}
}
return 0;
}
1047 还是先将字符串处理成了数字,这样更快
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
#include<ctime>
#include<cstdlib>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5+5;
typedef long long ll;
int change(char *a) {
int ans = 0;
for(int i = 0; i < 3; ++i) {
ans = ans*100 + a[i]-'A';
}
ans = ans*100 + a[3]-'0'+30;
return ans;
}
void put(int x) {
printf("%c%c%c%c\n", x/1000000+'A', (x%1000000)/10000+'A', (x%10000)/100+'A', (x%100-30)+'0' );
}
vector<int> course[2505];
int main() {
int n, k;
while(~scanf("%d %d", &n, &k)) {
for(int i = 1; i <= k; ++i) course[i].clear();
for(int i = 0; i < n; ++i) {
char s[10]; int a;
scanf("%s %d", s, &a);
int id = change(s);
for(int j = 0; j < a; ++j) {
int b; scanf("%d", &b);
course[b].push_back(id);
}
}
for(int i = 1; i <= k; ++i) {
printf("%d %d\n", i, course[i].size());
sort(course[i].begin(), course[i].end());
for(int j = 0; j < course[i].size(); ++j) {
put(course[i][j]);
}
}
}
return 0;
}
1048
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<map>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5+5;
typedef long long ll;
int coin[N];
map<int, int> mp;
int main() {
int n, m;
while(~scanf("%d %d", &n, &m)) {
mp.clear();
for(int i = 1; i <= n; ++i) {
scanf("%d", &coin[i]);
mp[coin[i]] ++;
}
sort(coin+1, coin+n+1);
int fl = 0;
for(int i = 1; i <= n; ++i) {
int re = m - coin[i];
if(re == coin[i]) {
if(mp[re] >= 2) {
printf("%d %d\n", re, re); fl = 1;
break;
}
}else {
if(mp.find(re) != mp.end()) {
printf("%d %d\n", coin[i], re); fl = 1;
break;
}
}
}
if(!fl) printf("No Solution\n");
}
return 0;
}
1049 这题还是要想下,先预处理 0-999999 那么一个大于1e6的数就可以拆分成前五位,后五位来做就行啦
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<map>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e6;
typedef long long ll;
ll num[N];
int cal(int x) {
int ans = 0;
while(x) {
int tt = x%10;
if(tt == 1) ans ++;
x /= 10;
}
return ans;
}
int main() {
num[0] = 0;
for(int i = 1; i < N; ++i) {
num[i] = num[i-1] + cal(i);
}
int x;
while(~scanf("%d", &x)) {
if(x < 1e6) printf("%lld\n", num[x]);
else {
int t1 = x % 1000000; int t2 = x / 1000000;
ll ans = (num[t2]-cal(t2)) * 1e6 + 1ll*cal(t2)*(t1+1) + num[999999] * t2 + num[t1];
printf("%lld\n", ans);
}
}
return 0;
}
1050
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<map>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e4+5;
typedef long long ll;
char a[N], b[N];
int has[200];
int main() {
memset(has, 0, sizeof(has));
gets(a); gets(b);
for(int i = 0; b[i]; ++i) {
has[b[i]] ++;
}
for(int i = 0; a[i]; ++i) {
if(!has[a[i]]) printf("%c", a[i]);
}
printf("\n");
return 0;
}
pat1041-1050的更多相关文章
- [Q&A] MySQL Error 1050(42S01): Table already exist
[环境说明] 1:MySQL Server 5.5 2:MyEclipse 2014 3:JDK 1.7 造成该问题的可能原因: 1:用 Java 读取 SQL 文件,并执行其中的 sql 语句,但是 ...
- ytu 1050:写一个函数,使给定的一个二维数组(3×3)转置,即行列互换(水题)
1050: 写一个函数,使给定的一个二维数组(3×3)转置,即行列互换 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 154 Solved: 112[ ...
- magento -- 解决magento错误:ERROR: Base table or view already exists: 1050 Table ... already exists
相信有更新magento或者,备份转移magento站点的时候可能会碰到类似这样的错误提示: Base table or view already exists: 1050 Table ... alr ...
- --hdu 1050 Moving Tables(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1050 AC code: #include<stdio.h> #include<str ...
- hdu 1050 Moving Tables 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1050 这道题目隔了很久才做出来的.一开始把判断走廊有重叠的算法都想错了.以为重叠只要满足,下一次mov ...
- NBOJv2 1050 Just Go(线段树/树状数组区间更新单点查询)
Problem 1050: Just Go Time Limits: 3000 MS Memory Limits: 65536 KB 64-bit interger IO format: % ...
- PAT 解题报告 1050. String Subtraction (20)
1050. String Subtraction (20) Given two strings S1 and S2, S = S1 - S2 is defined to be the remainin ...
- BZOJ 1050 旅行comf(枚举最小边-并查集)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1050 题意:给出一个带权图.求一条s到t的路径使得这条路径上最大最小边的比值最小? 思路 ...
- hiho #1050 : 树中的最长路 树的直径
#1050 : 树中的最长路 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中, ...
- 51nod 1050 循环数组最大子段和
题目链接:51nod 1050 循环数组最大子段和 #include<stdio.h> #include<algorithm> using namespace std; ; l ...
随机推荐
- dubbo中Listener的实现
这里继续dubbo的源码旅程,在过程中学习它的设计和技巧,看优秀的代码,我想对我们日程编码必然有帮助的.而那些开源的代码正是千锤百炼的东西,希望和各位共勉. 拿ProtocolListenerWrap ...
- BZOJ 3907: 网格 [Catalan数 高精度]
3907: 网格 Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 402 Solved: 180[Submit][Status][Discuss] De ...
- Ubuntu 配置FTP服务器
第三方的文件传输软件用着很不爽,想着自己搭建一个FTP来干活. 首先检查是否已经安装了FTP,输入命令: vsftpd -v 可以查看版本,如果没有安装,无法执行. 安装FTP p.p1 { mar ...
- Java中的双重检查锁(double checked locking)
最初的代码 在最近的项目中,写出了这样的一段代码 private static SomeClass instance; public SomeClass getInstance() { if (nul ...
- PyCharm安装Pygame方法
最近在自学Python,然后终于到了项目实战的时候了,而且还是做一个游戏,热情直接被调动起来了,嘿嘿 首先要安装一个Pygame 环境 win7 先去 这里 下载对应Python的Pygame版本 如 ...
- POJ - 1321 dfs [kuangbin带你飞]专题一
枚举行和列即可,当前已经放下cnt个棋子,当前已经搜索到第r行,如果 n - r + cnt < k 直接退出,因为后面无法放下剩下的棋子. AC代码 #include<cstdio&g ...
- C语言拼接字符串以及进制转换
#include<stdio.h> #include<stdlib.h> #include<string.h> char *join1(char *, char*) ...
- eclipse hadoop1.2.0配置及wordcount运行
"error: failure to login"问题 http://www.cnblogs.com/xia520pi/archive/2012/05/20/2510723.htm ...
- Django使用模板后无法找到静态资源文件
Django使用模板后无法找到静态资源文件 环境配置 Django版本1.11 python版本3.6.2 前言 在编写Django网站的时候,在涉及模板方面,一些简单的例子都没有问题,但这些例子都有 ...
- Http协议规范及格式
HTTP(HyperTextTransferProtocol)是超文本传输协议的缩写,它用于传送WWW方式的数据,关于HTTP协议的详细内容请参考RFC2616.HTTP协议采用了请求/响应模型.客户 ...