给 2 2 2个长度均为 n n n的十进制数,你可以任意次交换 2 2 2个相同位置的数字,要求使它们乘积最小
让其中一个数最小,另一个数最大。
#include
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (998244353)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
string a,b;
int main()
{
// freopen("A.in","r",stdin);
// freopen(".out","w",stdout);
int n=read();
cin>>a>>b;
ll p=0,q=0;
Rep(i,n) {
if(a[i]<b[i]) p=(p*10+a[i]-'0')%F,q=(q*10+b[i]-'0')%F;
else {
p=(p*10+b[i]-'0')%F,q=(q*10+a[i]-'0')%F;
}
}
cout<<mul(p,q);
return 0;
}
给 2 2 2个长度为 n n n的串,每次可以把第一个串的第一个字符塞进这个字符串任意位置,问把这两个串变相同的最小次数。无解 − 1 -1 −1。
有解当且仅当各个字符在2个字符串中出现次数相同
贪心匹配第一个字符串中的后缀
#include
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
int main()
{
// freopen("B.in","r",stdin);
// freopen(".out","w",stdout);
int n=read();
string s,t;
cin>>s>>t;
int c[256]={};
Rep(i,n) c[s[i]]++,c[t[i]]--;
Fork(i,'a','z') if(c[i]){
puts("-1");return 0;
}
int p=n-1,ans=0;
RepD(i,n-1) {
while(p>=0 && s[i]!=t[p]) --p;
if(p<0) break;
--p,++ans;
}cout<<n-ans;
return 0;
}
You are given sequences of positive integers of length A , B A,B A,B
You can repeat the following operation any number of times (possibly zero).
Choose an integer i i i such that 1 ≤ i ≤ N 1≤i≤N 1≤i≤N and A_i :=A_i+1
.
Here, regard A N + 1 A_{N+1} AN+1 as A 1 A_1 A1
Determine whether it is possible to make A A A equal B B B.
A A A有一个数不在了就可以循环平移。
最后要么 A A A和 B B B一开始就一样,要么 A A A平移后能与 B B B的段一一对应。
#include
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define MAXN (5000+10)
int a[MAXN],b[MAXN],c[MAXN];
bool work(){
int n=read();
Rep(i,n) a[i]=read();
Rep(i,n) b[i]=read();
//case 1
bool fl=0;
Rep(i,n) if(a[i]!=b[i]) fl=1;
if(!fl) return 1;
//case 2
int p=0,q=0;
Rep(i,n) if(a[i]!=(a[(i+1)%n])) ++p;
Rep(i,n) if(b[i]!=(b[(i+1)%n])) ++q;
if(p==n&&q==n) return 0;
if(p<q) return 0;
//case 3
Rep(i,n) c[i]=b[i];
int m=unique(c,c+n)-c;
// Rep(i,m) cout<
if(m>=2 &&c[m-1]==c[0]) --m;
Rep(t,n) {
int p=0;
Rep(len,n) {
int i=(t+len)%n;
if(a[i]==c[p]) {
++p;
}
else continue;
}
if(p>=m) return 1;
}
return 0;
}
int main()
{
// freopen("C.in","r",stdin);
// freopen(".out","w",stdout);
int T=read();
while(T--) puts(work()?"Yes":"No");
return 0;
}
PCT has a permutation P 1 , … , P N ) P_1,…,P_N) P1,…,PN). You are only informed of N ≤ 2000 N \le 2000 N≤2000.
You can ask him at most 25000 25000 25000 questions of the following form.
Specify a triple of integers ( i , j , k ) (i,j,k) (i,j,k) such that 1 ≤ i , j , k ≤ N 1≤i,j,k≤N 1≤i,j,k≤N and ask whether P i + P j < P k P_i+P_j
( i , j , k i,j,k i,j,k can be equal)
P x + P x < = P y ( y ≠ x ) P_x+P_x <= P_y (y\ne x) Px+Px<=Py(y=x),则KaTeX parse error: Double subscript at position 7: P_y<_P_̲x,所以可以通过遍历 y y y来找出比 x x x小的数,如果找出则 y y y更新 x x x,就能找出 i d 1 ( P i d 1 = 1 ) id_1(P_{id_1}=1) id1(Pid1=1)
于是排列中的不同数差绝对值至少为1, a < b aa<b等价于 a + 1 < = b a+1<=b a+1<=b,剩下的数可以用归并排序,比较次数 O ( n l o g n ) O(nlogn) O(nlogn)
注意为了避免最坏情况要用stable_sort
代替sort
https://legacy.cplusplus.com/reference/algorithm/stable_sort/?kw=stable_sort
#include
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define MAXN (2010)
int n,id[MAXN]={},cnt=0;
bool get() {
string s;
cin>>s;
return s[0]=='Y';
}
bool ask(int a,int b,int c) {
cout<<"? "<<a<<' '<<b<<' '<<c<<endl;
fflush(stdout);
return get();
}
int cmp(int i,int j) {
return ask(i,id[1],j)^1;
}
int main()
{
// freopen("D.in","r",stdin);
// freopen(".out","w",stdout);
cin>>n;
For(i,n) id[i]=i;
Fork(i,2,n) if(!ask(i,i,id[1])){
swap(id[1],id[i]);
}
if(n>1)
stable_sort(id+2,id+n+1,cmp);
int p[MAXN];
For(i,n) p[id[i]]=i;
cout<<"!";
For(i,n) cout<<' '<<p[i];cout<<endl;
fflush(stdout);
return 0;
}