1128
8皇后问题:任意两个皇后不能在同一个竖线、横线、斜线上
判断是否是N皇后成立
#include
#include
int main(){
int k;
scanf("%d", &k);
for(int i=0;i
1129 STL
题目大意: 根据物品被用户接受的次数来计算推荐指数。
一开始用的map,因为map的值不好排序,又转化到vector,再用sort排序。这样会超时。
#include
#include
#include
参考柳婼 の blog,应该重载set运算符:
#include
#include
using namespace std;
int book[50001];
struct node {
int value, cnt;
bool operator < (const node &a) const {
return (cnt != a.cnt) ? cnt > a.cnt : value < a.value;
}
};
int main() {
int n, k, num;
scanf("%d%d", &n, &k);
set s;
for (int i = 0; i < n; i++) {
scanf("%d", &num);
if (i != 0) {
printf("%d:", num);
int tempCnt = 0;
for(auto it = s.begin(); tempCnt < k && it != s.end(); it++) {
printf(" %d", it->value);
tempCnt++;
}
printf("\n");
}
auto it = s.find(node{num, book[num]});
if (it != s.end()) s.erase(it);
book[num]++;
s.insert(node{num, book[num]});
}
return 0;
}
1130 中缀表达式
题目大意: 给出二叉树,输出中缀表达式 ,并加上括号表示运算的优先级
- syntax:语法
- parentheses:圆括号
- precedences:优先
题目中说 data is a string of no more than 10 characters
当我数组设为10时就出现了运行时错误,将数组范围扩大到15就全对了。。
#include
#include
#include
#include
using namespace std;
typedef struct node{
char data[15];
int left,right;
}node;
int n;
vector v;
bool table[100] = {false};
string ans = "";
int root;
void inorder(int x){
if(v[x].left != -1){
if( x!= root ) ans += "(";
inorder(v[x].left);
}else if(v[x].right != -1 && x!= root){
ans += "(";
}
ans += v[x].data;
if(v[x].right != -1){
inorder(v[x].right);
if( x!= root ) ans += ")";
}
return;
}
int main(){
cin>>n;
v.resize(n+5);
for(int i=1;i<=n;i++){
char d[15];
int l,r;
cin>>d>>l>>r;
strcpy(v[i].data, d);
v[i].left = l;
v[i].right = r;
table[l] = table[r] = true;
}
//首先找到根节点
for(int i=1; i<=n; i++){
if(table[i] == false){
root = i;
break;
}
}
inorder(root);
cout<
1131 DFS
题目大意: 给出乘客的起点,找到途经停站最少的路线;如果经停站一样多,则取需要换乘线路次数最少的路线。
- 站点可能成圈
- 没有单个站点作为线路
- 不同线路无重复的一段路线
- 每个站点最多同时在5条线路上
#include
#include
#include