思路:折纸游戏,看长宽能各折多少次,就是2的几次方,相乘即可。
view code#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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 endl '\n'
#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} };
int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll w = read(), h = read(), n = read();
ll cnt1 = 0, cnt2 = 0;
while(w%2==0&&w)w/=2, cnt1++;
while(h%2==0&&h) h/=2, cnt2++;
if((1LL<<cnt1)*(1LL<<cnt2)>=n) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
思路:首先看1的个数,若1的个数为奇数肯定不能均分。
若1的个数是偶数,就继续看2的个数,如果2这时候也是偶数就肯定能均分完。但是如果2的数量是奇数个,说明有个人要多拿一个2,所以看均分好的1够不够每个人都大于等于1个,这个时候2多拿的那个人少拿一个1就行了。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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 endl '\n'
#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} };
int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n = read();
ll cnt1 = 0, cnt2 = 0;
rep(i,1,n)
{
ll x = read();
if(x==1) cnt1++;
else cnt2++;
}
if(cnt1&1) cout<<"NO"<<endl;
else
{
if(cnt2&1)
{
if(cnt1>=2) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
else cout<<"YES"<<endl;
}
}
return 0;
}
思路:每次到的地方i+a[i]处的贡献可以提前知道,类似dp。从后到前遍历,d[i] = d[i+a[i]] + a[i]。
view code#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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 endl '\n'
#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 = 2e5+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 d[maxn];
int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n = read();
rep(i,1,n) a[i] = read();
d[n] = a[n];
ll ma = d[n];
per(i,n-1,1)
{
ll to = i + a[i];
if(to<=n) d[i] = d[to] + a[i];
else d[i] = a[i];
ma = max(ma,d[i]);
}
cout<<ma<<endl;
}
return 0;
}
思路:考虑回合制。首先贪心,如果我有大的肯定会先拿大的,把各自奇偶数分开排序。
然后如果是Alice回合(拿偶数round),我可以自己拿偶数加分或者拿掉对面最大的(奇数)让对手难受。怎么判断拿哪个呢?其实就看各自的贡献,如果我当前最大比对方的最大要小,我就拿走对方的,这样我就算损失也可以尽可能小。反之就赶紧把自己大的这个拿走,不然对方会用同样招数来恶心你。
对于Bob也是如此。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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 endl '\n'
#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 = 2e5+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 odd[maxn];
ll even[maxn];
int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n = read();
int p1 = 0;
int p2 = 0;
rep(i,1,n)
{
ll x = read();
if(x&1) odd[++p1] = x;
else even[++p2] = x;
}
sort(odd+1,odd+1+p1);
sort(even+1,even+1+p2);
ll a = 0;
ll b = 0;
int cnt = 1;
while(p1>=1||p2>=1)
{
if(cnt&1)
{
if(p1>=1&&odd[p1]>even[p2]) p1--;
else a += even[p2--];
}
else
{
if(p2>=1&&even[p2]>odd[p1]) p2--;
else b += odd[p1--];
}
cnt++;
}
if(a>b) cout<<"Alice"<<endl;
else if(a<b) cout<<"Bob"<<endl;
else cout<<"Tie"<<endl;
}
return 0;
}
思路:注意题目输出的要求,他说随便找一个能放到i前面的就好了,本身就在i前面但满足题意的也可以当做一个解。
那我们先对原二元组按照w排序,现在w是从小到大了。然后我们只用顺序遍历一遍,因为前面的w肯定比当前的小,遍历的时候记录最小的h及其原下标即可。如果前面有最小h小于当前h的就直接用那个记录的原下标。
但是因为有重复的w存在,所以会出现下面这种情况:
1 5
2 3
2 6
2 7
如果在i=2的时候改变原下标(变成id=2),那么后面两个w=2的都会找不到解。所以遇到后面还有w一样的要先判断完再更新。
view code#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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 endl '\n'
#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 = 2e5+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} };
typedef struct Pos
{
ll w;
ll h;
ll id;
bool operator < (const Pos &a) const
{
if(w!=a.w)
return w<a.w;
return h<a.h;
}
}P;
P a[maxn];
ll Ans[maxn];
int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n = read();
rep(i,1,n)
{
ll w = read(), h = read();
if(w>h) swap(w,h);
a[i].w = w;
a[i].h = h;
a[i].id = i;
}
sort(a+1,a+1+n);
ll mih = inf;
ll miw = inf;
ll cur = 0;
rep(i,1,n)
{
if(a[i].h>mih&&a[i].w>miw) Ans[a[i].id] = cur;
else
{
int p = i+1;
Ans[a[i].id] = -1;
while(p<=n&&a[p].w==a[i].w)
{
if(a[p].w>miw&&a[p].h>mih) Ans[a[p].id] = cur;
else Ans[a[p].id] = -1;
p++;
}
cur = a[i].id;
mih = a[i].h;
miw = a[i].w;
i = p-1;
}
}
rep(i,1,n) cout<<Ans[i]<<' '; cout<<endl;
}
return 0;
}