2.10~2.16

牛客周赛80

A

题目

签到

代码

#include <bits/stdc++.h>
using namespace std;

int main(void){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int x,y;
	cin >> x >> y;
	if(x >= y) cout << x-y << '\n';
	else cout << "quit the competition!\n";
	return 0;
}

B

题目

降序排序,相邻两项之间相减,取绝对值,求和即可

代码

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+1;
bool cpa(int a,int b){
	return a>b;
}

int arr[maxn];

int main(void){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int n;
	cin >> n;
	n*=2;
	for(int i=0;i<n;i++)
		cin >> arr[i];
	sort(arr,arr+n,cpa);
	int sum=0;
	for(int i=0;i+2<=n;i+=2)
		sum += abs(arr[i] - arr[i+1]);
	cout << sum << '\n';
	return 0;
}

C

题目

模拟,在每个回合判断num0和num1的数量,如果0第一次>1那么ka++,0--,1++,在此刻之前的所以0都可以举手;以后0>1就不行了。如果没有0>1的情况,那么所有回合都可以举手,为n

代码

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+1;

int main(void){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int n;
	string s;
	cin >> n >> s;
	int num1=0,num0=0,ka=0,pd=1,sum=0,temp=0;
	for(int i=0;i<n;i++){
		if(s[i] == '0') num0++;
		else num1++;
		if(num0 > num1){
			ka++;
			temp = num0;
			num1++;
			num0--;
		}
		if(ka > 1){
			pd=0;
			cout << "0\n";
			break;
		}
	}
	if(pd) cout << (ka==1 ? temp:n) << '\n';
	return 0;
}

D

题目

还是如C题一样,temp1记录ka1。而temp2用来记录ka2的情况。如果两次举手不同,则是temp1*temp2减去重复的部分

代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 1e5+1;

signed main(void){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int n;
	string s;
	cin >> n >> s;
	int num1=0,num0=0,ka=0,pd=1,sum=0,temp1=0,temp2=0;
	for(int i=0;i<n;i++){
		if(s[i] == '0') num0++;
		else num1++;
		if(num0 == num1+1){
			if(ka==0){
				ka++;
				temp1 = num0;
				num1++;
				num0--;
			}
			else if(ka == 1){
				ka++;
				temp2 = num0;
				num0--;
				num1++;
			}
			else{
				pd=0;
				cout << "0\n";
				break;
			}
		}
	}
	if(pd){
		if(temp1==0) temp1 = n;
		if(temp2==0) temp2 = n-1;
		cout << temp1*temp2 - temp1*(temp1-1)/2 << '\n';
	}
	return 0;
}

牛客月赛110

A

题目

此题唯一注意的是,特判:当n是500的整数倍时,res调为500,index--

代码

#include <bits/stdc++.h>
using namespace std;

int main(void){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int n;
	cin >> n;
	string s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	int res = n % 500, index = n / 500;
	if(res == 0){
		res=500;
		index--;
	}
	if(res<10)
		cout << s[index] << "00" << res << '\n';
	else if(res<100)
		cout << s[index] << "0" << res << '\n';
	else cout << s[index] << res << '\n';
	return 0;
}

B

题目

字符串比较,按如题所述方式比较即可,签到

代码

#include <bits/stdc++.h>
using namespace std;

int main(void){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	string s1,s;
	cin >> s1 >> s;
	map <char,int> ma;
	int num=0; 
	for(int i=0;i<8;i++)
		ma[s1[i]]++;
	for(int i=0;i<8;i++){
		if(s[i] == s1[i]){
			cout << "g";
			num++;
		}
		else if(ma[s[i]] != 0) cout << "y";
		else cout << "r";
	}
	cout << (num==8 ? "\ncongratulations\n" : "\ndefeat\n");
	return 0;
}

C

题目

把智数升序排列起来就会发现,7个智数为一个周期,因此输出k后,k/7*30算出前半部分,arr[k%7]是后半部分,相加即可

注意arr里的第一个数需要是-3

代码

#include <bits/stdc++.h>
#define int long long 
using namespace std;

signed main(void){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int t;
	cin >> t;
	while(t--){
		int k;
		cin >> k;
		int arr[8] = {-3,3,5,9,15,21,25,27};
		int ans = k/7*30 + arr[k%7];
		cout << ans << '\n';
	}
	return 0;
}

牛客寒假训练营

A

题目

贪心,要使得原始a数组包含元素最少,就要尽可能使复制的元素更多,所以把每个重复的元素都进行缩减,就能求出包含元素最少的原始a数组

代码

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+1;

int arr[maxn];

int main(void){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int t;
	cin >> t;
	while(t--){
		int sum=1,n;
		cin >> n;
		memset(arr,0,sizeof(arr));
		for(int i=0;i<n;i++)
			cin >> arr[i];
		int index = arr[0];
		for(int i=1;i<n;i++){
			if(arr[i] != index){
				sum++;
				index = arr[i];
			}
		}
		cout << sum << '\n';
	}
	return 0;
}

