A few years ago, Hitagi encountered a giant crab, who stole the whole of her body weight. Ever since, she tried to avoid contact with others, for fear that this secret might be noticed.
To get rid of the oddity and recover her weight, a special integer sequence is needed. Hitagi's sequence has been broken for a long time, but now Kaiki provides an opportunity.
Hitagi's sequence a has a length of n. Lost elements in it are denoted by zeros. Kaiki provides another sequence b, whose length k equals the number of lost elements in a (i.e. the number of zeros). Hitagi is to replace each zero in a with an element from b so that each element in b should be used exactly once. Hitagi knows, however, that, apart from 0, no integer occurs in a and b more than once in total.
If the resulting sequence is not an increasing sequence, then it has the power to recover Hitagi from the oddity. You are to determine whether this is possible, or Kaiki's sequence is just another fake. In other words, you should detect whether it is possible to replace each zero in a with an integer from b so that each integer from b is used exactly once, and the resulting sequence is not increasing.
The first line of input contains two space-separated positive integers n (2 ≤ n ≤ 100) and k (1 ≤ k ≤ n) — the lengths of sequence a and brespectively.
The second line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 200) — Hitagi's broken sequence with exactly k zero elements.
The third line contains k space-separated integers b1, b2, ..., bk (1 ≤ bi ≤ 200) — the elements to fill into Hitagi's sequence.
Input guarantees that apart from 0, no integer occurs in a and b more than once in total.
Output "Yes" if it's possible to replace zeros in a with elements in b and make the resulting sequence not increasing, and "No" otherwise.
4 2 11 0 0 14 5 4
Yes
6 1 2 3 0 8 9 10 5
No
4 1 8 94 0 4 89
Yes
7 7 0 0 0 0 0 0 0 1 2 3 4 5 6 7
Yes
In the first sample:
- Sequence a is 11, 0, 0, 14.
- Two of the elements are lost, and the candidates in b are 5 and 4.
- There are two possible resulting sequences: 11, 5, 4, 14 and 11, 4, 5, 14, both of which fulfill the requirements. Thus the answer is "Yes".
In the second sample, the only possible resulting sequence is 2, 3, 5, 8, 9, 10, which is an increasing sequence and therefore invalid.
直接对b数组从大到小进行排序,然后填进a中,判断a数组是否是非递增
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
const int N = 1e7+5;
int n,m,t;
int a[1000];
vi b;
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
cin >> n>>m;
for(int i=0;i<n;i++)
{
cin >> a[i];
}
for(int i=0;i<m;i++)
{
cin>>t;
b.push_back(t);
}
sort(b.begin(),b.end(),cmp);
int x = 0 ;
for(int i=0;i<n;i++)
{
if(a[i]==0)
{
a[i] = b[x++];
}
}
for(int i=1;i<n;i++)
{
if(a[i]<=a[i-1])
{
cout <<"Yes"<<endl;
return 0;
}
}
cout <<"No"<<endl;
return 0;
}
Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.
On that night, Sengoku constructed a permutation p1, p2, ..., pn of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with n meteorids, colours of which being integer sequences a1, a2, ..., an and b1, b2, ..., bn respectively. Meteoroids' colours were also between 1 and n inclusive, and the two sequences were not identical, that is, at least one i (1 ≤ i ≤ n) exists, such that ai ≠ bi holds.
Well, she almost had it all — each of the sequences a and b matched exactly n - 1 elements in Sengoku's permutation. In other words, there is exactly one i (1 ≤ i ≤ n) such that ai ≠ pi, and exactly one j (1 ≤ j ≤ n) such that bj ≠ pj.
For now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.
The first line of input contains a positive integer n (2 ≤ n ≤ 1 000) — the length of Sengoku's permutation, being the length of both meteor outbursts at the same time.
The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of colours in the first meteor outburst.
The third line contains n space-separated integers b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of colours in the second meteor outburst. At least one i (1 ≤ i ≤ n) exists, such that ai ≠ bi holds.
Output n space-separated integers p1, p2, ..., pn, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them.
Input guarantees that such permutation exists.
5 1 2 3 4 3 1 2 5 4 5
1 2 5 4 3
5 4 4 2 3 1 5 4 5 3 1
5 4 2 3 1
4 1 1 3 4 1 4 3 4
1 2 3 4
In the first sample, both 1, 2, 5, 4, 3 and 1, 2, 3, 4, 5 are acceptable outputs.
In the second sample, 5, 4, 2, 3, 1 is the only permutation to satisfy the constraints.
a和b只有两种情况:
a和b只有一个元素不相同:为结果填充上其没出现的那个元素即可
a和b有两个元素不相同:尝试两种情况,分别填充其没出现的两个元素,并检验
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
const int N = 1e7+5;
int n,t;
vi a;
vi b;
bool cmp(int x,int y)
{
return x>y;
}
int f[10005];
int main()
{
int fa = 0;
cin >> n;
for(int i=0;i<n;i++)
{
cin >>t;
a.push_back(t);
}
for(int i=0;i<n;i++)
{
cin >>t;
b.push_back(t);
}
for(int i=0;i<n;i++)
{
if(a[i]!=b[i])
fa++;
}
if(fa==1)
{
int d = 0;
for(int i=0;i<n;i++)
{
if(a[i]!=b[i])
{
d = i;
}
else
{
f[a[i]]=1;
}
}
for(int i=1;i<=n;i++)
{
if(f[i]==0)
{
a[d] = i;
break;
}
}
for(int i=0;i<n;i++)
{
cout << a[i]<<" ";
}
}
else
{
vi ans;
ans.clear();
ans = a;
vi dd;
dd.clear();
for(int i=0;i<n;i++)
{
if(a[i]!=b[i])
{
dd.push_back(i);
}
else
{
f[a[i]]=1;
}
}
int tt = 0;
for(int i=1;i<=n;i++)
{
if(f[i]==0)
{
ans[dd[tt++]] = i;
}
}
int ct1 =0 ;
int ct2 =0 ;
for(int i=0;i<n;i++)
{
if(a[i]!=ans[i])
ct1 ++;
}
for(int i=0;i<n;i++)
{
if(b[i]!=ans[i])
ct2 ++;
}
if(ct1==1&&ct2==1)
{
for(int i=0;i<n;i++)
{
cout << ans[i]<<" ";
}
}
else
{
swap(ans[dd[0]],ans[dd[1]]);
for(int i=0;i<n;i++)
{
cout << ans[i]<<" ";
}
}
}
cout <<endl;
return 0;
}
Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it!
Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from left to right, and the i-th piece has a colour si, denoted by a lowercase English letter. Nadeko will repaint at most m of the pieces to give each of them an arbitrary new colour (still denoted by a lowercase English letter). After this work, she finds out all subsegments of the garland containing pieces of only colour c — Brother Koyomi's favourite one, and takes the length of the longest among them to be the Koyomity of the garland.
For instance, let's say the garland is represented by "kooomo", and Brother Koyomi's favourite colour is "o". Among all subsegments containing pieces of "o" only, "ooo" is the longest, with a length of 3. Thus the Koyomity of this garland equals 3.
But problem arises as Nadeko is unsure about Brother Koyomi's favourite colour, and has swaying ideas on the amount of work to do. She has q plans on this, each of which can be expressed as a pair of an integer mi and a lowercase letter ci, meanings of which are explained above. You are to find out the maximum Koyomity achievable after repainting the garland according to each plan.
The first line of input contains a positive integer n (1 ≤ n ≤ 1 500) — the length of the garland.
The second line contains n lowercase English letters s1s2... sn as a string — the initial colours of paper pieces on the garland.
The third line contains a positive integer q (1 ≤ q ≤ 200 000) — the number of plans Nadeko has.
The next q lines describe one plan each: the i-th among them contains an integer mi (1 ≤ mi ≤ n) — the maximum amount of pieces to repaint, followed by a space, then by a lowercase English letter ci — Koyomi's possible favourite colour.
Output q lines: for each work plan, output one line containing an integer — the largest Koyomity achievable after repainting the garland according to it.
6 koyomi 3 1 o 4 o 4 m
3 6 5
15 yamatonadeshiko 10 1 a 2 a 3 a 4 a 5 a 1 b 2 b 3 b 4 b 5 b
3 4 5 7 8 1 2 3 4 5
10 aaaaaaaaaa 2 10 b 10 z
10 10
In the first sample, there are three plans:
- In the first plan, at most 1 piece can be repainted. Repainting the "y" piece to become "o" results in "kooomi", whose Koyomity of 3 is the best achievable;
- In the second plan, at most 4 pieces can be repainted, and "oooooo" results in a Koyomity of 6;
- In the third plan, at most 4 pieces can be repainted, and "mmmmmi" and "kmmmmm" both result in a Koyomity of 5.
模拟 贪心
对于其改变的数贪心填充。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=1505;
char s[MAXN];
int main()
{
int n;
scanf("%d%s",&n,s+1);
int q;
scanf("%d",&q);
while(q--)
{
int m,res=0;
char t[10],ch;
scanf("%d%s",&m,t);
ch = t[0];
for(int l=1,r=1;l<=n;l++)
{
while(m>=(s[r]!=ch) && r<=n)
{
if(s[r++]!=ch)
m--;
}
if(s[l]!=ch)
m++;
res=max(res,r-l);
}
printf("%d\n",res);
}
return 0;
}