新生赛题解

A.吃东西

题目

已知a和b,求a*100+b

题解

代码

#include <iostream>
using namespace std;

double a;
int b;

int main()
{
	cin>>a>>b;
	cout<<100*a+b<<endl;
	return 0;
}

B.翻转字符串

题目

给定一个不重复的字符串s,然后给出字符串中的两个字符s[i]和s[j],要求输出交换s[i]到s[j]这一段的字符串后,输出这个更改后的字符串s

题解

从s中寻找s[i]和s[j]的下标,然后用三个循环逐次交换就行了。

代码

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

string s;

int main()
{
	int T;
	cin >> T;
	while (T--){
		cin>>s;
		string b,c;
		cin>>b>>c;
		int pos1,pos2;
		for(int i=0;i<s.length();i++) {
			if(s[i]==b[0])	pos1 = i;
			if(s[i]==c[0])	pos2 = i;
		}		
		for(int i=0;i<pos1;i++)	cout<<s[i];
		for(int i=pos2;i>=pos1;i--)	cout<<s[i];
		for(int i=pos2+1;i<s.length();i++)	cout<<s[i];
		cout<<endl;
	}
	return 0;
}

C.坏人与负鼠

题目

略...

题解

用一个大循环来解决。到了不能扔以后就跳出循环。

代码

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

int a,b,t;

int main()
{
	cin>>t;
	while(t--) {
		cin>>a>>b;
		for(int i=1;i<=1e9;i++) {
			if(i&1) {
				if(a<i) {
					cout<<"A"<<endl;
					break;
				}else
					a -= i; 
			}else {
				if(b<i) {
					cout<<"B"<<endl;
					break;
				}else
					b -= i;
			}
		}	
	}
	return 0;
}

D.爬楼梯

题目

爬楼梯,一次可以爬一级或两级,每级台阶都有一个伤害值,要求爬到第n级台阶时总伤害值尽量低。

题解

dp数组,w数组存=是每一级台阶的伤害,dp[i]代表到i级楼梯的最大血量,第i级可以从i-1,i-2级走到,dp[i]=max(dp[i-1]-w[i],dp[i-2]-w[i])

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+7;
ll dp[maxn];
void init()
{
	memset(dp,0,sizeof(dp));
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		init();
		int m;
		int n;
		cin>>m>>n;
		for(int i=1;i<=n;i++)
		{
			cin>>dp[i];
		}
		for(int i=3;i<=n;i++)
		{
			dp[i] += min(dp[i-1],dp[i-2]);
		}
		if(m-dp[n]>0)
		{
			cout<<m-dp[n]<<endl;
		}
		else
		{
			cout<<"Can't get the top!"<<endl;
		}
	} 
	return 0;
} 

E.Mr.muddle的历险

题目

给定一个三维迷宫,给出n个位置,每个位置用(x,y,z)来表示,表明当前位置不能走。问从(0,0,0)到(x,y,z)至少要走多少步。

题解

裸的BFS,注意由于存在数据不能到达终点,可能造成死循环。
所以需要vis数组来标记。

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <cmath>
using namespace std;

#define INIT(x) memset(x,0,sizeof(x))
#define eps 1e-8
#define next next_
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x7fffffff;
const int inf = 0x3f3f3f3f;
const int maxn = 105;

inline void read(int &x) {
    int f=1;x=0;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    x*=f;
}

bool vis[maxn][maxn][maxn];
int t,x,y,z,ex,ey,ez,n,tot;
int dx[6] = {1,-1,0,0,0,0};
int dy[6] = {0,0,1,-1,0,0};
int dz[6] = {0,0,0,0,1,-1};
bool a[maxn][maxn][maxn];
struct node{
	int x,y,z,tot;	
};
queue<node> q;

int bfs() {
	node temp;
	temp.x = 0,temp.y = 0,temp.z = 0,temp.tot = 0;	
	vis[0][0][0] = 1;
	q.push(temp);
	while(!q.empty()) {
		temp = q.front();
		x = temp.x, y = temp.y, z = temp.z, tot = temp.tot;
//		cout<<x<<" "<<y<<" "<<z<<endl;
		q.pop();
		if(x==ex&&y==ey&&z==ez) {
			return tot;
		}
		for(int i=0;i<6;i++) {
			int newx = x + dx[i];
			int newy = y + dy[i];
			int newz = z + dz[i];
			if(!a[newx][newy][newz]&&newx>=0&&newx<=ex&&newy>=0&&newy<=ey&&newz>=0&&newz<=ez&&!vis[newx][newy][newz]) {
				temp.x = newx,temp.y = newy,temp.z = newz,temp.tot = tot+1;	
				q.push(temp);
				vis[newx][newy][newz] = 1;
			}
		}
	}
	return -1;
}

