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;
}