IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 暑假算法训练day3 -> 正文阅读

[数据结构与算法]暑假算法训练day3

C. Challenging Cliffs

题意:
在这里插入图片描述
思路:把高度差最小的放两边。分别将比 a 1 a_1 a1?大的按递增放 a 1 a_1 a1?后边,将比 a n a_n an?大的按递增放 a n a_n an?前边

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
const int N=2e5+10;
int a[N];
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	int d=ll_INF;
	int x=0,y=0;
	int idx1=0,idx2=0;
	sort(a+1,a+1+n);
	for(int i=2;i<=n;i++)
	{
		if(d>abs(a[i]-a[i-1]))
		{
			x=a[i],y=a[i-1];
			idx1=i-1,idx2=i;
			d=abs(a[i]-a[i-1]);
		}
	}
	if(x>y)swap(x,y);
	vector<int>v1,v2;
	for(int i=1;i<=n;i++)
	{
		if(i==idx1||i==idx2)continue;
		if(a[i]>=x)v1.pb(a[i]);
		else v2.pb(a[i]);
	}
	cout<<x<<' ';
	for(auto x:v1)cout<<x<<' ';
	for(auto x:v2)cout<<x<<' ';
	cout<<y<<endl;
}
signed main()
{
    io;
 	cin>>_; 
 	while(_--)
    solve();
    return 0;
}

B. Prinzessin der Verurteilung

题意:
在这里插入图片描述
思路:
字符串长度不超过1000,而 2 6 3 > 1000 26^3>1000 263>1000证明满足题意得子串长度最大为3

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
void solve()
{
	cin>>n;
	string s;
	cin>>s;
	string a="";
	queue<pair<string,int>>q;
	q.push({a,0});
	while(q.size())
	{
		auto t=q.front();
		q.pop();
		t.x.resize(t.y+1);
		for(char c='a';c<='z';c++)
		{
			t.x[t.y]=c;
			if(s.find(t.x)==-1)
			{
				cout<<t.x<<endl;
				return;
			}
			else q.push({t.x,t.y+1});
		}
	}
}
signed main()
{
    io;
 	cin>>_; 
 	while(_--)
    solve();
    return 0;
}

B1. Palindrome Game (easy version)

题意:
在这里插入图片描述
思路:
A和B足够聪明,所以A和B要尽可能在自己操作完一次后形成一个回文串。
如果零得个数为偶数只有两种情况:

  1. 平手
  2. B获胜

但是A的操作对答案无影响,所以B足够聪明,B就一定能获胜(后出手)。
如果零的个数是奇数:
A第一次放在最中间,那么剩下偶数个是B起手,那么

  1. A赢
    在这里插入图片描述

  2. B赢(只要A在第三步反转“作死”,B赢,但是两个人足够聪明,所以排除这种情况)
    在这里插入图片描述

    综上所述,如果零的个数为偶数或者1,Bwin,如果是奇数,Awin

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
void solve()
{
	cin>>n;
	string s;
	cin>>s;
	int one=0,zero=0;
	for(int i=0;i<n;i++) 
		if(s[i]=='0')zero++;
		else one++;
	if(zero==0)
	{
		cout<<"DRAW"<<endl;
		return;
	}
	else if(zero%2)
	{
		if(zero>1)cout<<"ALICE"<<endl;
		else cout<<"BOB"<<endl;
	    return;
	}
	else
	{
	    cout<<"BOB"<<endl;
	    return;
	}
}
signed main()
{
    io;
 	cin>>_; 
 	while(_--)
    solve();
    return 0;
}

C. Factorials and Powers of Two

