关于题目中遇到的一些坑

1085 Perfect Sequence

Link
这道题,别想着用 lower_boundupper_bound 了。如果有相同元素,那么 len 很可能就算少了。
另外,p 要用 long long 类型,不然最后一个测试用例过不了。
不过这题用 lower_boundupper_bound 亲测可过,但是要记住它们返回的是一连串相等的数中的第一个

都是利用二分查找在排好序的数组里找。
lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

做法就贴一部分了,不过最好别这么做:

for(int i=0;i<n;++i){
	ll cur=a[i]*p;
	int pos=upper_bound(a,a+n,cur)-a;
	if(pos>=n){
		len=max(len,n-i);
		break;
	}
	len=max(len,pos-i);
}

毫无疑问的AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
using namespace std;
typedef long long ll;
int n,len;
ll p;
int a[100010];
int main() {
	scanf("%d%lld",&n,&p);
	for(int i=0;i<n;++i)
		scanf("%d",&a[i]);
	sort(a,a+n);
	for(int i=0;i<n;++i){
		ll cur=a[i]*p;
		for(int j=i+len;j<n;++j){
			if(a[j]<=cur) len=max(len,j-i+1);
			else break;
		}
	}
	printf("%d\n",len);
	return 0;
}

1084 Broken Keyboard

Link
这道题的坑在于,type出来的字符串遍历完之后,原字符串剩下的就全都是坏掉的。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
#include <unordered_map>
using namespace std;
string s1,s2;
bool v[40];
int main() {
	cin>>s1>>s2;
	int i=0,j=0;
	while(i<s1.size()){
		if(j<s2.size()&&s1[i]==s2[j]){i++;j++;}
		else{
			if((s1[i]>='A'&&s1[i]<='Z')||(s1[i]>='a'&&s1[i]<='z')){
				int ch;
				if(s1[i]>='a') ch=s1[i]-'a';
				else ch=s1[i]-'A';
				if(!v[ch+10]){
					printf("%c",ch+'A');
					v[ch+10]=true;
				}
			}else if(s1[i]=='_'){
				if(!v[36]){
					printf("_");
					v[36]=true;
				}
			}else{
				if(!v[s1[i]-'0']){
					printf("%c",s1[i]);
					v[s1[i]-'0']=true;
				}
			}
			i++;
		}
	}
	printf("\n");
	return 0;
}

1070 Mooncake

Link
注意这道题的库存量 num 应该浮点存储, 不然测试点 3 过不了.

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
using namespace std;
int n,d,cnt;
double ans;
struct node{
	// 就是这里
	// int num;
	double num,p,u;
	bool operator<(node x)const{
		return u>x.u;
	}
}price[1010];
int main() {
	scanf("%d%d",&n,&d);
	for(int i=1;i<=n;++i)
		scanf("%lf",&price[i].num);
	for(int i=1;i<=n;++i){
		scanf("%lf",&price[i].p);
		price[i].u=price[i].p/price[i].num;
	}
	sort(price+1,price+1+n);
	for(int i=1;i<=n;++i){
		if(cnt+price[i].num<=d){
			ans+=price[i].p;
			cnt+=price[i].num;
		}else{
			ans+=price[i].u*(d-cnt);
			break;
		}
	}
	printf("%.2lf\n",ans);
	return 0;
}

1071 Speech Patterns

Link
代码就不放了, 注意这个测试点:

a

输出应该是 a 0, 如果不是请看看自己是不是没有加上最后的 word (做一做该题就懂我的意思了).
再就是注意 <cctype><ctype.h> 中有 isalnum 可以用来判断 alphanumerical ([0-9 A-Z a-z]) 的字符, 还有 tolowertoupper 函数.

1075 PAT Judge

Link
测试点 4 可能过不了, 因为排序的问题:
show=0 来表示是否显示成绩, 但是 show=1 的时候也有可能总分为0. 在排序时会将 show=0 的和 show=1 的考生一起排序, 当 show=0 的考生的 id 小于 show=1 的考生时, 由于总分一样为 0 且不可能有满分题, 这就导致 show=0 的考生排在 show=1 的考生前面. 而最后输出的时候如果直接根据 show=0 break 掉, 就不会输出后面 show=1 的考生.
解决方法就是, 在自定义排序的时候除了考虑总分、满分题数量之外, 还要考虑 show 的值, 最后才按 id 大小排序.

1074 Reversing Linked List

Link
测试点 6 过不了的原因:
给出的 n 个节点并不一定都在链表中, 因此需要重新计算 n.

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
#include <stack>
using namespace std;
int n,k,head,m,p;
struct node{
	int id,data,next;
}nodeList[100010];
int main() {
	scanf("%d%d%d",&head,&n,&k);
	for(int i=0;i<n;++i){
		int id,data,next;
		scanf("%d%d%d",&id,&data,&next);
		nodeList[id]={id,data,next};
	}
	p=head;
	n=0;
	while(p!=-1){
		p=nodeList[p].next;
		n++;
	}
	m=n/k;
	p=head;
	for(int i=0;i<m;++i){
		stack<node>st;
		for(int j=0;j<k;++j){
			//i*k+j
			st.push(nodeList[p]);
			p=nodeList[p].next;
		}
		if(i==0){
			node top=st.top();
			st.pop();
			printf("%05d %d ",top.id,top.data);
		}
		while(!st.empty()){
			node top=st.top();
			st.pop();
			printf("%05d\n%05d %d ",top.id,top.id,top.data);
		}
	}
	while(p!=-1){
		printf("%05d\n%05d %d ",nodeList[p].id,nodeList[p].id,nodeList[p].data);
		p=nodeList[p].next;
	}
	printf("-1\n");
	return 0;
}

1061 Dating

Link
这题简单...但是题目很容易理解错意思. 再就是注意第一个循环内并不是 isupper 就可以解决的, 要在 [A-G] 中才行.
强烈建议重做一遍 (小声地对自己说 😃
代码就不放了.

posted @ 2022-07-18 23:23  Ryomk  阅读(26)  评论(0编辑  收藏  举报