首页 > 其他分享 >寒假训练——vj题解

寒假训练——vj题解

时间:2024-02-18 11:26:42浏览次数:23  
标签:int 题解 vj cin ++ 寒假 include sum dp

  • B - B
    M 算日期
    M 是一位数学高手,今天他迎来了 Kita 的挑战。Kita 想让 BM 算出这几年内有多少个闰年。

BM 觉得这问题实在太简单了,于是 Kita 加大了难度。

他先给出第一个年份,再给出一个整数。Kita 要 BM 进行加法运算后得到第二个年份,然后算出这两个年份之间有多少个闰年。

然而,BM 对于大于 9999
的年份十分恐惧,于是他耗尽力气,不仅将年份大于 9999
的部分删除了,并且利用多余的部分使这个年份更小了。

例如,如果 Kita 给出的第一个年份为 9997
,整数为 3
,进行加法运算后得到的年份为 10000
,大于 9999
的部分为 1
年,所以得到的第二个年份为 9999−1=9998
,BM 最终得到的年份区间就是 [9997,9998]

在进行了如上操作后,BM 筋疲力尽,甚至没有力气确定自己最终的年份区间是什么。为了守护他的数学高手的称号,请您帮助他算出最终的答案。
简单的模拟

点击查看代码
#include <bits/stdc++.h>
using namespace std;
bool isLeapYear(int year) {
	if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
		return true;
	} else {
		return false;
	}
}
int main ()
{
	int n,a,b;
    int ans=0;
	int res=0;
	int sum=0;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a>>b;
	     if(b<0)
		sum=a-abs(b);
		else
		 sum=a+b;
		if(sum>=10000)
		{
			res=sum-9999;
			sum=9999-res;
			if(sum>=a)
			{
				for(int i=a;i<=sum;i++){
					if(isLeapYear(i))
						ans++;
				}
				cout<<ans<<endl;
					ans=0;
			}else
			{
			for(int i=a;i>=sum;i--){
				if(isLeapYear(i))
					ans++;	
		     	}
				cout<<ans<<endl;
					ans=0;
			}
		}else
		{
			if(sum>=a)
			{
				for(int i=a;i<=sum;i++){
					if(isLeapYear(i))
						ans++;
				}
				cout<<ans<<endl;
				ans=0;
			}
			else if(sum<a)
			{
				for(int i=a;i>=sum;i--){
					if(isLeapYear(i))
						ans++;
				}
				cout<<ans<<endl;
					ans=0;
			}
		}
		
	
	}

	return 0;
}

* E - E BM 充饥 到了年末,BM 的积蓄所剩无几,但他又很想吃 KFC,请您帮助他画 "KFC" 充饥吧! 签到题,注意对齐和\这个符号需要两次
点击查看代码
#include <bits/stdc++.h>
using namespace std;
int main ()
{
	string a;
	
	cin>>a;
	cout<<" __      _____"<<endl;
    cout<<"|  | ___/ ____\\____"<<endl;
	cout<<"|  |/ /\\   __\\/ ___\\"<<endl;
	cout<<"|    <  |  | \\  \\___"<<endl;
	cout<<"|__|_ \\ |__|  \\___  >"<<endl;
	cout<<"     \\/           \\/"<<endl;
}
* H - H sueh- Draw Progress According to scientific research, in the waiting process, if the progress bar can be used to show the progress, the waiting person will be more patient.

Therefore, Hsueh- needs you to help him draw a progress bar in command line.

The specific progress bar style can be obtained by observing the sample.
后面那个数除前面那个数,一共是前面那个数的总数“-”为第一个数-第二个数,先输出第二个数相同的#

点击查看代码
#include <bits/stdc++.h>
using namespace std;
int main ()
{
	int n;
	double sum;
	cin>>n;
	int a,b;
	for(int i=1;i<=n;i++){
		cin>>a>>b;
		sum=1.0*b/a;
		cout<<"[";
		for(int i=1;i<=b;i++){
			cout<<"#";
		}
		for(int i=1;i<=a-b;i++)
			cout<<"-";
		cout<<"]";
		cout<<" "<<(int)(sum*100)<<"%"<<endl;
	}
}
* I - I 众所周知 bm 学姐喜欢旅游。这一天他来到了著名的杭师大景点勤园 13-406。该景点中有四个著名的观景台分为 A,B,C,D。

如果只去过上述一个观景台,输出 "Oh dear!!"(没有引号)。
如果去过两个,输出 "BaoBao is good!!"(没有引号)。
如果去过三个,输出 "Bao Bao is a SupEr man///!"(没有引号)。
如果全都去过,输出 "Oh my God!!!!!!!!!!!!!!!!!!!!!"(没有引号)。
如果一个都没去过,输出 "Bao Bao is so Zhai......"(没有引号)。
依旧是模拟,考虑全部情况

