codeforce 891 B
构造法,让其sort后统统向后移一位,最小的变最大,再映射回去。
正确性,我们考虑变大的那一个(其他都变小,不取这个不可能),若是相等,必然要全取,得证。
#include<bits/stdc++.h> #define N 57 #define sight(c) ('0'<=c&&c<='9') inline char nc(){ static char buf[1000000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++; } inline void read(int &x){ static char c; for (c=getchar();!sight(c);c=getchar()); for (x=0;sight(c);c=getchar())x=x*10+c-48; } void write(int x){if (x<10) {putchar('0'+x); return;} write(x/10); putchar('0'+x%10);} inline void writeln(int x){ if (x<0) putchar('-'),x*=-1; write(x); putchar('\n'); } inline void writel(int x){ if (x<0) putchar('-'),x*=-1; write(x); putchar(' '); } int n,a[N],b[N],p[N]; bool cmp(int A,int B){return a[A]<a[B];} using namespace std; signed main () { read(n); for (int i=1;i<=n;i++) read(a[i]),p[i]=i; sort(p+1,p+n+1,cmp); for (int i=1;i<n;i++) b[p[i+1]]=a[p[i]]; b[p[1]]=a[p[n]]; for (int i=1;i<=n;i++) writel(b[i]); return 0; }