题意:
在这里插入图片描述
思路:
d ! d! d! d = 15 d=15 d=15时已经超过题目给的范围,我们发现, 2 d 2^d 2d是2进制中1的个数,如果k要求最小,那么 2 d 2^d 2d的个数尽可能少, d ! d! d!尽可能大,考虑状态压缩,枚举剪掉的 d ! d! d!

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
const int N=21;
int f[N];
int cnt(int x)
{
	int res=0;
	while(x)
	{
		if(x&1)res++;
		x>>=1;
	}
	return res;
}
void solve()
{ 
	int x;
	cin>>x;
	int res=cnt(x);
	for(int i=0;i<1<<15;i++)
	{
		int y=x;
		for(int j=0;j<15;j++)
		{
			if((i>>j)&1)y-=f[j+1];
		}
		if(y>=0)res=min(res,cnt(y)+cnt(i));
	}
	cout<<res<<endl;
}
signed main()
{
    io;
    f[0]=1;
    for(int i=1;i<=15;i++)f[i]=f[i-1]*i;
 	cin>>_; 
 	while(_--)
    solve();
    return 0;
}

C. Not Assigning

题意:
在这里插入图片描述
思路:
素数除了2以外都是奇数,奇数+奇数=偶数,偶数+奇数=奇数
可知这棵树一定是一条链,一种方案是轮换放2,3
在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
const int N=1e5+10;
int h[N],e[N*2],ne[N*2],idx;
int d[N];
map<PII,int>mp;
PII q[N];
void add(int a,int b)
{
	e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void dfs(int u,int fa,int f)
{
	if(h[u]==-1)return;
	for(int i=h[u];~i;i=ne[i])
	{
		int j=e[i];
		if(j==fa)continue;
		if(f)mp[{u,j}]=mp[{j,u}]=2;
		else mp[{u,j}]=mp[{j,u}]=3;
		f^=1;
		dfs(j,u,f);
	}
}
void solve()
{ 
	cin>>n;
	idx=0;
	memset(h,-1,sizeof h);
	for(int i=1;i<=n;i++)d[i]=0;
	for(int i=0;i<n-1;i++)
	{
		int a,b;
		cin>>a>>b;
		add(a,b),add(b,a);
		d[a]++,d[b]++;
		q[i]={a,b};
	}
	int root=-1;
	for(int i=1;i<=n;i++)
	{
		if(d[i]>2)
		{
			cout<<-1<<endl;
			return;
		}
		if(d[i]==1)
		{
			root=i;
		}
	}
	dfs(root,-1,0);
	for(int i=0;i<n-1;i++)
	{
		cout<<mp[q[i]]<<' ';
	}
	cout<<endl; 
}
signed main()
{
    io;
 	cin>>_; 
 	while(_--)
    solve();
    return 0;
}

D. Weights Assignment For Tree Edges

题意:
在这里插入图片描述
思路:
我们可以构造两个点之间的距离是他们距离根节点的差值,dfs即可

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
const int N=2e5+10;
int h[N],ne[N<<1],e[N<<1],idx=1;
int d[N];
int c[N];
void add(int a,int b)
{
	e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void dfs(int u)
{
	for(int i=h[u];~i;i=ne[i])
	{
		int j=e[i];
		if(j==u)continue;
		d[i]=c[j]-c[u];
		dfs(j);	
	}
}
void solve()
{ 
	cin>>n;
	idx=1;
	int root=-1;
	memset(h,-1,sizeof h);
	for(int i=1;i<=n;i++)
	{
		int x;
		cin>>x;
		if(x==i)
		{
			root=x;
			d[root]=0;
		}
		add(x,i);
	}
	for(int i=1;i<=n;i++)
	{
		int x;
		cin>>x;
		c[x]=i;
	}
	dfs(root);
	for(int i=1;i<=n;i++)
	{
		if(d[i]<0)
		{
			cout<<-1<<endl;
			return;
		}
	}
	for(int i=1;i<=n;i++)cout<<d[i]<<' ';
	cout<<endl;
}
signed main()
{
    io;
 	cin>>_; 
 	while(_--)
    solve();
    return 0;
}
  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2022-06-29 19:19:23  更:2022-06-29 19:24:31 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/26 11:00:44-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码