A. Working Week
思路:就是把n-3分成互相之差最大的三段,那么我们可以二分答案判断是否可行。
#include <bits/stdc++.h>//#include<iostream>//#include<string.h>//#include<math.h//#include<algorithm>
#define ll long long
#define db double
#define pii pair<int,int>
#define cf int _;cin>>_;while(_--)
#define de cout<<"---"<<endl;
#define mem(x,v) memset(x,v,sizeof(x))
#define L(x) x&(-x)
#define pb push_back//emplace_back//priority_queue <int,vector<int>,greater<int> > q;
#define INF 0x3f3f3f3f
#define endl '\n'
//function<int(int,int)> add = [&](int x,int y);
//#define x first
//#define y second
using namespace std;
const int mod=998244353;
int n;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
bool check(int res){
ll t=res+1;
t+=res*2+1;
return t<=n-4;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
cf{
cin>>n;
int l=0,r=n;
while(l<r){
int mid=(l+r+1)/2;
if(check(mid)){
l=mid;
}
else {
r=mid-1;
}
}
cout<<l<<endl;
}
}
B. Tea with Tangerines
思路:贪心可知最小的橘子皮大小就是a1,那么后面的的橘子皮就按最大2*a1-1去分,如果分到最后一块的大小不足a1那么可以和前面一块匀一匀,能保证这两块都大于等于a1。
#include <bits/stdc++.h>//#include<iostream>//#include<string.h>//#include<math.h//#include<algorithm>
#define ll long long
#define db double
#define pii pair<int,int>
#define cf int _;cin>>_;while(_--)
#define de cout<<"---"<<endl;
#define mem(x,v) memset(x,v,sizeof(x))
#define L(x) x&(-x)
#define pb push_back//emplace_back//priority_queue <int,vector<int>,greater<int> > q;
#define INF 0x3f3f3f3f
#define endl '\n'
//function<int(int,int)> add = [&](int x,int y);
//#define x first
//#define y second
using namespace std;
const int mod=998244353;
int n;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
int main(){
ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
cf{
cin>>n;
vector<int>a(n);
for(int i=0;i<n;i++)cin>>a[i];
ll res=0;
for(int i=1;i<n;i++){
res+=(a[i]-1)/(a[0]*2-1);
}
cout<<res<<endl;
}
}
C. Phase Shift
思路:从前往后模拟过程,尽量从字典序小的字符转移而来,如果满足j!=ai,j没有转移为其他字符且ai不断转移没有经过j或者经过j但是步数为25,即能满足j可以转移为ai。
#include <bits/stdc++.h>//#include<iostream>//#include<string.h>//#include<math.h//#include<algorithm>
#define ll long long
#define db double
#define pii pair<int,int>
#define cf int _;cin>>_;while(_--)
#define de cout<<"---"<<endl;
#define mem(x,v) memset(x,v,sizeof(x))
#define L(x) x&(-x)
#define pb push_back//emplace_back//priority_queue <int,vector<int>,greater<int> > q;
#define INF 0x3f3f3f3f
#define endl '\n'
//function<int(int,int)> add = [&](int x,int y);
//#define x first
//#define y second
using namespace std;
const int mod=998244353;
int n;
int res[30];
int f[30];
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
int main(){
ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
cf{
mem(res,0);
mem(f,0);
cin>>n;
string a;
cin>>a;
for(int i=0;i<n;i++){
if(res[a[i]-'a']){
cout<<char(res[a[i]-'a']);
}
else{
for(int j='a';j<='z';j++){
if(j==a[i]||f[j-'a']){
continue;
}
int t=a[i],tt=0;
while(f[t-'a']&&t!=j){
tt++;
t=f[t-'a'];
}
if(t==j){
if(tt==25){
res[a[i]-'a']=j;
f[j-'a']=a[i];
break;
}
else{
continue;
}
}
else {
res[a[i]-'a']=j;
f[j-'a']=a[i];
break;
}
}
cout<<(char)res[a[i]-'a'];
}
}
cout<<endl;
}
}
D. Meta-set
思路:由于可以通过两张牌计算出可以使这三张牌成为集合的第三张,所以可以求出哪些牌可以成为“中心牌”,接下来枚举“中心牌”,计算其为“中心牌”的集合中选两个集合的方案数即可。
#include <bits/stdc++.h>//#include<iostream>//#include<string.h>//#include<math.h//#include<algorithm>
#define ll long long
#define db double
#define pii pair<int,int>
#define cf int _;cin>>_;while(_--)
#define de cout<<"---"<<endl;
#define mem(x,v) memset(x,v,sizeof(x))
#define L(x) x&(-x)
#define pb push_back//emplace_back//priority_queue <int,vector<int>,greater<int> > q;
#define INF 0x3f3f3f3f
#define endl '\n'
//function<int(int,int)> add = [&](int x,int y);
//#define x first
//#define y second
using namespace std;
const int mod=998244353;
int n,k;
int a[1005][25];
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
int main(){
ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
cin>>n>>k;
map<vector<int>,ll>ans;
for(int i=0;i<n;i++){
vector<int>t(k);
for(int j=0;j<k;j++){
cin>>a[i][j];
t[j]=a[i][j];
}
ans[t]++;
}
map<vector<int>,int>res;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
vector<int>t(k);
for(int h=0;h<k;h++){
if(a[i][h]==a[j][h])t[h]=a[i][h];
else if(a[i][h]!=0&&a[j][h]!=0){
t[h]=0;
}
else if(a[i][h]!=1&&a[j][h]!=1){
t[h]=1;
}
else{
t[h]=2;
}
}
res[t]++;
}
}
ll anss=0;
for(auto i:res){
anss+=ans[i.first]*i.second*(i.second-1)/2;
}
cout<<anss<<endl;
}
本文提供了四道算法竞赛题目的解答思路及代码实现,包括工作周问题的最优划分、橘子皮分割策略、字符串相位偏移模拟以及元集合计数等。
135





