Tutorial is loading...
code
#include <bits/stdc++.h>
using namespace std;
int main(){
int t;cin>>t;
while(t--){
int x,y;cin>>x>>y;
string s;cin>>s;
int u=0,d=0,l=0,r=0;
for(int i=0;i<s.length();i++){
if(s[i]=='U')u++;
else if(s[i]=='R')r++;
else if(s[i]=='D')d++;
else l++;
}
if(x > 0 && r >= x )x = 0;
if(x < 0 && l >= -x )x = 0;
if(y > 0 && u >= y )y = 0;
if(y < 0 && d >= -y )y = 0;
cout<<((!x && !y)?"YES":"NO")<<endl;
}
}
Author: Salem_Alwarawreh
Tutorial is loading...
code
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define fast ios::sync_with_stdio(false);cin.tie(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main(){
fast
int t;
cin>>t;
while(t--){
int n,k;
cin>>n>>k;
vector<int> a(n);
for(int i=0;i<n;i++)cin>>a[i];
int mx = *max_element(all(a));
if(n * mx < k){
cout << -1 << '\n';
continue;
}
int ans = n+1;
for(int b=0;b<k;b++){
int to = -2;
for(int i=0;i<n-1;i++){
if(a[i] < a[i+1]){
to = i;
break;
}
}
ans = to + 1;
if(to == -2)break;
a[to]++;
}
cout << ans << '\n';
}
}
Author: Warawreh
Tutorial is loading...
code
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define oo 1000000010
const int N = 300010;
int n , m;
int a[N] , b[N] , c[N] , ans[N];
vector< int > g[N];
void solve(){
scanf("%d%d",&n,&m);
for(int i = 1 ;i <= n;i++) g[i].clear();
for(int i = 0 ;i < n;i++)
scanf("%d",&a[i]);
for(int i = 0 ;i < n;i++){
scanf("%d",&b[i]);
if(b[i] != a[i])
g[b[i]].push_back(i);
}
for(int i = 0; i < m;i++){
scanf("%d",&c[i]);
}
int last = -1;
if((int)g[c[m - 1]].size() > 0){
last = g[c[m - 1]].back();
g[c[m - 1]].pop_back();
}
else{
for(int i = 0 ;i < n;i++){
if(b[i] == c[m - 1]){
last = i;
break;
}
}
}
if(last == -1){
puts("NO");
return;
}
ans[m - 1] = last;
for(int i = 0 ;i < m - 1;i++){
if((int)g[c[i]].size() == 0){
ans[i] = last;
}
else{
ans[i] = g[c[i]].back();
g[c[i]].pop_back();
}
}
for(int i = 1;i <= n;i++){
if((int)g[i].size() > 0){
puts("NO");
return;
}
}
puts("YES");
for(int i = 0 ;i < m;i++){
if(i) putchar(' ');
printf("%d",ans[i] + 1);
}
puts("");
}
int main(){
int t;
cin >> t;
while(t--)
solve();
return 0;
}
Author: Warawreh
Tutorial is loading...
code
#include <bits/stdc++.h>
using namespace std;
#define mod 998244353
#define oo 1000000010
const int N = 1010;
int n , m;
char grid[N][N];
int has[N][2];
void solve(){
scanf("%d%d",&n,&m);
for(int i = 0 ;i <= n;i++) has[i][0] = has[i][1] = -1;
for(int i = 0 ;i < n;i++){
scanf("%s",grid[i]);
for(int j = 0 ;j < n;j++){
if(j == i) continue;
has[i][grid[i][j] - 'a'] = j;
}
}
if(m & 1){
puts("YES");
for(int i = 0 ;i < m + 1;i++){
if(i) putchar(' ');
printf("%d",(i & 1) + 1);
}
puts("");
return;
}
for(int i = 0 ;i < n;i++){
for(int j = i + 1;j < n;j++){
if(grid[i][j] == grid[j][i]){
puts("YES");
for(int k = 0 ;k < m + 1;k++){
if(k) putchar(' ');
printf("%d",(k & 1 ? i + 1 : j + 1));
}
puts("");
return;
}
}
}
for(int i = 0 ;i < n;i++){
for(int j = 0;j < n;j++){
if(i == j) continue;
if(has[j][grid[i][j] - 'a'] == -1) continue;
puts("YES");
int cur = has[j][grid[i][j] - 'a'];
if((m / 2) % 2 == 1){
for(int k = 0 ;k < m + 1;k++){
if(k) putchar(' ');
if(k % 4 == 0)
printf("%d",i + 1);
else if(k % 4 == 2)
printf("%d",cur + 1);
else
printf("%d",j + 1);
}
puts("");
return;
}
printf("%d",j + 1);
for(int k = 0 ;k < m / 2;k++){
if(k & 1) printf(" %d",j + 1); else printf(" %d",cur + 1);
}
for(int k = 0 ;k < m / 2;k++){
if(k & 1) printf(" %d",j + 1); else printf(" %d",i + 1);
}
puts("");
return;
}
}
puts("NO");
return;
}
int main(){
int t;
scanf("%d",&t);
while(t--)
solve();
return 0;
}
Author: Kilani
Tutorial is loading...
code
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define fast ios::sync_with_stdio(false);cin.tie(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main(){
fast
int n;
cin>>n;
vector<int> a(n),f(n),cf(n);
vector<pair<int,int>> at(n,{-1,-1});
for(int i=0;i<n;i++){
cin>>a[i];
a[i]--;
f[a[i]]++;
if(at[a[i]].first == -1)at[a[i]] = {i,i};
at[a[i]].second = i;
}
vector<int> dp(n+1);
for(int i=n-1;i>=0;i--){
dp[i] = dp[i+1];
cf[a[i]]++;
if(i == at[a[i]].first)dp[i] = max(dp[i] , dp[at[a[i]].second + 1] + f[a[i]]);
else dp[i] = max(dp[i] , cf[a[i]]);
}
cout << n - dp[0] << '\n';
}
Author: Warawreh
Tutorial is loading...
code
#include <bits/stdc++.h>
using namespace std;
#define mod 998244353
#define oo 1000000010
const int N = 100010;
const int SQ = 500;
int n , x , p;
vector< int > g[N] , cur[N];
char c[N];
int mx;
vector< pair<int,int> > v , v2;
pair< int , char > a , b;
int sz[N];
bool comp(int u,int v){
return (sz[u] < sz[v]);
}
int DFS(int node,int d){
mx = max(mx,d);
cur[d].push_back(node);
sz[node] = 1;
for(int i = 0 ;i < (int)g[node].size();i++)
sz[node] += DFS(g[node][i] , d + 1);
return sz[node];
}
bool dp[SQ][N];
int f[N];
void build_path(int i,int j){
if(i == (int)v2.size())
return;
while(!dp[i + 1][j]){
f[v2[i].first]++;
j -= v2[i].first;
}
build_path(i + 1 , j);
}
int main(){
scanf("%d%d",&n,&x);
a = make_pair(x , 'a');
b = make_pair(n - x , 'b');
if(a > b) swap(a , b);
for(int i = 2 ;i <= n;i++){
scanf("%d",&p);
g[p].push_back(i);
}
DFS(1 , 0);
for(int i = 0 ;i <= mx;i++)
v.push_back(make_pair((int)cur[i].size() , i));
sort(v.begin(),v.end());
for(int i = 0 ;i < (int)v.size();i++){
if(i == 0 || v[i].first != v[i - 1].first)
v2.push_back(make_pair(v[i].first , 1));
else
v2.back().second++;
}
dp[(int)v2.size()][0] = true;
int val , frq;
for(int i = (int)v2.size() - 1;i >= 0;i--){
val = v2[i].first;
frq = v2[i].second;
vector< int > last(val , -1);
for(int j = 0 ;j <= a.first;j++){
if(dp[i + 1][j] == true)
last[j % val] = j;
if(last[j % val] == -1 || ((j - last[j % val]) / val) > frq)
dp[i][j] = false;
else
dp[i][j] = true;
}
}
if(dp[0][a.first]){
build_path(0 , a.first);
for(int i = 1;i <= n;i++) c[i - 1] = b.second;
for(int i = 0 ;i <= mx;i++){
if(f[(int)cur[i].size()] == 0) continue;
f[(int)cur[i].size()]--;
for(int j = 0 ;j < (int)cur[i].size();j++)
c[cur[i][j] - 1] = a.second;
}
printf("%d\n",mx + 1);
c[n] = '\0';
puts(c);
return 0;
}
printf("%d\n",mx + 2);
for(int i = 0 ;i <= mx;i++){
sort(cur[i].begin(),cur[i].end(),comp);
if(a.first < b.first) swap(a , b);
while((int)cur[i].size() > 0 && a.first > 0){
c[cur[i].back() - 1] = a.second;
cur[i].pop_back();
a.first--;
}
while((int)cur[i].size() > 0 && b.first > 0){
c[cur[i].back() - 1] = b.second;
cur[i].pop_back();
b.first--;
}
}
c[n] = '\0';
puts(c);
return 0;
}
Author: Kilani