【基础算法-模拟-例题-*校长的问题】-C++

本文深入探讨了一种使用模拟方法解决特定编程挑战的策略。通过详细分析题目需求,文章提供了清晰的代码实现步骤,并强调了在某些情况下,即使数据条件宽松,模拟方法也能有效地解决问题。此外,文中还分享了一个具体的代码示例,展示了如何在main函数内直接进行模拟操作,避免了不必要的函数封装,从而简化了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为什么在题目前面打上星号呢?
这道题的正解不是模拟!

正解树状数组!
正解树状数组!
正解树状数组!

重要的事情说够三遍了!
但是,歪解模拟因为数据水都能AC!
因为这道题放在模拟专题中,所以我们就讨论如何用模拟来过!
原题链接
按照题目描述,我们就用函数来分块解决(方便校验)
但是当我们打完代码,我们可以发现。
哪里需要函数?直接在main函数里面模拟即可!
查找直接暴力跑一遍都能AC我是实在没想到
代码比较容易理解所以我就不做过多解释了哈!
代码水一波:

#include<bits/stdc++.h>
using namespace std;
int a_[100000+1];
int main()
{
	int n,m,a,b;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>a_[i];
	}
	for(int i=1;i<=m;i++)
	{
		cin>>a>>b;
		int ans=0;
		for(int i=1;i<=a;i++)
		{
			if(a_[i]<=b)ans++;
		}
		cout<<ans<<endl;
	}
	return 0;
}

转载于:https://www.cnblogs.com/moyujiang/p/11167758.html

算法sicily例题 1000. sicily 1155. Can I Post the lette Time Limit: 1sec Memory Limit:32MB Description I am a traveler. I want to post a letter to Merlin. But because there are so many roads I can walk through, and maybe I can’t go to Merlin’s house following these roads, I must judge whether I can post the letter to Merlin before starting my travel. Suppose the cities are numbered from 0 to N-1, I am at city 0, and Merlin is at city N-1. And there are M roads I can walk through, each of which connects two cities. Please note that each road is direct, i.e. a road from A to B does not indicate a road from B to A. Please help me to find out whether I could go to Merlin’s house or not. Input There are multiple input cases. For one case, first are two lines of two integers N and M, (N<=200, M<=N*N/2), that means the number of citys and the number of roads. And Merlin stands at city N-1. After that, there are M lines. Each line contains two integers i and j, what means that there is a road from city i to city j. The input is terminated by N=0. Output For each test case, if I can post the letter print “I can post the letter” in one line, otherwise print “I can't post the letter”. Sample Input 3 2 0 1 1 2 3 1 0 1 0 Sample Output I can post the letter I can't post the letter Source Code #include #include using namespace std; int n,m; vector vout[200]; bool visited[200]; bool flood(int u) { visited[u]=1; if (u==n-1) return 1; for (int x=0; x<vout[u].size(); x++) { int &v=vout[u][x]; if (!visited[v] && flood(v)) return 1; } return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值