点击查看代码
#include <bits/stdc++.h>
using namespace std;
int solve (int x)
{
	int sum =0 ;
	while(x!=0)
	{
		sum+=x%10;
        x=x/10;
	}
	if(sum==6||sum>=16)
		return 1;
	else 
		return 0;
}
int main ()
{
	int sum=0;
	int a,b,c,d;
    cin>> a >> b >>c >>d;
      sum+=solve(a)+solve(b)+
	solve(c)+solve(d);
	if(sum==0)
	{
		cout<<"Bao Bao is so Zhai......";
	}else if(sum==1)
	{
		cout<<"Oh dear!!";
	}else if(sum==2)
	{
		cout<<"BaoBao is good!!";
	}else if(sum==3)
	{
		cout<<"Bao Bao is a SupEr man///!";
	}else if(sum==4)
	{
		cout<<"Oh my God!!!!!!!!!!!!!!!!!!!!!";
	}
	return 0;
}
* J -J 大扫除 你是一个住在大豪宅里的富二代「聘请的老青结清洁工」。

你需要给这座高 n
层楼的豪宅清理垃圾。

已知字符 "." 表示没有垃圾,其他任何字符都表示垃圾,每种字符表示一种垃圾。

要求计算整个豪宅每层楼垃圾种类的总和。
这段代码首先读取输入的测试用例数量 T。然后,对于每个测试用例,它会读取楼层数 n,并依次读取每层楼的垃圾情况。对于每层楼,它会将垃圾字符加入到一个无序集合 trashSet 中,确保只统计每层楼的垃圾种类数。然后,将 trashSet 的大小加到 totalTrashTypes 中,并清空 trashSet。最后,输出 totalTrashTypes。

点击查看代码
#include &lt;iostream&gt;
#include &lt;unordered_set&gt;
using namespace std;

int countTrashTypes(int n) {
    int totalTrashTypes = 0;
    unordered_set&lt;char&gt; trashSet;
    for (int i = 0; i &lt; n; i++) {
        string floor;
        cin &gt;&gt; floor;
        for (char c : floor) {
            if (c != '.') {
                trashSet.insert(c);
            }
        }
        totalTrashTypes += trashSet.size();
        trashSet.clear();
    }
    return totalTrashTypes;
}

int main() {
    int T;
    cin &gt;&gt; T;
    for (int i = 0; i &lt; T; i++) {
        int n;
        cin &gt;&gt; n;
        int totalTrashTypes = countTrashTypes(n);
        cout &lt;&lt; totalTrashTypes &lt;&lt; endl;
    }
    return 0;
}

* D -D Palindrome Hard Problem 个问题可以使用动态规划来解决。我们可以定义一个二维数组dp,其中dp[i][j]表示从第i个字符串到第j个字符串能够形成的回文串的最大数量。

首先,我们可以初始化dp[i][i]为1,因为单个字符串本身就是一个回文串。

然后,我们可以遍历字符串的长度l,从2开始到字符串的长度。对于每个长度l,我们遍历字符串的起始位置i,计算dp[i][i+l-1]的值。

对于dp[i][i+l-1],我们可以考虑两种情况:

如果第i个字符和第i+l-1个字符相等,那么dp[i][i+l-1]的值可以为dp[i+1][i+l-2] + 2。这是因为如果第i+1个字符到第i+l-2个字符能够形成回文串,并且第i个字符和第i+l-1个字符相等,那么就可以将这两个字符加入到回文串中,所以回文串的长度加2。

如果第i个字符和第i+l-1个字符不相等,那么dp[i][i+l-1]的值可以为max(dp[i+1][i+l-1], dp[i][i+l-2])。这是因为我们可以选择舍弃第i个字符或者舍弃第i+l-1个字符,来使得回文串的数量最大化。

最后,dp[0][n-1]就是我们所求的终极串能够分割出的回文串的最大数量。

点击查看代码
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int countPalindromes(int n, vector<string>& strings) {
    int m = strings[0].length();
    vector<vector<int>> dp(n, vector<int>(n, 0));

    for (int i = 0; i < n; i++) {
        dp[i][i] = 1;
    }

    for (int l = 2; l <= m; l++) {
        for (int i = 0; i < n - l + 1; i++) {
            int j = i + l - 1;
            if (strings[i] == strings[j]) {
                dp[i][j] = dp[i + 1][j - 1] + 2;
            } else {
                dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);
            }
        }
    }

    return dp[0][n - 1];
}

int main() {
    int n;
    cin >> n;
    vector<string> strings(n);
    for (int i = 0; i < n; i++) {
        cin >> strings[i];
    }

    int result = countPalindromes(n, strings);
    cout << result << endl;

    return 0;
}

* K - K words Find Kth Element Hello, guys. I am Kwords. I prepared a problem for the contest invited by Hsueh-. Due to I was very boring, I generate n arrays, labeled from 1 to n . And then I will give q queries.

In each queries:

First, I will give a interger m
, and then m
different intergers.
You need to merge the arrays pointed to by these m
labels which I given.
Then, I will give a interger k
.
You need to answer me, what is the k
-th
smallest number in the combined array.
It is attention that, each query is independent. It means that the array merging operation of a single query will not affect other queries.

