第二周周赛

奇奇怪怪的错

链接: link.
C题的输出格式需要注意一下,不然就会Runtime error,就让第一次输出直接输出值就行了, 以后都是换行后再输出值,
如下

        k++;
		if(k==1)
			cout<<max;
		else
			cout<<endl<<max;

链接: link.
D题
给你n个整数,请按从大到小的顺序输出其中前m大的数。

Input
每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000), 第二行包含n个各不相同,且都处于区间[-500000,500000]的整数。

Output
对每组测试数据按从大到小的顺序输出前m大的数。

Sample Input
5 3
3 -35 92 213 -644

Sample Output
213 92 3

Hint
请用VC/VC++提交

看见加粗的部分没,意思是测试数据有好几个,不是只有一个,得用上循环
n是10^6级别,如果用冒泡排序(两个for循环),
(10^6 * 106=1012) 时间复杂度为O(n^2)肯定不行,考虑到快排时间复杂度为O(n*logn) logn最大为30 ,即n不超过2^30, 为了效率更高一点,可以将输入输出全为 scanf , printf

#include<bits/stdc++.h>
using namespace std;
 
const int N=1e6+10;

//map<string,int> m;
bool cmp(int a,int b)
{
	return a>b;
}
int main()
{	

	int n,m;
	while(~scanf("%d %d",&n,&m))
	{
		int a[n];
		for(int i=0;i<n;i++) scanf("%d",&a[i]);
		sort(a,a+n,cmp);
		for(int i=0;i<m;i++)
		{
			if(i==m-1)
				printf("%d\n",a[i]);
			else
				printf("%d ",a[i]);		
		}	
	}

	return 0;
}


链接: link.
A题,二进制位和这道题,注意加粗的部分,10^5 代表的是字符串的长度,不是整型数据,就更别说开个数组 10^5 了,根本不可能!!区别一下,长长记性所以直接对字符串上的每个字符进行处理就行了,会用到ASCII码
In the 2022 year, Mike found two binary integers a and b of length n (both of them are written only by digits 0 and 1) that can have leading zeroes. In order not to forget them, he wanted to construct integer d in the following way:

he creates an integer c as a result of bitwise summing of a and b without transferring carry, so c may have one or more 2-s. For example, the result of bitwise summing of 0110 and 1101 is 1211 or the sum of 011000 and 011000 is 022000;
after that Mike replaces equal consecutive digits in c by one digit, thus getting d. In the cases above after this operation, 1211 becomes 121 and 022000 becomes 020 (so, d won’t have equal consecutive digits).
Unfortunately, Mike lost integer a before he could calculate d himself. Now, to cheer him up, you want to find any binary integer a of length n such that d will be maximum possible as integer.

Maximum possible as integer means that 102>21, 012<101, 021=21 and so on.

Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases.

The first line of each test case contains the integer n (1≤n≤105) — the length of a and b

The second line of each test case contains binary integer b of length n. The integer b consists only of digits 0 and 1.

It is guaranteed that the total sum of n over all t test cases doesn’t exceed 105.

Output
For each test case output one binary integer a of length n. Note, that a or b may have leading zeroes but must have the same length n.

Example
Input
5
1
0
3
011
3
110
6
111000
6
001011
Output
1
110
100
101101
101110
Note
In the first test case, b=0 and choosing a=1 gives d=1 as a result.

In the second test case, b=011 so:

if you choose a=000, c will be equal to 011, so d=01;
if you choose a=111, c will be equal to 122, so d=12;
if you choose a=010, you’ll get d=021.
If you select a=110, you’ll get d=121.
We can show that answer a=110 is optimal and d=121 is maximum possible.
In the third test case, b=110. If you choose a=100, you’ll get d=210 and it’s the maximum possible d.

In the fourth test case, b=111000. If you choose a=101101, you’ll get d=212101 and it’s maximum possible d.

In the fifth test case, b=001011. If you choose a=101110, you’ll get d=102121 and it’s maximum possible d.

int main()
{	
	int n,t;
	cin>>t;
	while(t--)
	{
		cin>>n>>str;
		int i=0,a[n];
		int up;
		a[0]=1;
		up=str[0]-'0'+a[0];
		for(int i=1;i<n;i++){
			int t=str[i]-'0'+1; 
			if(t!=up) a[i]=1;
			else  a[i]=0;
			up=str[i]-'0'+a[i];
		}
		for(int i=0;i<n;i++) cout<<a[i];
		cout<<endl;
	}
	return 0;
}

题解

今天晚上无聊写起了博客,忽然想感慨一下今个周赛的A题,就一个技巧,会了就超级简单,当时也是虽然见过这个题技巧,也用过,不过好久不用就生了,不如记录一下,以防下次再出错
方法:把字符串每位上的数字字符类型转换为整型数字类型,用到ASCII码,比如说:‘1’–>1 , 过程:用字符‘1’的ASCII码减去‘0’的ASCII 码
如下:
链接: link.

	int n,t;
	cin>>t;
	while(t--)
	{
		cin>>n>>str;
		int i=0,a[n];
		int up;
		a[0]=1;
		up=str[0]-'0'+a[0];
		for(int i=1;i<n;i++){
			int t=str[i]-'0'+1; 
			if(t!=up) a[i]=1;
			else  a[i]=0;
			up=str[i]-'0'+a[i];
		}
	}

链接: link.
这道题是个排序题,把最小的两个数拿出来就好了,

map 容器 技巧
链接: link.
对 map<string,int> 类型容器里面的数进行排序,但是找最值还是可以,就是边统计每个string的数目,边比较找出最值,如下

	int n,k=0;
	while(cin>>n)
	{
		m.clear();
		string max,s;
		int max_ans=0;
		for(int i=0;i<n;i++){
			cin>>s;
			m[s]++;
			if(m[s]>max_ans){
				max=s;
				max_ans=m[s];	
			}
		}
	}

说到 map 我不得不谈一谈里面的一些用法,比如可以写成 map[a[i]] 或者 map["!"+a[i]]

map 和 set 容器上,find() 函数,不过 find() 函数的功能可以手写,比如 map 写成 map[ a[i] ] 是否大于 0即:map[a[i]]>0 来判断 a[i] 在容器里面是否存在,如下

    int n;
	cin>>n;
	string s[n];
	for(int i=0;i<n;i++)
	{
		cin>>s[i];
		if(m[s[i]]==0)
			m[s[i]]++;
	}
	for(int i=0;i<n;i++)
	{
		if(m[s[i]]>0 && m["!"+s[i]]>0)
		{
			cout<<s[i]<<endl;
			return 0;			
		}	
	}
	cout<<"satisfiable"<<endl;

压轴题(数学)

以后再写

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值