#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<ctime>
#define ll long long
#define ull unsigned long long
using namespace std;
// const int N=2e5+5,MM=10007;
// const ull base=13331;
// const double Pi=acos(-1.0);
// const ll C=299792458;
// //读入挂
// inline void read(int &x){char ch;bool ok;
// for(ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if(ch=='-') ok=1;
// for(x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());if(ok) x=-x;}
// namespace IO {
// const int MX = 4e7; //1e7 占用内存 11000kb
// char buf[MX]; int c, sz;
// void Begin() {
// c = 0;
// sz = fread(buf, 1, MX, stdin);//一次性全部读入
// }
// inline bool Read(int &t) {
// while (c < sz && buf[c] != '-' && (buf[c] < '0' || buf[c] > '9')) c++;
// if (c >= sz) return false;//若读完整个缓冲块则退出
// bool flag = 0; if(buf[c] == '-') flag = 1, c++;
// for(t = 0; c < sz && '0' <= buf[c] && buf[c] <= '9'; c++) t = t * 10 + buf[c] - '0';
// if(flag) t = -t;
// return true;
// }
// }
int a[300010];
int main () {
//ios::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
//int len = 0;
//bool aa = 0;
int j = n - 1;
int i = 0;
int temp1 = 0;
int temp2 = 0;
for (int i = 0 , j = n - 1; i < j; ) {
if (a[i] != a[j]){
temp1 = j - i;
break;
}
else {
i++;
}
}
for (int i = 0 , j = n - 1; i < j; ) {
j--;
if (a[i] != a[j]){
temp2 = j - i;
break;
}
}
temp1 = max(temp1, temp2);
//temp = max(temp, n - 1 - temp);
printf("%d\n", temp1);
}
Examples
3 3
0 1 0
0 1 0
1 0 0
1 0 0
1 0 0
1 0 0
Yes
6 7
0 0 1 1 0 0 1
0 1 0 0 1 0 1
0 0 0 1 0 0 1
1 0 1 0 1 0 0
0 1 0 0 1 0 1
0 1 0 1 0 0 1
1 1 0 1 0 1 1
0 1 1 0 1 0 0
1 1 0 1 0 0 1
1 0 1 0 0 1 0
0 1 1 0 1 0 0
0 1 1 1 1 0 1
Yes
3 4
0 1 0 1
1 0 1 0
0 1 0 1
1 1 1 1
1 1 1 1
1 1 1 1
No
#include<bits/stdc++.h>
#define inf 1000000007
#define N 1000007
#define pb push_back
#define ss second
#define ff first
#define pii pair<int,int>
#define ll long long
using namespace std;
int n,m;
bool v1[510][510],v2[510][510];
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> v1[i][j];
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> v2[i][j];
}
}
for(int i = 1; i < n; i++){
for(int j = 1; j < m; j++){
if(v1[i][j] == v2[i][j]) continue;
v1[i][j] = !(v1[i][j]);
v1[i][j+1] = !(v1[i][j+1]);
v1[i+1][j] = !(v1[i+1][j]);
v1[i+1][j+1] = !(v1[i+1][j+1]);
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(v1[i][j] != v2[i][j]){
cout << "NO";
return 0;
}
}
}
cout << "YES";
}
#include<bits/stdc++.h>
using namespace std;
int n,h,a[1005],i,j;
int main(){
ios::sync_with_stdio(false);
cin >> n >> h;
for (i = 1; i <= n; ++i) {
cin >> a[i];
sort(a + 1, a + i + 1);
long long su = 0;
for (j = i; j > 0; j -= 2)
su += a[j];
if (su > h)
break;
}
cout << i - 1 << endl;
return 0;
}