K

题目

由于n+n+1 = 2*n+1一定是奇数,所以y是偶数直接输出NO

然后判定abs(y/2-x) % 2 == 0,满足就是YES,否则NO

代码

#include <bits/stdc++.h>
#define int long long
using namespace std;

signed main(void){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int t;
	cin >> t;
	while(t--){
		int x,y;
		cin >> x >> y;
		if(y%2==0){
			cout << "NO\n";
			continue;
		}
		if(abs(y/2-x) % 2 == 0) cout << "YES\n";
		else cout << "NO\n";
	}
	return 0;
}

L

题目

先判断输入的字符串中是否含有”CHICKEN“,然后判断出现次数最多的那个字符是否小于等于(n-7)/2,是则YES,否则NO

代码

#include <bits/stdc++.h>
using namespace std;

map <char,int> ma;

int main(void){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	string c = "CHICKEN";
	int t;
	cin >> t;
	while(t--){
		int n;
		string s;
		cin >> n >> s;
		if(n%2==0 || n < 7){
			cout << "NO\n";
			continue;
		}
		int index=0;
		for(int i=0;i<n;i++){
			if(s[i] == c[index] && index < 7)
				index++;
			else ma[s[i]]++;
		}
		int maxn=0;
		for(auto i:ma)
			if(i.second > maxn)
				maxn = i.second;
		if(maxn*2 <= n-7 && index == 7) cout << "YES\n";
		else cout << "NO\n";
		ma.clear(); 
	}
	return 0;
}

C

题目

结论是:除了形如 2^p(p>1) 的整数不能表示,其他数字都可以表示。

因此,本题相当于要找第 k个非2^p(p>1)形式的偶数

也就是pow(2,i) < k+i

代码

#include <bits/stdc++.h>
#define int long long
using namespace std;

signed main(void){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int t;
	cin >> t;
	while(t--){
		int k;
		cin >> k;
		int i;
		for(i=60;i>0;i--)
			if(pow(2,i) < k+i)
				break;
		int num = (k+i)*2;
		cout << num << '\n';
	}
	return 0;
}

洛谷

P1036 选数

题目

深搜+素数筛

埃氏筛筛出5e7内的素数,然后dfs搜n个数中选k个数的所有情况,并判断k个数的和是否为素数

代码

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e7+5;

int n,k,arr[21],ans=0;
bool shai[maxn];

void sushu(){
	for(int i=2;i*i<=maxn;i++)
		if(!shai[i])
			for(int j=i*i;j<=maxn;j+=i)
				shai[j] = true;
}

void dfs(int m,int sum,int start){
	if(m == k){
		if(!shai[sum])
			ans++;
		return;
	}
	for(int i=start;i<n;i++)
		dfs(m+1,sum+arr[i],i+1);
	return;
}

int main(void){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n >> k;
	for(int i=0;i<n;i++) cin >> arr[i];
	sushu();
	dfs(0,0,0);
	cout << ans << '\n';
	return 0;
}

P1332 血色先锋队

题目

bfs板子,很多题目都有类似于本体中”感染源“的事物,比如火灾蔓延,龙之吐息等等,用bfs搜。注意领主的数据开大一些,n*m而非n

代码

#include <bits/stdc++.h>
using namespace std;

int n,m,a,b;
int room[501][501],lz[501*501][2];
int explore[][2] = {0,1,0,-1,1,0,-1,0};
bool vis[501][501];

struct node{
	int x,y,steps;
};
queue <node> q;

void jindui(int x1,int y1){
	node temp;
	temp.x = x1;
	temp.y = y1;
	temp.steps = 0;
	q.push(temp);
	vis[x1][y1] = true;
	room[x1][y1] = 0;
	return;
}

void bfs(){
	while(!q.empty()){
		node start,next;
		start = q.front();
		for(int i=0;i<4;i++){
			next.x = start.x + explore[i][0];
			next.y = start.y + explore[i][1];
			if(next.x>=1 && next.x<=n && next.y>=1 && next.y<=m && !vis[next.x][next.y]){
				vis[next.x][next.y] = true;
				next.steps = start.steps + 1;
				q.push(next);
				room[next.x][next.y] = next.steps;
			}
		}
		q.pop();
	}
	return;
}

int main(void){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n >> m >> a >> b;
	for(int i=1;i<=a;i++){
		int x,y;
		cin >> x >> y;
		jindui(x,y);
	}
	bfs();
	for(int i=1;i<=b;i++){
		cin >> lz[i][0] >> lz[i][1];
		cout << room[lz[i][0]][lz[i][1]] << '\n';
	}
	return 0;
}
posted @ 2025-06-14 12:22  HLAIA  阅读(7)  评论(0)    收藏  举报