Make Equal With Mod - SMUOJ
可以选择任意一个数(不小于2),让数组中的数换成对这个数取模的结果。要让这个数组中的数相等,可以看出如果数组中0和1同时存在那么这两个数不可能操作后相等,那么考虑对其他数操作,假设全变成0,那么需要每次对最大数取模,如果全部变成1,需要每次对最大数-1取模。如果两个数相邻并且1存在,必然无法全部相同。
#include<bits/stdc++.h>
#define endl '\n'
#define mk make_pair
#define int long long
using namespace std;
typedef pair<int, int> PII;
const int N = 2e5+10;
int n,m,k;
int a[N];
void sovle(){
map<int,int>v;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
v[a[i]]++;
}
if(v[1]==n){
cout<<"YES"<<endl;
return;
}else{
for(auto ed:v){
if(v[ed.first]&&v[ed.first-1]&&v[1]){
cout<<"NO"<<endl;
return;
}
}
cout<<"YES"<<endl;
}
}
signed main()
{
ios::sync_with_stdio(false), cin.tie(0),cout.tie(0);
int t = 1;
cin>>t;
while (t--){
sovle();
}
return 0;
}
Contest Problem Details - SMUOJ
花里胡哨的,就是一个二分图匹配问题。貌似也可以贪心
#include<bits/stdc++.h>
#define endl '\n'
#define mk make_pair
#define int long long
using namespace std;
typedef pair<int, int> PII;
const int N = 2e5+10;
int n,m,k;
int p[1005];
map<int,int>xx;
bool t[1005];
struct node{
PII x;
vector<int>v;
}a[1005];
int match(int u){
for(int i=0;i<a[u].v.size();i++){
int f=a[u].v[i];
if(!t[f]){
t[f]=1;
if(p[f]==-1||match(p[f])){
p[f]=u;
xx[u]=f;
return 1;
}
}
}
return 0;
}
void sovle(){
cin>>n;
xx.clear();
for(int i=0;i<1005;i++){
p[i]=-1;
a[i].v.clear();
}
for(int i=0;i<n;i++){
int l,r;
cin>>l>>r;
a[i].x.first=l;
a[i].x.second=r;
for(int j=l;j<=r;j++) a[i].v.push_back(j);
}
for(int i=0;i<n;i++) {
memset(t,0,sizeof(t));
match(i);
}
for(int i=0;i<n;i++){
cout<<a[i].x.first<<' '<<a[i].x.second<<' '<<xx[i]<<endl;
}
}
signed main()
{
ios::sync_with_stdio(false), cin.tie(0),cout.tie(0);
int t = 1;
cin>>t;
while (t--){
sovle();
}
return 0;
}
Buy an Integer - SMUOJ
一开始没看到只能买一个,还以为是什么贪心的题。
因为只能买一个,并且答案具有单调性,直接二分找
#include<bits/stdc++.h>
#define endl '\n'
#define mk make_pair
#define int long long
using namespace std;
typedef pair<int, int> PII;
const int N = 2e5+10;
int n,m,k;
int a[N];
int check(int x){
int ans=1;
while(x){
x/=10;
if(!x) return ans;
ans++;
}return ans;
}
void sovle(){
cin>>n>>m>>k;
int l=1,r=1e9,ans=0;
while(l<=r){
int mid=(l+r)>>1;
if(n*mid+check(mid)*m<=k){
ans=mid;
l=mid+1;
}else r=mid-1;
}
cout<<ans<<endl;
}
signed main()
{
ios::sync_with_stdio(false), cin.tie(0),cout.tie(0);
int t = 1;
//cin>>t;
while (t--){
sovle();
}
return 0;
}
String Formation - SMUOJ
用双端队列模拟一下就好了,也不需要每次都反转字符串,只需要颠倒放入的规则就行了
#include<bits/stdc++.h>
#define endl '\n'
#define mk make_pair
#define int long long
#define ll long long
using namespace std;
typedef pair<int, int> PII;
const int N = 2e5+7;
const int mod = 1e9+7;
int n,m,k;
int a[N];
void sovle(){
string s;cin>>s;
deque<char>ss;
for(int i=0;i<s.size();i++){
ss.push_back(s[i]);
}
cin>>m;
bool ok=0;
while(m--){
int x,y;
cin>>x;
if(x==1) ok=!ok;
else{
char ch;
cin>>y>>ch;
if(y==1){
if(!ok) ss.push_front(ch);
else ss.push_back(ch);
}else{
if(!ok) ss.push_back(ch);
else ss.push_front(ch);
}
}
}
if(ok){
while(ss.size()){
cout<<ss.back();ss.pop_back();
}
}else{
while(ss.size()){
cout<<ss.front();ss.pop_front();
}
}
}
signed main()
{
ios::sync_with_stdio(false), cin.tie(0),cout.tie(0);
int t = 1;
//cin>>t;
while (t--){
sovle();
}
return 0;
}
Bouquet - SMUOJ
排列组合知识,一个序列(0到n)的全排列为2的n次方
#include<bits/stdc++.h>
#define endl '\n'
#define mk make_pair
#define int long long
#define ll long long
using namespace std;
typedef pair<int, int> PII;
const int N = 2e5+7;
const int mod = 1e9+7;
int n,m,k;
int a[N];
int dp[N][15];
int qmi(int x,int y){
int res=1;
while(y){
if(y&1) res=(res*x)%mod;
y>>=1;
x=(x*x)%mod;
}return res;
}
int C(int a,int b){
int res=1;
for(int i=1,j=a;i<=b;i++,j--){
res=res*j%mod;
res=res*qmi(i,mod-2)%mod;
}
return res;
}
void sovle(){
cin>>n>>m>>k;
int sum=0;
cout<<((qmi(2,n)-1-C(n,m)+mod)%mod-C(n,k)+mod)%mod<<endl;
}
signed main()
{
ios::sync_with_stdio(false), cin.tie(0),cout.tie(0);
int t = 1;
//cin>>t;
while (t--){
sovle();
}
return 0;
}
String Cards - SMUOJ
首先是对字符串进行排序,让a+b<b+a,具体证明不会...有点类似于下面的。
要让字符串最小,则在保证低位最小的情况下,高位尽量小。
如果对字符串正着遍历,则不能保证高位尽量小,所以我们从后面倒着遍历。
具体参考【ABC225F】String Cards - Cry_For_theMoon - 博客园 (cnblogs.com)
#include<bits/stdc++.h>
#define endl '\n'
#define mk make_pair
#define int long long
#define ll long long
using namespace std;
typedef pair<int, int> PII;
const int N = 2e5+7;
const int mod = 1e9+7;
int n,m,k;
string s[55],d="zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
string dp[55][55];
int cmp(string a,string b){
return a+b<b+a;
}
void sovle(){
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>s[i];
for(int i=1;i<=n+1;i++){
for(int j=1;j<=n+1;j++){
dp[i][j]=d;
}
}
sort(s+1,s+n+1,cmp);
dp[n+1][0]="";
for(int i=n;i>0;i--){
for(int j=1;j<=n-i+1;j++){
dp[i][j]=min(dp[i+1][j],s[i]+dp[i+1][j-1]);
}
}
cout<<dp[1][k]<<endl;
}
signed main()
{
ios::sync_with_stdio(false), cin.tie(0),cout.tie(0);
int t = 1;
//cin>>t;
while (t--){
sovle();
}
return 0;
}