#include<bits/stdc++.h>
using namespace std;
void solve(){
int a,b,c;
cin >> a>> b >> c;
if(a < b && b < c) cout <<"STAIR" << endl;
else if(a < b && c < b) cout << "PEAK" << endl;
else cout << "NONE" << endl;
}
int main()
{
int t;
cin >> t;
while(t--){
solve();
}
}
#include<bits/stdc++.h>
using namespace std;
void solve(){
int n;
cin >> n;
for(int i = 1;i <= n;i++){
string s = "";
for(int j= 1;j <= n;j++){
if(i % 2 == 1){
if(j%2 == 1) s+="##";
else s+="..";
} else {
if(j%2 == 1) s+="..";
else s+="##";
}
}
cout << s <<endl;
cout << s << endl;
}
}
int main()
{
int t;
cin >> t;
while(t--){
solve();
}
}
#include<bits/stdc++.h>
using namespace std;
void solve(){
string s;
cin >> s;
string ss="";
int hour = (s[0]-'0')*10+s[1]-'0';
if(hour >= 12) ss = "PM";
else ss = "AM";
if(hour==12) cout <<hour << ':' << s[3] <<s[4] <<" " << ss<<endl;
else if(hour > 12) {
if(hour%12<10)
cout <<0<<hour%12 << ':' << s[3] <<s[4] <<" " << ss<<endl;
else cout <<hour%12 << ':' << s[3] <<s[4] <<" " << ss<<endl;
}
else if(hour == 0) cout <<12 << ':' << s[3] <<s[4] <<" " << ss<<endl;
else cout << s <<" " <<ss<<endl;
}
int main()
{
int t;
cin >> t;
while(t--){
solve();
}
}
枚举2000以内的二进制数据,依次判断即可
#include<bits/stdc++.h>
using namespace std;
int check(int x){
while(x){
int t = x%10;
x/=10;
if(t!=1 && t!= 0) return 0;
}
return 1;
}
int a[] = {10,11,100,101,110,111,1000,1001,1010,1011,1100,1101,1110,1111};
void solve(){
int n;
cin >> n;
for(int i = 0;i < 14;i++){
if(check(n)){
puts("yes");
return;
}
while(n%a[i] == 0){
n/=a[i];
}
}
if(check(n)) puts("yes");
else puts("no");
}
int main()
{
int t;
cin >> t;
while(t--){
solve();
}
}
先预处理n的所有因子,在暴力判断每一个因子是否符合答案即可
#include<bits/stdc++.h>
using namespace std;
void solve(){
int n;
cin >> n;
string s;
cin >> s;
vector<int> q;
for(int i = 1;i <= n;i++){
if(n%i == 0) q.push_back(i);
}
for(int i = 0;i < q.size();i++){
if(q[i] == n){
cout << q[i] <<endl;
return;
}
int cnt = 0;
if(n / q[i] == 2){
for(int j = 0,k = n/2;j<n/2&&k<n;j++,k++){
if(s[j] != s[k]) cnt++;
if(cnt > 2){
break;
}
}
if(cnt<=1){
cout << q[i] <<endl;
return;
}
else continue;
}
string ss;
string a = s.substr(0,q[i]);
string b = s.substr(q[i],q[i]);
string c = s.substr(2*q[i],q[i]);
if(a!=b && b!=c && a != c) continue;
if(a==b || a==c) ss = a;
else ss = b;
int k = 0;
for(int j = 0;j < n;j++){
if(ss[k++] != s[j]){
cnt++;
if(cnt > 1){
break;
}
}
if(k%q[i]==0) k = 0;
}
if(cnt <= 1){
cout << q[i] <<endl;
return;
}
}
}
int main()
{
int t;
cin >> t;
while(t--){
solve();
}
}
当一个点扩展为两个子节点时,叶子结点会增加1,因此c=a+1,不满足则输出-1
再贪心扩展树,为了使深度最小,一定要使得树接近二叉树,因此优先扩展需要两个儿子的结点,最后再扩展只有一个儿子的结点
#include<bits/stdc++.h>
using namespace std;
void solve(){
int a,b,c;
cin >> a>> b >> c;
if(c != a+1){
cout << -1 <<endl;
return;
}
priority_queue<int,vector<int>,greater<int> > q;
q.push(0);
for(int i = 0;i < a;i++){
int t = q.top();
q.pop();
q.push(t+1);
q.push(t+1);
}
for(int i = 0;i < b;i++){
int t = q.top();
q.pop();
q.push(t+1);
}
int ans = 0;
while(!q.empty()){
ans = max(ans,q.top());
q.pop();
}
cout << ans <<endl;
}
int main()
{
int t;
cin >> t;
while(t--){
solve();
}
}
状态压缩dp,将可以相邻连接的歌之间连一条双向边,dp[i][j]表示当前状态为i,且该歌链的最后一个为j,进行状态转移,当j与k有边且k在状态i中没有被选,则可以转移到dp[i|(1<<k)][k].
#include<bits/stdc++.h>
using namespace std;
string g[20],w[20];
int e[20][20];
int dp[(1<<17)+10][20];
void solve(){
memset(e,0,sizeof(e));
memset(dp,0,sizeof(dp));
int n;
cin >> n;
for(int i = 0;i < n;i++) cin >> g[i] >> w[i];
for(int i = 0;i < n;i++){
for(int j = i+1;j <n;j++){
if(g[i] == g[j] || w[i] == w[j]) e[i][j] = e[j][i] = 1;
}
}
for(int i = 0;i < n;i++){
dp[(1<<i)][i] = 1;
}
for(int i = 0;i < (1<<n);i++){
for(int j = 0;j < n;j++){
if(dp[i][j]){
for(int k = 0;k < n;k++){
if((i>>k)&1) continue;
if(e[j][k]) dp[(1<<k)|i][k] = 1;
}
}
}
}
int ans = 0;
for(int i = 0;i < (1<<n);i++){
for(int j = 0;j < n;j++){
if(dp[i][j]) ans = max(ans,__builtin_popcount(i));
}
}
cout << n - ans << endl;
}
int main()
{
int t;
cin >> t;
while(t--){
solve();
}
}