A. Strange Table
签到题,算出对应行列即可。
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 endl '
'
#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(), m = read(), x = read();
ll cols = x/n;
ll rows = x%n;
if(rows)
{
cout<<(rows-1)*m+cols+1<<endl;
}
else
{
cout<<(n-1)*m+cols<<endl;
}
}
return 0;
}
B. Partial Replacement
思路:贪心,非等到刚好k步或者苟不到下一个改变位时才变。
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 endl '
'
#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(), k = read();
string s;
cin>>s;
ll all = 0;
for(int i=0; i<s.size();i++) if(s[i]=='*') all++;
ll cnt = 0;
int flag = 1;
int id = -1;
int pre = -1;
for(int i=0; i<s.size(); i++)
{
if(s[i]=='*'&&flag) cnt++, flag = 0, pre = i,all--;
else if(s[i]=='*')
{
if(i-pre<k) id = i;
else if(i-pre==k) pre = i, cnt++, id = -1;
all--;
}
else
{
if(pre!=-1&&all&&i-pre>=k) pre = id, cnt++, id = -1;
}
}
if(id!=-1) cnt++;
cout<<cnt<<endl;
}
return 0;
}
C. Double-ended Strings
思路:数据量小,直接暴力,枚举a,b串各自的起始和终点下标al,ar,bl,br,看看这两个子串是否相等,若相等,我们就知道a和b要头部尾部要删掉多少个了,枚举更新答案即可。
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 endl '
'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ac cout<<ans<<endl
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} };
string a,b;
bool check(int al, int ar, int bl,int br)
{
string tmpa, tmpb;
for(int i=al; i<=ar; i++) tmpa += a[i];
for(int i=bl; i<=br; i++) tmpb += b[i];
if(tmpa==tmpb) return true;
return false;
}
int main()
{
int kase;
cin>>kase;
while(kase--)
{
cin>>a>>b;
ll ans = inf;
for(int al=0; al<a.size(); al++) for(int ar=al;ar<a.size();ar++ ) for(int bl=0; bl<b.size(); bl++) for(int br=bl;br<b.size();br++ )
{
if(check(al,ar,bl,br))
{
ans = min(ans,al+(int)a.size()-ar-1+bl+(int)b.size()-br-1);
}
}
if(ans==inf) ans = a.size()+b.size();
cout<<ans<<endl;
}
return 0;
}
D. Epic Transformation
首先统计各个不同数,然后只关心不同数的个数以及他们各自有多少个。这题可能有人一开始想对p个不同数按照所含个数进行排序,然后贪心先用小的去掉大的,但是“最大”一直在变化(原最大的可能抵消几次后次大的变成最大的了),所以这种方法不够贪。
这样考虑,当p=2时,只有两个不同数,相互匹配完剩下的那个即是答案。如1 2(这里数组表示不同数各自的个数),抵消完是0 1。
当p=3时,如1 2 3,多加进来的一个3,它可以在原来的抵消轮里面“插入”,就是每个1<->2的抵消变成1<->3, 3<->2,这样就使得在前i-1个数最优的情况下,第i个数也能达到最优,这个插入次数即是(min(a[i]/2, round)),round是前i-1个的匹配轮数,a[i]/2是因为每一轮插入时需要两个a[i]。这样就是最优贪心了。同时因为a[i-1]可能有剩,所以a[i]要先和a[i-1]抵消完再执行上述操作。
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 endl '
'
#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 each[maxn];
ll sum[maxn];
int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n = read();
rep(i,1,n) a[i] = read();
map<ll,ll> cnt;
rep(i,1,n) cnt[a[i]]++;
int p = 0;
for(auto it = cnt.begin(); it != cnt.end(); it ++)
{
each[++p] = it->se;
}
sort(each+1,each+1+p);
rep(i,1,p) a[i] = each[i];
if(p==1)
{
cout<<a[1]<<endl;
continue;
}
ll round = a[1];
a[2] -= a[1], a[1] = 0;
rep(i,3,n)
{
ll balance1 = a[i-1];
a[i] -= balance1;
ll balance2 = min(a[i]/2, round);
a[i] -= balance2*2;
round += balance1 + balance2;
}
cout<<a[p]<<endl;
}
return 0;
}
E. Restoring the Permutation
思路:贪心+优先队列。
当前q[i]如果大于q[i-1]的话,说明第i位是更新位,不能动,就是q[i], 同时区间([q[i-1]+1,q[i]-1])内的数都是可以用的,存入队列(因为后面的q只会更大)。
否则每次取队列中最大/最小的数即可。
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 endl '
'
#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 b[maxn];
ll c[maxn];
int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n = read();
rep(i,1,n) a[i] = read();
b[1] = a[1];
priority_queue<ll,vector<ll>, greater<ll> > q1;
rep(i,1,a[1]-1) q1.push(i);
rep(i,2,n)
{
if(a[i]>a[i-1])
{
rep(j,a[i-1]+1,a[i]-1) q1.push(j);
b[i] = a[i];
}
else
{
b[i] = q1.top();
q1.pop();
}
}
c[1] = a[1];
priority_queue<ll,vector<ll>, less<ll> > q2;
rep(i,1,a[1]-1) q2.push(i);
rep(i,2,n)
{
if(a[i]>a[i-1])
{
rep(j,a[i-1]+1,a[i]-1) q2.push(j);
c[i] = a[i];
}
else
{
c[i] = q2.top();
q2.pop();
}
}
rep(i,1,n) cout<<b[i]<<' ';cout<<endl;
rep(i,1,n) cout<<c[i]<<' '; cout<<endl;
}
return 0;
}