ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

关于题目中遇到的一些坑

2022-07-14 13:34:07  阅读:128  来源: 互联网

标签:题目 show int s1 遇到 len num 关于 include


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 大小排序.

标签:题目,show,int,s1,遇到,len,num,关于,include
来源: https://www.cnblogs.com/preccrep/p/16445134.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有