我的博客園傳送門,看起來方便點
A. Ahahahahahahahaha
題意:給個一個偶數長度的01序列,要求洗掉不超過2/n個元素使得奇數位和等于偶數位和,
思路:注意到題目給的提示,只有0和1,且為偶數長度,
那么對和有貢獻的也就只有1,而且0或1總有一個出現次數小于等于n/2,
所以我們就有這樣的策略,把最后搞的只剩0或者1即可,0的個數小就刪0,反之刪1,
最后注意只保留1的時候還要考慮一下答案陣列長度的奇偶性,
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
ll a[maxn];
ll n;
int main()
{
int kase;
cin>>kase;
while(kase--)
{
vector<ll> ans;
n = read();
rep(i,1,n) a[i] = read();
ll sum = 0;
rep(i,1,n) sum += a[i];
if(sum > n/2)
{
rep(i,1,n) if(a[i]==1) ans.pb(a[i]);
if(ans.size()&1)
{
ans.pop_back();
}
}
else rep(i,1,n) if(a[i]==0) ans.pb(a[i]);
if(ans.size()==1) ans[0] = 0;
cout<<ans.size()<<'\n';
for(int i=0; i<ans.size(); i++) cout<<ans[i]<<' ';
cout<<'\n';
}
return 0;
}
B. Big Vova
題意:給一個a陣列,問你構造一個b序列,使得存在c序列,其中c[i] = gcd(b[1],b[2],…,b[i]),且c的字典序最大,
思路:貪心,第一個肯定是最大的那個元素,然后每次暴力列舉出下一位能放的產生gcd最大的那個即可,
view code#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
ll n;
ll a[maxn];
ll vis[1005];
int main()
{
int kase;
cin>>kase;
while(kase--)
{
vector<ll> ans;
mem(vis,0);
ll n = read();
rep(i,1,n) a[i] = read();
sort(a+1,a+1+n);
ll m = a[n];
ans.pb(m);
ll cnt = 1;
while(cnt<n)
{
ll ma = 0, pos = 0;
per(i,n-1,1) if(!vis[i]&&gcd(a[i],m)>ma||(!vis[i]&&gcd(a[i],m)==ma&&a[pos]>a[i])) ma = gcd(a[i],m), pos = i;
cnt++;
ans.pb(a[pos]);
m = ma;
vis[pos] = 1;
}
rep(i,1,n) cout<<ans[i-1]<<' ';
cout<<'\n';
}
return 0;
}
C. Chocolate Bunny
題意:互動題,有一個n排列(沒告訴你長什么樣),你每次可以問它p[i]%p[j]的結果,然后他會告訴你這個值,讓你在不超過2n的詢問下把這個序列求出來,
思路:第一次做互動題剛開始有點蒙圈
其實想到一個點就可以立馬A掉了,我們用雙指標從頭尾兩端開始遍歷,每次詢問兩個,(p,q)和(q,p),因為肯定要么是a[p] > a[q] 要么是a[q] > a[p],所以結果肯定一個等于兩者之間的最小值,一個是比這個最小值要小,所以我們每兩次詢問都能得到一個a[p]或a[q],然后移動指標即可,如2%5 和 5%2 ,分別是2和1,所以a[p]肯定是2,
具體詳見代碼,
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
ll a[maxn];
ll vis[maxn];
int main()
{
ll n = read();
int p = 1, q = n;
int cnt = 0;
while(cnt<=n-1&&p<q)
{
printf("? %d %d\n", p, q);
fflush(stdout);
ll r1 = read();
printf("? %d %d\n", q, p);
fflush(stdout);
ll r2 = read();
if(r1 > r2) a[p] = r1, p++,vis[r1]=1;
else a[q] = r2, q--, vis[r2]=1;
cnt++;
}
ll sum = (1+n)*n/2;
ll all = 0;
rep(i,1,n) all += a[i];
rep(i,1,n) if(a[i]==0) a[i] = sum - all;
printf("! ");
rep(i,1,n) printf("%lld%c",a[i], i==n?'\n':' ');
fflush(stdout);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/3322.html
標籤:python
上一篇:canvas實作地圖放大縮小拖拽
