Logo aaa 的博客

博客

标签
暂无

T4Solution

本文章由 WyOJ Shojo 从洛谷专栏拉取,原发布时间为 2022-08-29 14:09:42

为了方便我们规定 0 一开始就被传染,第 $i$ 个人 $a_i$ 分钟出来等于第 $i$ 个人在第 $a_i$ 分钟与 0 接触。

我们可以从 $n$ 点出发,求一条非递增路径,一直到 0 点,我们要求其中最大的边权也就是经过的第一条边最小,当然,可能有环,因此我们设 $f_i$ 表示 $i$ 所经过的最小的最大的路径也就是一开始在第 $n$ 个点经过的边后到达这个点后让这条边最小的值,只要判断 $f_i$ 大于经过的第一条边时才访问第 $i$ 个点,从第 $n$ 个点dfs 开始就可以了。

UVA13221

本文章由 WyOJ Shojo 从洛谷专栏拉取,原发布时间为 2021-04-04 12:39:39

1 1
2 3
3 7
4 11
5 17
6 23
7 31

规律不难发现,递推式为:
$\frac{(n+1)^2}{2}-1$

最后献上我的代码:

#include<bits\/stdc++.h>
using namespace std;
typedef long long ll;
int main () {
	ll n;
	while(cin>>n) {
		ll p=(n+1)*(n+1)\/2-1;
		cout<<p;
		cout<<endl;
	}
	return 0;
}

CF381A

本文章由 WyOJ Shojo 从洛谷专栏拉取,原发布时间为 2021-04-08 14:59:38

思路

deque是一个好东西。

输入数据用双向队列存储。用一个 while 循环。如果现在是 Sereja 的环节,如果队列的头比尾大,Sereja的牌就加上队列的头,否则加队列的尾。反之,一样。

代码

#include <iostream>
#include <cstdio>
#include <queue>
#include <deque>
using namespace std;

int main()
{
	bool q=0;
	int Sereja=0,Dima=0;
	int n;
	deque<int> a;
	cin>>n;
	for(int i=1;i<=n;++i){
		int t;
		cin>>t;
		a.push_back(t);
	}
	while(!a.empty()){
		if(q==0){
			if(a.front()>a.back()){
				Sereja+=a.front();
				a.pop_front();
				q=1;
				continue;
			}
			Sereja+=a.back();
			a.pop_back();
			q=1;
		}
		else{
			if(a.front()>a.back()){
				Dima+=a.front();
				a.pop_front();
				q=0;
				continue;
			}
			Dima+=a.back();
			a.pop_back();
			q=0;
		}
	}
	cout<<Sereja<<' '<<Dima;
	return 0;
} 

UVA12895

本文章由 WyOJ Shojo 从洛谷专栏拉取,原发布时间为 2021-04-08 16:17:07

思路

输入 $n$ 之后,先求出他的位数,接着让变量 $sum$ 加上 $n$ 每一位的位数方,最后判断一下就可以了。

代码

#include <iostream>
#include <cmath>
#include <cstring> 
#include <queue>
#include <algorithm>
using namespace std;

