松山湖游船
题意:
输入:
第一行输入三个整数n(1<=n<=20), m(1<=m<= 500 ) 和 W(1<=w<= 10000)。
接下来n行输入n名游客的重量ci(1<=ci<=1000)。
接下来m行,每行一个1到n之间的整数,表示游客的编号。表示该游客的上/下船,如果该游客已经在船上,那么该游客就下船,反之就上船。刚开始的时候所有游客都不在船上。
输出:
输出一个整数表示整个上下船过程中,船所受到的最大重量。如果最大重量超过了船的最大承重能力,输出“Oh, My God!”
思路:题意简单,直接模拟就行!(思路在代码下方)
code:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#define INF 0x3f3f3f3f
#define ll long long
using namespace std;
ll n,m,w;
ll maxx;
map<ll,ll> ma;
map<ll,bool> b;
bool ok = true;
int main()
{
ios::sync_with_stdio(false);
cin >> n >> m >> w;
for(int i = 1,t;i <= n;++i)
{
cin >> t;
ma[i] = t;
}
ll sum = 0;
while(m--)
{
ll x;
cin >> x;
if(b[x] == 0)//没有在船上
{
sum += ma[x];
b[x] = true;//改变状态
}
else
{
sum -= ma[x];
b[x] = false;//改变状态
}
if(sum > w)
{
cout << "Oh, My God!" << endl;
ok = false;
break;
}
maxx = max(maxx,sum);
}
if(ok) cout << maxx << endl;
return 0;
}
//思路:
/*
1.我们规定ma[i]表示第i号人的重量
2.我们规定b[i]为第i号人的状态(false为不在,true则反之)
3.对于每一次的询问,我们根据b[i]的值决定,sum是加还是减,只要不超过最大承受值,就与取答案max
*/
Thanks for your watching!
Hope this blog can help you!