生活随笔
收集整理的這篇文章主要介紹了
Codeforces Round #744 (Div. 3)【A-E1】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
后面的題沒做,不想補了。因為最近才開始在cf上刷題,打比賽。
主要是英語太差,div3目前打算到可以很快的AC前5道,再開始攻克前6道。
以此類推,慢慢的進步吧。
目錄
- A. Casimir's String Solitaire【水題】
- B. Shifting Sort【難度: 一般 / 知識點:用隊列模擬左移】
- C. Ticks【難度: 一般 / 知識點: 暴力枚舉】
- D. Productive Meeting【難度: 一般 / 知識點: 貪心/優先隊列 】
- E1. Permutation Minimization by Deque【難度: 簡單 / 知識點:雙端隊列模擬】
A. Casimir’s String Solitaire【水題】
https://codeforces.com/contest/1579/problem/A
判斷B的數量等不等于A的數量加上C的數量
#include<bits/stdc++.h>
using namespace std
;
int main(void)
{int t
; cin
>>t
;while(t
--){string s
; cin
>>s
;int cnt1
=0,cnt2
=0,cnt3
=0;for(int i
=0;i
<s
.size();i
++){if(s
[i
]=='A') cnt1
++;else if(s
[i
]=='B') cnt2
++;else cnt3
++;}if(cnt2
==cnt1
+cnt3
) puts("YES");else puts("NO");}
}
B. Shifting Sort【難度: 一般 / 知識點:用隊列模擬左移】
https://codeforces.com/contest/1579/problem/B
思路: 用一個B數組保存題目給的A數組,排序B數組。
用隊列q來保存目前A數組。
遍歷數組,將隊列的頭部元素和數組B的元素一一比較即可。
相等的話不用移動,不相等的話就模擬左移,直到到達指定的位置。
#include<bits/stdc++.h>
using namespace std
;
typedef long long int LL
;
const int N
=1e5*2+10;
LL a
[N
],b
[N
];
int main(void)
{int t
; cin
>>t
;while(t
--){int n
; cin
>>n
;queue
<int>q
;for(int i
=0;i
<n
;i
++) cin
>>a
[i
],b
[i
]=a
[i
],q
.push(a
[i
]);sort(b
,b
+n
);vector
<int>s1
,s2
,s3
;for(int i
=0;i
<n
;i
++){if(b
[i
]!=q
.front()){int cnt
=0;while(q
.front()!=b
[i
]) {cnt
++;int temp
=q
.front(); q
.pop();q
.push(temp
);}q
.pop();s1
.push_back(i
+1),s2
.push_back(n
),s3
.push_back(cnt
);}else q
.pop();}cout
<<s1
.size()<<endl
;for(int i
=0;i
<s1
.size();i
++) cout
<<s1
[i
]<<" "<<s2
[i
]<<" "<<s3
[i
]<<endl
;}return 0;
}
C. Ticks【難度: 一般 / 知識點: 暴力枚舉】
https://codeforces.com/contest/1579/problem/C
暴力枚舉所有可以畫的合法的對勾,將其對勾的每一個點標記為1。
最后看所有的" * " 的狀態是不是都是 1 即可
#include<bits/stdc++.h>
using namespace std
;
const int N
=1e5*2+10;
string s
[N
];
int st
[25][25],backup
[25][25];
int n
,m
,k
;
void dfs(int xx
,int yy
)
{for(int len
=20;len
>=k
;len
--){int x
=xx
+len
,y
=yy
+len
;if(x
>=n
||y
>=m
) continue;if(s
[x
][y
]!='*') continue;memcpy(backup
,st
,sizeof st
);bool flag
=true;st
[x
][y
]=1;for(int i
=1;i
<=len
;i
++){int tempx
=x
-i
,tempy
=y
-i
;if(tempx
>=n
||tempx
<0||tempy
>=m
||tempy
<0) flag
=false;if(s
[tempx
][tempy
]!='*') flag
=false;st
[tempx
][tempy
]=1;tempx
=x
-i
,tempy
=y
+i
;if(tempx
>=n
||tempx
<0||tempy
>=m
||tempy
<0) flag
=false;if(s
[tempx
][tempy
]!='*') flag
=false;st
[tempx
][tempy
]=1;}if(!flag
) memcpy(st
,backup
,sizeof st
);}
}
int main(void)
{int t
; cin
>>t
;while(t
--){cin
>>n
>>m
>>k
;memset(st
,0,sizeof st
);for(int i
=0;i
<n
;i
++) cin
>>s
[i
];for(int i
=0;i
<n
;i
++)for(int j
=0;j
<m
;j
++)if(s
[i
][j
]=='*') dfs(i
,j
);bool ans
=true;for(int i
=0;i
<n
;i
++)for(int j
=0;j
<m
;j
++)if(s
[i
][j
]=='*'&&st
[i
][j
]==0) ans
=false;if(ans
) puts("YES");else puts("NO");}return 0;
}
D. Productive Meeting【難度: 一般 / 知識點: 貪心/優先隊列 】
https://codeforces.com/contest/1579/problem/D
#include<bits/stdc++.h>
using namespace std
;
typedef long long int LL
;
typedef pair
<int,int> PII
;
const int N
=1e5*2+10;
LL a
[N
];
int main(void)
{int t
; cin
>>t
;while(t
--){int n
; cin
>>n
;vector
<pair
<int,int>>ans
,ve
;for(int i
=0;i
<n
;i
++) cin
>>a
[i
],ve
.push_back({a
[i
],i
});sort(ve
.begin(),ve
.end());priority_queue
<PII
>s1
;priority_queue
<PII
, vector
<PII
>, greater
<PII
> >s2
;for(int i
=0;i
<n
/2;i
++) if(ve
[i
].first
) s2
.push(ve
[i
]);for(int i
=n
/2;i
<n
;i
++) if(ve
[i
].first
) s1
.push(ve
[i
]);while(s1
.size()&&s2
.size()){auto temp1
=s1
.top(); s1
.pop();auto temp2
=s2
.top(); s2
.pop();ans
.push_back({temp1
.second
,temp2
.second
});temp1
.first
--;temp2
.first
--;if(temp1
.first
>=1) s1
.push(temp1
);if(temp2
.first
>=1) s2
.push(temp2
);}while(s1
.size()>=2){auto temp1
=s1
.top(); s1
.pop();auto temp2
=s1
.top(); s1
.pop();temp1
.first
--;temp2
.first
--;ans
.push_back({temp1
.second
,temp2
.second
});if(temp1
.first
>=1) s1
.push(temp1
);if(temp2
.first
>=1) s1
.push(temp2
);}while(s2
.size()>=2){auto temp1
=s2
.top(); s2
.pop();auto temp2
=s2
.top(); s2
.pop();temp1
.first
--;temp2
.first
--;ans
.push_back({temp1
.second
,temp2
.second
});if(temp1
.first
>=1) s2
.push(temp1
);if(temp2
.first
>=1) s2
.push(temp2
);}cout
<<ans
.size()<<endl
;for(int i
=0;i
<ans
.size();i
++) cout
<<ans
[i
].first
+1<<" "<<ans
[i
].second
+1<<endl
;}return 0;
}
其實不用那么的麻煩,直接用一個大根堆,每次取堆頂的倆元素即可。
#include<bits/stdc++.h>
using namespace std
;
typedef long long int LL
;
typedef pair
<int,int> PII
;
const int N
=1e5*2+10;
LL a
[N
];
int main(void)
{int t
; cin
>>t
;while(t
--){int n
; cin
>>n
;vector
<pair
<int,int>>ans
,ve
;for(int i
=0;i
<n
;i
++) cin
>>a
[i
],ve
.push_back({a
[i
],i
});sort(ve
.begin(),ve
.end());priority_queue
<PII
>s1
;for(int i
=0;i
<n
;i
++) if(ve
[i
].first
) s1
.push(ve
[i
]);while(s1
.size()>=2){auto temp1
=s1
.top(); s1
.pop();auto temp2
=s1
.top(); s1
.pop();temp1
.first
--;temp2
.first
--;ans
.push_back({temp1
.second
,temp2
.second
});if(temp1
.first
>=1) s1
.push(temp1
);if(temp2
.first
>=1) s1
.push(temp2
);}cout
<<ans
.size()<<endl
;for(int i
=0;i
<ans
.size();i
++) cout
<<ans
[i
].first
+1<<" "<<ans
[i
].second
+1<<endl
;}return 0;
}
E1. Permutation Minimization by Deque【難度: 簡單 / 知識點:雙端隊列模擬】
https://codeforces.com/contest/1579/problem/E1
用雙端隊列模擬即可。
#include<bits/stdc++.h>
using namespace std
;
typedef long long int LL
;
const int N
=1e5*2+10;
LL a
[N
];
int main(void)
{int t
; cin
>>t
;while(t
--){int n
; cin
>>n
;for(int i
=0;i
<n
;i
++) cin
>>a
[i
];deque
<int>q
;for(int i
=0;i
<n
;i
++) {if(q
.size()==0) q
.push_back(a
[i
]);else {if(q
.front()>=a
[i
]) q
.push_front(a
[i
]);else q
.push_back(a
[i
]);}}vector
<int>ans
;while(q
.size()) ans
.push_back(q
.front()),q
.pop_front();for(int i
=0;i
<ans
.size();i
++) {if(i
) cout
<<" ";cout
<<ans
[i
];}cout
<<endl
;}return 0;
}
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的Codeforces Round #744 (Div. 3)【A-E1】的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。