int main()
{
	cin>>t;
	while(t--) {
		cin>>n>>ex>>ey>>ez;
		INIT(a);
		INIT(vis);
		while(!q.empty())	q.pop();
		for(int i=0;i<n;i++) {
			cin>>x>>y>>z;
			a[x][y][z] = 1;
		}
		int ans = bfs();
		if(ans==-1)	cout<<"Impossible"<<endl;
		else	cout<<ans<<endl;
	}
	return 0;
}

F.syf的难题

防ak的,我也不会

G.不同的数

题目

给定一个数字n,然后给出n个正整数a1, a2, …, an,以及m个询问,每次询问包含l和r两个数,要求出a1, a2, …, al, ar, ar + 1, …, an中不同的数的个数。

题解

本题用树状数组来完成。
  首先,可以将n扩充为原来的两倍,那么原数组的询问可以转化为求从a[r]到a[l+n]共有多少个不同的数。
  其次,用pre[i]记录从a[1]-a[i]有多少个不同的数字,那么对于a[l…r]的答案就为pre[r] - pre[l-1] + 在a[l…r]和a[1…l-1]同时出现的数字的种类,而对于如何求在a[l…r]和a[1…l-1]同时出现的数字的种类,可以考虑使用树状数组维护,树状数组的第i个点表示a[i]是否已经在1…l出现过,对于每个查询只需要查树状数组中l~r的区间和即可
  在实际运用中,可以将所有的询问按照左端点从小到大排序,然后依次向后添加即可。

代码

#include <bits/stdc++.h>
using namespace std;
  
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
  
const int N = 2e5+5;
  
int a[N];
int pre[N];
int ans[N];
bool vis[N];
int last[N];
int nxt[N];
int tree[N];
int n,q,t;
  
struct node{
    int l,r,id;
}x[N];
  
bool cmp(const node &a,const node &b)
{
    return a.l<b.l;
}
  
int lowbit(int x)
{
    return x & -x;
}
  
void update(int x,int y)
{
    for(int i=x;i<N;i+=lowbit(i))
        tree[i] += y;
}
  
int query(int x)
{
    int ans = 0;
    for(int i=x;i>0;i-=lowbit(i))
        ans += tree[i];
    return ans;
}
  
int getsum(int l,int r)
{
    return query(r)-query(l-1);
}
  
void init()
{
    memset(ans,0,sizeof(ans));
    memset(vis,0,sizeof(vis));
    memset(tree,0,sizeof(tree));
    memset(last,-1,sizeof(last));
    memset(nxt,-1,sizeof(nxt));
    memset(pre,0,sizeof(pre));
}

int main()
{
	cin>>t;
    while(t--)
    {
    	scanf("%d",&n);
        init();
        q=read();
        for(int i=1;i<=n;i++)
        {
            a[i] = read();
            a[i+n] = a[i];
        }
        n = n*2;
        pre[0] = 0;
        for(int i=1;i<=n;i++)
        {
            if(!vis[a[i]])
            {
                vis[a[i]] = 1;
                pre[i] = pre[i-1]+1;
            }
            else
                pre[i] = pre[i-1];
            if(~last[a[i]])
                nxt[last[a[i]]] = i;
            last[a[i]] = i;
        }
        for(int i=0;i<q;i++)
        {
            x[i].l=read();
            x[i].r=read();
            x[i].l += n/2;
            swap(x[i].l,x[i].r);
            x[i].id = i;
        }
        sort(x,x+q,cmp);
        int cnt = 1;
        for(int i=0;i<q;i++)
        {
            while(cnt<x[i].l) 
            {
                if(~nxt[cnt])
                    update(nxt[cnt],1);
                cnt++;
            }
            ans[x[i].id] = pre[x[i].r]-pre[x[i].l-1]+getsum(x[i].l,x[i].r);
        }
        for(int i=0;i<q;i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}

H.计算机的本质

题目

给定n个数,求出现次数超过n/2的数

题解

本意是用栈模拟,但是抬了一手,map也能过。
由于n个数里面有的数非常大,所以要用map<string,int>。

代码1(map)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <unordered_map>
#include <map>
#include <set>
#include <vector>
#include <cmath>
using namespace std;

int n;
string ans,s;
unordered_map<string,int> mp;

int main()
{
	read(n);
	for(int i=0;i<n;i++) {
		cin>>s;
		mp[s]++;
		if(mp[s]>n/2)	ans = s;
	}
	cout<<ans<<endl;
	return 0;
}

代码2(栈)

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int main() {
    string t, s;
    int c;
    for(int n; cin >> n;) {
        c = 0;
        s = t = "";
        for(int i = 0; i < n ; i++) {
            cin >> t;
            if(t != s) {
                if(s == "") {
                    s = t;
                    c = 1;
                } else {
                    if(c == 1) {
                        c = 0;
                        s = "";
                    } else {
                        c--;
                    }
                }
            } else {
                c++;
            }
        }
        cout << s << endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

总想玩世不恭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值