int main(){
	int t;
	cin>>t;
	while(t--){
		long long n,sum=0,q,len=0;
		cin>>n;
		q=n;
		while(q){\/\/判断位数
			++len;
			q\/=10;
		}
		q=n;
		while(q){\/\/加上每位的位数方
			sum+=(long long)(pow(q%10,len));
			q\/=10;
		}
		cout<<(sum==n?"Armstrong":"Not Armstrong")<<endl;	
	}
	return 0;

十年OI一场空,不开 long long 见祖宗

CF523A

本文章由 WyOJ Shojo 从洛谷专栏拉取,原发布时间为 2021-04-11 11:16:04

思路

输入 $w$ 和 $h$,然后要反着输入矩形,最后 2 倍反着输入矩形就完成了。

代码

#include <iostream>
#include <cstdio>
#include <string>
#include <queue>
#include <algorithm>
using namespace std;

int main() {
	char c[105][105];
	int w,h;
	cin>>w>>h;
	for(int i=1; i<=h; ++i)
		for(int j=1; j<=w; ++j)
			cin>>c[i][j];
	for(int i=1; i<=w; ++i) {
		for(int p=1; p<=2; ++p) {
			for(int j=1; j<=h; ++j) {
				cout<<c[j][i]<<c[j][i];
			}
			cout<<endl;
		}
	}
	return 0;
}

CF721A

本文章由 WyOJ Shojo 从洛谷专栏拉取,原发布时间为 2021-04-11 11:28:15

思路

输入完之后,再次判断只要 $c_{i}$ 是 'B',就 ++ $a_{ans}$,如果 c[i+1]!=b++ans,最后输出一下就好了。

代码

#include <iostream>
#include <cstdio>
#include <string>
#include <queue>
#include <algorithm>
using namespace std;

int main() {
	int n,a[105]={0},ans=0;
	char c[105];
	cin>>n;
	for(int i=1;i<=n;++i)
		cin>>c[i];
	for(int i=1;i<=n;++i){
		if(c[i]=='B'){
			++a[ans];
			if(c[i+1]!='B')
				++ans;
		}
	}
	cout<<ans<<endl;
	for(int i=0;i<ans;++i)
		cout<<a[i]<<' ';
	return 0;
}

题解

本文章由 WyOJ Shojo 从洛谷专栏拉取,原发布时间为 2021-04-25 15:39:48

题目大意

输入 $n$,之后给出 $n$ 组数据,每组数据在给出 $m$,接下来 $m$ 行,每行输入 $a$、$b$和$c$,最后输出每次第一个数乘上第三个数的和。

思路

定义变量 $sum$,输入完之后每次加上第一个数乘第二个数,最后输出就行了。

最后贴上代码:

#include <iostream>
#include <cstdio>
using namespace std;

int main(){
	int n;
	cin>>n;
	while(n--){
		long long sum=0;
		int m;
		cin>>m;
		while(m--){
			int a,b,c;
			cin>>a>>b>>c;
			sum+=a*c;
		}
		cout<<sum<<endl;
	}	
	return 0;
}

十年OI一场空,不开 long long 见祖宗

题解A2202

本文章由 WyOJ Shojo 从洛谷专栏拉取,原发布时间为 2021-04-28 16:14:09

思路

定义一个队列 $s$,之后每次输入都进队列中。输入完循环只要队列 $s$ 里面不是只有一个元素,就用 $p1$ 和 $p2$ 代表开头的两个数,之后按照题意插入就可以了。

最后献上我的代码:

#include <iostream>
#include <queue>
#include <cstdlib>
using namespace std;

int main(){
	queue<int> s;
	int n;
	cin>>n;
	for(int i=1;i<=1<<n;++i){
		int t;
		cin>>t;
		s.push(t);
	}
	while(s.size()!=1){
		int p1=s.front();
		s.pop();
		int p2=s.front();
		s.pop();
		if(p1>p2)
			s.push(p1-p2);
		if(p1==p2)
			s.push(p1);
		if(p1<p2)
			s.push(p2-p1);
	}
	cout<<s.front();
	return 0;
}

UVA12279

本文章由 WyOJ Shojo 从洛谷专栏拉取,原发布时间为 2021-05-07 15:42:56

题目大意

不停输入 $n$,只要 $n$ 不为0,就输入 $n$ 个整数,之后输出这些数字中不是 0 的个数减去是 0 的个数就好了。

思路

每次都用一个变量 $a$ 输入,如果$a$ 是 0,就让存储 0 的个数的变量加一,反之,就让存储不是 0 的个数的变量加一,最后输出它们相减就好了。

代码

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;

int main(){
	int n,p=0;
	while(cin>>n&&n){
		int a;
		++p;
		int ans=0,sum=0;
		for(int i=1;i<=n;++i)
		{
			cin>>a;
			if(a)
				++ans;
			else
				++sum;
		}
		cout<<"Case "<<p<<": "<<ans-sum<<endl;
	}
	return 0;
}

UVA12503

本文章由 WyOJ Shojo 从洛谷专栏拉取,原发布时间为 2021-05-07 16:00:53

思路

每次输入玩 $n$ 之后,$n$ 次循环,判断一下 $s[0]$ 。如果是 L,就让存储位置的变量减一,$a_{i}$ 等于 -1;如果是 R,就让存储位置的变量加1,$a_{i}$ 等于 1;如果是 S,就先把后面的 AS 读进来,然后再读一个变量 $p$,让存储位置的变量加上 $a_{p}$,$a_{i}$ 等于 $a_{p}$。

代码

#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
using namespace std;

int main(){
	int t;
	cin>>t;
	while(t--){
		int sum=0,a[105],n,p;
		cin>>n;
		for(int i=1;i<=n;++i){
			string s;
			cin>>s;
			switch(s[0]){
				case 'L':{
					--sum;
					a[i]=-1;
					break;
				}
				case 'R':{
					++sum;
					a[i]=1;
					break;
				}
				case 'S':{
					cin>>s;
					cin>>p;
					sum+=a[p];
					a[i]=a[p];
					break;
				}
			}
		}
		cout<<sum<<endl;
	}
	return 0;
}
共 45 篇博客