点击查看代码
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    int n;
    cin >> n;

    vector<vector<int>> arrays(n);
    for (int i = 0; i < n; i++) {
        int m;
        cin >> m;
        arrays[i].resize(m);
        for (int j = 0; j < m; j++) {
            cin >> arrays[i][j];
        }
    }

    int q;
    cin >> q;

    for (int i = 0; i < q; i++) {
        int querySize;
        cin >> querySize;

        vector<int> indices(querySize);
        for (int j = 0; j < querySize; j++) {
            cin >> indices[j];
        }

        int k;
        cin >> k;

        vector<int> mergedArray;
        for (int j = 0; j < querySize; j++) {
            mergedArray.insert(mergedArray.end(), arrays[indices[j] - 1].begin(), arrays[indices[j] - 1].end());
        }

        sort(mergedArray.begin(), mergedArray.end());

        cout << mergedArray[k - 1] << endl;
    }

    return 0;
}

标签:int,题解,vj,cin,++,寒假,include,sum,dp
From: https://www.cnblogs.com/dontian/p/18018880

相关文章

  • ABC341G 题解
    blog。妈的,被trick干爆了。\(\textbf{Trick}\):将所有\(N_i=(i,\sum\limits_{j=1}^ia_j)\)视作一点,则区间\([l,r]\)的平均值为\((N_{l-1},N_r)\)的斜率。\(\textbf{Prove}\):由\(\text{slope}=\dfrac{y_2-y_1}{x_2-x_1}\)易证。根据这个trick,\(k\)的答案即为\(k......
  • luogu2119题解
    本题考察对于枚举的方式对程序的性能的提升。有一个小的优化,\(n\)的范围比\(m\)的范围小,由于我们不关心顺序,我们既可以在值域上枚举也可以在物品上枚举,这里为了优化在值域上枚举更好。最简单的枚举是直接枚举\(a,b,c,d\)或是枚举其中三个数枚举另一个,时间复杂度为\(O(n^4)......
  • P10171题解
    P10171[DTCPC2024]取模题目传送门题解不会多项式导致的,赛后秒过。一个显然的结论:如果原序列有相等的数答案为\(0\),其次大于\(4\times10^5\)的\(k\)均符合要求。问题在于小于\(4\times10^5\)的答案。赛时想了很多奇妙的算法,诸如根号分治、线段树维护余数等等。其......
  • CF1472C Long Jumps题解
    【题目分析】本题有两个方法,方法一:每一个位置可得的分分别求出,打擂找出最大(可得部分分)方法二:从后往前求可得的分数,以避免一些不必要的重复。【设计程序】方法一:#include<bits/stdc++.h>#include<iostream>#include<stdio.h>#include<cstdio>#include<queue>usingnames......
  • CF1196B Odd Sum Segments题解
    【问题分析】本题考了奇数。由此想到以下定律:奇数+偶数=奇数;奇数+奇数=偶数;偶数+偶数=偶数;所以偶数可以忽略不计,只有奇数可以对结果产生影响,所以我们只要注意奇数即可。经过思考可得奇数的个数至少为$k$个且比$k$多的个数为偶数,此时多出的奇数可组成偶数,对结果不产生......
  • P2036 [COCI2008-2009#2] PERKET题解
    【问题分析】分析题目可得此问题为01背包问题因此题数据较小所以可用枚举每一样物品选或不选的方法来写【设计程序】#include<bits/stdc++.h>#include<iostream>#include<stdio.h>#include<cstdio>#include<queue>usingnamespacestd;constintN=10+5;struct......
  • P1162 填涂颜色题解
    【问题分析】分析题目可得此问题为连通块问题因此题枚举被包围的‘0’较难所以可用枚举每一个不被包围的‘0’【设计程序】#include<bits/stdc++.h>#include<iostream>#include<stdio.h>#include<cstdio>#include<queue>usingnamespacestd;constintN=30+5;i......
  • P2249 【深基13.例1】查找题解
    【问题分析】本题有n个数(n>10^6)n很大,查找m个数(m≤10^5),数最大为(10^9)方法一:用顺序查找的话时间复杂度为:O(n*m)会超时,只能得部分分;方法二:用桶排时间复杂度为O(n)+O(m),但是因为数最大为(109)空间复杂度为:O(109);方法三:用二分查找,时间复杂度为:O(m*logn),空间复杂度为O(n)。综合以......
  • P8248 简单数列 题解
    首先,圈重点:$1\len\le500$所有元素在$1\sim4$之间任意连续的连续子串不相同只要输出一种答案即可于是我们可以得到的是:由第一点和第二点可以看出此题可以写搜索解决。由第三点我们可以得到一种剪枝方式,就是如果目前数字放入后会产生相同的连续的连续子串。由第四点......
  • P1380 T型骨牌 题解
    本题每个位置有$5$种可能,据题中$n,m$均小于五,所以可以用搜索直接过。上代码#include<cstdio>usingnamespacestd;boolmp[15][15];intn,m,ans;intdt[4][5][2]={{{-1,-1},{0,-1},{1,-1},{0,0},{0,1}},{{-1,0},{0,0},{1,-1},{1,0},{1,1}},{{0,......