新生41
Cat Snuke and a Voyage
题目描述
In Takahashi Kingdom, there is an archipelago of N islands, called Takahashi Islands. For convenience, we will call them Island 1, Island 2, ..., Island N.
There are M kinds of regular boat services between these islands. Each service connects two islands. The i-th service connects Island ai and Island bi.
Cat Snuke is on Island 1 now, and wants to go to Island N. However, it turned out that there is no boat service from Island 1 to Island N, so he wants to know whether it is possible to go to Island N by using two boat services.
Help him.
Constraints
3≤N≤200 000
1≤M≤200 000
1≤ai<bi≤N
(ai,bi)≠(1,N)
If i≠j, (ai,bi)≠(aj,bj).
There are M kinds of regular boat services between these islands. Each service connects two islands. The i-th service connects Island ai and Island bi.
Cat Snuke is on Island 1 now, and wants to go to Island N. However, it turned out that there is no boat service from Island 1 to Island N, so he wants to know whether it is possible to go to Island N by using two boat services.
Help him.
Constraints
3≤N≤200 000
1≤M≤200 000
1≤ai<bi≤N
(ai,bi)≠(1,N)
If i≠j, (ai,bi)≠(aj,bj).
输入
Input is given from Standard Input in the following format:
N M
a1 b1
a2 b2
:
aM bM
N M
a1 b1
a2 b2
:
aM bM
输出
If it is possible to go to Island N by using two boat services, print POSSIBLE; otherwise, print IMPOSSIBLE.
样例输入 Copy
3 2
1 2
2 3
样例输出 Copy
POSSIBLE
这个题我好激动,原来只开一维的map我就知道肯定会出问题,比如遇到2 4,2 3这种情况会被覆盖,所以想到开二维mp,第二维再开个vector容器
#include<iostream> #include<vector> #include<map> using namespace std; vector<int> s; map<int,vector<int> > mp; int main(){ int n,m; cin>>n>>m; for(int i=0;i<m;i++) { int a,b; cin>>a>>b; if(a==1) s.push_back(b); else if(b==1) s.push_back(a); else if(a!=1&&b!=1) { mp[a].push_back(b); mp[b].push_back(a); } } int f=1; for(int i=0;i<s.size();i++) { int x=s[i]; for(int j=0;j<mp[x].size();j++) { if(mp[x][j]==n) { f=0; cout<<"POSSIBLE"<<endl; } } } if(f) cout<<"IMPOSSIBLE"<<endl; }
接水问题:
题目描述
学校里有一个水房,水房里一共装有m个龙头可供同学们打开水,每个龙头每秒钟的供水量相等,均为1。
现在有n名同学准备接水,他们的初始接水顺序已经确定。将这些同学按接水顺序从1到n编号,i号同学的接水量为wi。接水开始时,1到m号同学各占一个水龙头,并同时打开水龙头接水。当其中某名同学j完成其接水量要求wj后,下一名排队等候接水的同学k马上接替j同学的位置开始接水。这个换人的过程是瞬间完成的,且没有任何水的浪费。即j同学第x秒结束时完成接水,则k同学第x+1秒立刻开始接水。若当前接水人数n’不足m,则只有n’个龙头供水,其它m−n’个龙头关闭。
现在给出n名同学的接水量,按照上述接水规则,问所有同学都接完水需要多少秒。
现在有n名同学准备接水,他们的初始接水顺序已经确定。将这些同学按接水顺序从1到n编号,i号同学的接水量为wi。接水开始时,1到m号同学各占一个水龙头,并同时打开水龙头接水。当其中某名同学j完成其接水量要求wj后,下一名排队等候接水的同学k马上接替j同学的位置开始接水。这个换人的过程是瞬间完成的,且没有任何水的浪费。即j同学第x秒结束时完成接水,则k同学第x+1秒立刻开始接水。若当前接水人数n’不足m,则只有n’个龙头供水,其它m−n’个龙头关闭。
现在给出n名同学的接水量,按照上述接水规则,问所有同学都接完水需要多少秒。
输入
第1行2个整数n和m,用一个空格隔开,分别表示接水人数和龙头个数。
第2行n个整数w1、w2、……、wn,每两个整数之间用一个空格隔开,wi表示i号同学的接水量。
1≤n≤10000,1≤m≤100且m≤n;1≤wi≤100。
第2行n个整数w1、w2、……、wn,每两个整数之间用一个空格隔开,wi表示i号同学的接水量。
1≤n≤10000,1≤m≤100且m≤n;1≤wi≤100。
输出
输出只有一行,1个整数,表示接水所需的总时间。
样例输入 Copy
5 3
4 4 1 2 1
样例输出 Copy
4
提示
第1秒,3人接水。第1秒结束时,1、2、3号同学每人的已接水量为1,3号同学接完水,4号同学接替3号同学开始接水。
第2秒,3人接水。第2秒结束时,1、2号同学每人的已接水量为2,4号同学的已接水量为1。
第3秒,3人接水。第3秒结束时,1、2号同学每人的已接水量为3,4号同学的已接水量为2。4号同学接完水,5号同学接替4号同学开始接水。
第4秒,3人接水。第4秒结束时,1、2号同学每人的已接水量为4,5号同学的已接水量为1。1、2、5号同学接完水,即所有人完成接水。
总接水时间为4秒。
第2秒,3人接水。第2秒结束时,1、2号同学每人的已接水量为2,4号同学的已接水量为1。
第3秒,3人接水。第3秒结束时,1、2号同学每人的已接水量为3,4号同学的已接水量为2。4号同学接完水,5号同学接替4号同学开始接水。
第4秒,3人接水。第4秒结束时,1、2号同学每人的已接水量为4,5号同学的已接水量为1。1、2、5号同学接完水,即所有人完成接水。
总接水时间为4秒。
这个题看清楚题目,是按顺序来的,那没啥好说的了,单纯的模拟题
#include<iostream> #include<deque> using namespace std; const int N=1e5+10; deque<int> a,b; int main(){ int n,m,t,f; cin>>n>>m; for(int i=1;i<=m;i++) scanf("%d",&a[i]); for(int j=m+1;j<=n;j++) { cin>>t; b.push_back(t); } int sum=0; while(b.size()>=1) { int minn=1000000; for(int i=1;i<=m;i++) { if(minn>a[i]) { f=i; minn=a[i]; } } sum+=minn; for(int i=1;i<=m;i++) a[i]-=minn; int x=b.front(); b.erase(b.begin()); a[f]=x; } int maxn=-1; for(int i=1;i<=m;i++) maxn=max(maxn,a[i]); sum=sum+maxn; cout<<sum<<endl; return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具