Gym 102433
B - Perfect Flush
答案:
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
#define PII pair<int,int>
const int mod = 11092019;
const int N = 2e5 + 10;
const int M = 222;
using namespace std;
int dp[N];
int last[N];
set<int> mp;
set<PII>st;
queue<int>ans;
bool vis[N];
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>dp[i];
last[dp[i]]=i;
}
for(int i=1;i<=m;i++){
mp.insert(last[i]);
}
int l=1;
int r=*mp.begin();
for(int i=l;i<=r;i++){
st.insert(PII {dp[i],i});
}
for(int i=1;i<=m;i++){
PII pos=*st.begin();
while(vis[pos.first]){
st.erase(st.begin());
pos=*st.begin();
}
ans.push(pos.first);
vis[pos.first]=1;
while(l<=pos.second){
if(st.count(PII {dp[l],l})) st.erase(PII {dp[l],l});
l++;
}
mp.erase(last[pos.first]);
int key=*mp.begin();
while(r<key){
r++;
st.insert(PII {dp[r],r});
}
}
bool flag=0;
while(!ans.empty()){
if(!flag){
cout<<ans.front();
flag=1;
}
else cout<<" "<<ans.front();
ans.pop();
}
cout<<endl;
return 0;
}
C - Coloring Contention
答案:
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
#define PII pair<int,int>
#define pb push_back
const int mod = 11092019;
const int N = 2e6 + 10;
const int M = 2222;
using namespace std;
int n,m;
int dis[N];
vector<int>mp[N];
void _clear(){
memset(dis,-1,sizeof(dis));
// for(int i=0;i<=n;i++){
// dis[i]=0;
// mp[i].clear();
// }
}
void init(){
for(int i=0;i<m;i++){
int u,v;
cin>>u>>v;
mp[u].pb(v);
mp[v].pb(u);
}
}
void Dijkstra(){
queue<int>dp;
dp.push(1);
dis[1]=0;
while(!dp.empty()){
int pos=dp.front();
dp.pop();
if(pos==n){
cout<<dis[n]-1<<endl;
return ;
}
for(int i=0;i<(int)mp[pos].size();i++){
int key=mp[pos][i];
if(dis[key]==-1){
dis[key]=dis[pos]+1;
dp.push(key);
}
}
}
}
int main()
{
cin>>n>>m;
_clear();
init();
Dijkstra();
return 0;
}
D - Dividing By Two
答案:
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
const int M = 30;
using namespace std;
ll hp[N];
ll dis[N];
ll judge(ll a,ll b){
if(a==b) return 0;
else if(a<b) return b-a;
else if(a%2) return judge(a+1,b)+1;
return judge(a/2,b)+1;
}
int main()
{
ll a,b;
cin>>a>>b;
cout<<judge(a,b)<<endl;
}
E - Rainbow Strings
答案:
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
const int mod = 11092019;
const int N = 1e5 + 10;
const int M = 222;
using namespace std;
int dp[M];
int main()
{
string s;
cin>>s;
//memset(dp,1,sizeof(dp));
int len=s.length();
for(int i=0;i<len;i++){
dp[s[i]]++;
}
ll ans=1;
for(int i='a';i<='z';i++){
ans*=dp[i]+1;
ans%=mod;
}
cout<<ans<<endl;
return 0;
}
I - Error Correction
答案:
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
#define PII pair<int,int>
#define pb push_back
const int mod = 11092019;
const int N = 2e6 + 10;
const int M = 1111;
using namespace std;
int n,m;
int dis[N];
int len;
char s[M][M];
bool vis[M];
int vp[N];
vector<int>mp[M];
bool judge(int x){
for(int i=0;i<(int)mp[x].size();i++){
if(!vis[mp[x][i]]){
vis[mp[x][i]]=1;
if(!vp[mp[x][i]]||judge(vp[mp[x][i]])){
vp[mp[x][i]]=x;
return true;
}
}
}
return false;
}
void init(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>s[i];
}
len=strlen(s[1]);
}
void check(){
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
int cnt=0;
for(int k=0;k<len;k++){
if(s[i][k]!=s[j][k]) cnt++;
if(cnt>2) break;
}
if(cnt==2){
mp[i].pb(j);
mp[j].pb(i);
}
}
}
}
void solve(){
int ans=0;
for(int i=1;i<=n;i++){
memset(vis,0,sizeof(vis));
if(judge(i)) ans++;
}
cout<<n-(ans>>1)<<endl;
}
int main()
{
init();
check();
solve();
return 0;
}
M - Maze Connect
答案:
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
#define PII pair<int,int>
const int mod = 11092019;
const int N = 1e4 + 10;
const int M = 2222;
using namespace std;
int n,m;
int l,r;
int mp[M][M];
//bool vis[N][N];
int dxy[4][2]={0,1,0,-1,1,0,-1,0};
queue<int>dx;
queue<int>dy;
void BFS(int X,int Y)
{
mp[X][Y]=1;
dx.push(X);
dy.push(Y);
while(!dx.empty())
{
int x=dx.front();
dx.pop();
int y=dy.front();
dy.pop();
for(int i=0;i<4;i++)
{
int xx=x+dxy[i][0];
int yy=y+dxy[i][1];
if(xx>=0&&xx<=l&&y>=0&&y<=r)
{
if(!mp[xx][yy])
{
mp[xx][yy]=1;
dx.push(xx);
dy.push(yy);
}
}
}
}
return;
}
int main()
{
cin>>n>>m;
char s;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>s;
if(s=='\\'){
mp[i*2][j*2]=1;
mp[i*2-1][j*2-1]=1;
}
else if(s=='/'){
mp[i*2-1][j*2]=1;
mp[i*2][j*2-1]=1;
}
else continue;
}
}
l=n*2+1;
r=m*2+1;
ll ans=-1;
for(int i=0;i<=l;i++){
for(int j=0;j<=r;j++){
if(!mp[i][j]){
BFS(i,j);
ans++;
}
}
}
cout<<ans<<endl;
return 0;
}