#include <iostream>
#include <vector>
#include <set>
#define ll long long
using namespace std;
signed main() {
ll n;
cin >> n;
vector<pair<ll,ll>> a(n);
vector<ll> pref(n,0),d(n,0),ans(n,0);
set<ll> s;
for(ll i = 0; i < n; i++){
cin >> a[i].first;
a[i].second = i;
s.insert(a[i].first);
if(i == 0)
pref[i] = a[i].first;
else
pref[i] = pref[i-1] + a[i].first;
d[i] = s.size();
}
if(d[n-1] > 1 || n == 1)
ans[a[n-1].second] = 1;
for(ll i = n - 2; i >= 0; i--){
if(pref[i] > a[i + 1].first && ans[a[i+1].second] == 1 && d[i] > 1)
ans[a[i].second] = 1;
for(ll i = 0; i < n; i++)
cout << ans[i] << " ";
P.S. Откуда задача, уж больно часто я ее встречаю на подобных сайтах
#include <cstdlib>
#include <ctime>
int main()
{
srand((int)time(0));
int M, N;
cout << "M="; cin >> M;
do
cout << "N="; cin >> N;
if (N%2!=0) cout << "N - even. Repeat please...\n";
while (N%2!=0);
double **a = new double*[M];
for (int i = 0; i < M; i++)
a[i]=new double[N];
for (int j = 0; j < N; j++)
a[i][j]=rand()%9 + 1;
cout << a[i][j] << " ";
cout << "\n";
for (int j = 0; j < N/2; j++)
swap (a[i][j],a[i][j+N/2]);
cout <<"\n";
delete[]a[i];
delete[]a;
system("pause");
return 0;
Объяснение:
#include <iostream>
#include <vector>
#include <set>
#define ll long long
using namespace std;
signed main() {
ll n;
cin >> n;
vector<pair<ll,ll>> a(n);
vector<ll> pref(n,0),d(n,0),ans(n,0);
set<ll> s;
for(ll i = 0; i < n; i++){
cin >> a[i].first;
a[i].second = i;
s.insert(a[i].first);
if(i == 0)
pref[i] = a[i].first;
else
pref[i] = pref[i-1] + a[i].first;
d[i] = s.size();
}
if(d[n-1] > 1 || n == 1)
ans[a[n-1].second] = 1;
for(ll i = n - 2; i >= 0; i--){
if(pref[i] > a[i + 1].first && ans[a[i+1].second] == 1 && d[i] > 1)
ans[a[i].second] = 1;
}
for(ll i = 0; i < n; i++)
cout << ans[i] << " ";
}
P.S. Откуда задача, уж больно часто я ее встречаю на подобных сайтах
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand((int)time(0));
int M, N;
cout << "M="; cin >> M;
do
{
cout << "N="; cin >> N;
if (N%2!=0) cout << "N - even. Repeat please...\n";
}
while (N%2!=0);
double **a = new double*[M];
for (int i = 0; i < M; i++)
a[i]=new double[N];
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
a[i][j]=rand()%9 + 1;
cout << a[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
for (int i = 0; i < M; i++)
for (int j = 0; j < N/2; j++)
swap (a[i][j],a[i][j+N/2]);
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
cout << a[i][j] << " ";
cout <<"\n";
}
for (int i = 0; i < M; i++)
delete[]a[i];
delete[]a;
system("pause");
return 0;
}
Объяснение: