CodeForces 319B
2016-03-31 21:55
239 查看
有编号为1~N的N个人站成一排(顺序是乱的),每人手中拿着一把枪。每一个时刻,每个人同时瞄准右边的人(如
果存在的话),然后如果这个人编号比自己大,则开枪将他打死。因此,一个人可能在开枪的同时被打死。一轮开
枪结束后,死亡的人视为离开了队列。那么经过多少时刻才能使得整个序列进入稳定模式—— 即不再有人死亡呢?
Input
第一行是N(<=100000),然后N个数,表示初始的站序。
Output
一行一个正整数,表示需要的时刻(即多少轮同时开枪)。此输出应该为最早满足如下条件的时刻:此时的状态应
当等同于此时刻以后任意时刻的状态。
Sample Input
10
10 9 7 8 6 5 3 4 2 1
Sample Output
2
Hint
[10 9 7 8 6 5 3 4 2 1]→[10 8 4]→[10]
不知道怎么回事头很晕,从最后面开始遍历。脑袋不够清楚,明天仔细想清楚。#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> v;
vector<int>::iterator it,it2;
const int N=100500;
int a
;
int nex
;
int main()
{
//freopen("in.txt","r",stdin);
int n;
cin>>n;
a[0]=0;
a[n+1]=n+1;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
nex[i]=i+1;
if(a[i]<a[i-1]) v.push_back(i-1);
}
int step=0;
while(!v.empty())
{
++step;
it2=it=v.end();
it2--;
bool flag=true;
do
{
it=it2;
if(it2!=v.begin())
it2--;
int now=*it,now2=*it2;
nex[now]=nex[nex[now]];
if(it==v.begin()) flag=false;
if(a[now] < a[nex[now]] || (it != v.begin() && nex[now2] == now)) v.erase(it);
}while(flag);
}
cout<<step<<endl;
return 0;
}
果存在的话),然后如果这个人编号比自己大,则开枪将他打死。因此,一个人可能在开枪的同时被打死。一轮开
枪结束后,死亡的人视为离开了队列。那么经过多少时刻才能使得整个序列进入稳定模式—— 即不再有人死亡呢?
Input
第一行是N(<=100000),然后N个数,表示初始的站序。
Output
一行一个正整数,表示需要的时刻(即多少轮同时开枪)。此输出应该为最早满足如下条件的时刻:此时的状态应
当等同于此时刻以后任意时刻的状态。
Sample Input
10
10 9 7 8 6 5 3 4 2 1
Sample Output
2
Hint
[10 9 7 8 6 5 3 4 2 1]→[10 8 4]→[10]
不知道怎么回事头很晕,从最后面开始遍历。脑袋不够清楚,明天仔细想清楚。#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> v;
vector<int>::iterator it,it2;
const int N=100500;
int a
;
int nex
;
int main()
{
//freopen("in.txt","r",stdin);
int n;
cin>>n;
a[0]=0;
a[n+1]=n+1;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
nex[i]=i+1;
if(a[i]<a[i-1]) v.push_back(i-1);
}
int step=0;
while(!v.empty())
{
++step;
it2=it=v.end();
it2--;
bool flag=true;
do
{
it=it2;
if(it2!=v.begin())
it2--;
int now=*it,now2=*it2;
nex[now]=nex[nex[now]];
if(it==v.begin()) flag=false;
if(a[now] < a[nex[now]] || (it != v.begin() && nex[now2] == now)) v.erase(it);
}while(flag);
}
cout<<step<<endl;
return 0;
}
相关文章推荐
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- codeforces 618C. Constellation
- Codeforces Round #265 (Div. 2)
- Codeforces #310 div2 C. Case of Matryoshkas
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers
- codeforces 16 Problem E fish
- Codeforces Round332 部分题解
- CodeForces 603A_Alternative Thinking (DP)
- CodeForces 602B_Approximating a Constant Range_DP
- Codeforces round #247 for Div. 2
- Codeforces Round #246 (Div. 2)
- Codeforces #264(div 2)D.Gargari and Permutations
- Codeforces Round #236 (Div. 2)------A,B
- codeforces 257 div2 B
- Codeforces Gym100571A Cursed Query