图像旋转
实现
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n,m,a[N][N];
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
}
}
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
cout<<a[j][m+1-i]<<" ";
}
cout<<endl;
}
}
数字排序
实现
#include <bits/stdc++.h>
using namespace std;
const int N =1010;
int n;
map<int,int> a;
struct node
{
int key,val;
}nodes[N];
bool cmp(node a,node b)
{
if(a.val!=b.val) return a.val>b.val;
else return a.key<b.key;
}
int main()
{
int t;
int cnt = 0;
scanf("%d\n",&n);
while(n--)
{
scanf("%d",&t);
a[t]++;
}
for(map<int,int>::iterator it = a.begin();it!=a.end();it++)
{
nodes[cnt].key = it->first;
nodes[cnt].val = it->second;
cnt++;
}
sort(nodes,nodes+cnt,cmp);
for(int i=0;i<cnt;i++)
{
cout<<nodes[i].key<<" "<<nodes[i].val<<endl;
}
}
节日
实现
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int months[13] = {
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
int is_leap(int year)
{
if(year % 400 == 0 || (year % 4 == 0 && year % 100)) return 1;
return 0;
}
int get_days(int year, int month)
{
if(month == 2) return months[month] + is_leap(year);
return months[month];
}
int main()
{
int a, b, c, y1, y2;
cin >> a >> b >> c >> y1 >> y2;
int days = 0;
for(int year = 1850; year <= y2; year ++ )
for(int month = 1; month <= 12; month ++ )
{
if(year >= y1 && month == a)
{
int w = (1 + days) % 7;
int cnt = 0;
for(int d = 1; d <= get_days(year, month); d ++ )
{
if(w == c - 1)
cnt ++ ;
if(cnt == b)
{
printf("%04d/%02d/%02d\n", year, month, d);
break;
}
w = (w + 1) % 7;
}
if(cnt < b)
{
puts("none");
}
}
days += get_days(year, month);
}
return 0;
}
网络延时
思路
求树的直径的两种方法
- 两次dfs:首先从任意节点 开始进行第一次 DFS,到达距离其最远的节点,记为 z z z ,然后再从 z z z 开始做第二次 DFS,到达距离 z z z 最远的节点,记为 z ′ z' z′ ,则 z z z和 z ′ z' z′之间的距离即为树的直径。
- 树形dp:我们记录当1为树的根时,每个节点作为子树的根向下所能延伸的最远距离 d 1 d1 d1 ,和次远距离 d 2 d2 d2,那么直径就是所有 d 1 + d 2 d1+d2 d1+d2的最大值。
实现
- 两次dfs:
#include <bits/stdc++.h>
using namespace std;
const int N = 20000 + 10;
int n,m ,c, d[N];
vector<int> E[N];
void dfs(int u, int fa) {
for (int v : E[u]) {
if (v == fa) continue;
d[v] = d[u] + 1;
if (d[v] > d[c]) c = v;
dfs(v, u);
}
}
int main() {
scanf("%d %d", &n,&m);
for (int i = 2; i <= n; i++) {
int u;
scanf("%d", &u);
E[u].push_back(i), E[i].push_back(u);
}
for(int i = n + 1; i <= n + m; i++)
{
int u;
scanf("%d", &u);
E[u].push_back(i), E[i].push_back(u);
}
dfs(1, 0);
d[c] = 0, dfs(c, 0);
printf("%d\n", d[c]);
return 0;
}
- 树形dp:
#include <bits/stdc++.h>
using namespace std;
const int N = 20000 + 10;
int n,m, d = 0;
int d1[N], d2[N];
vector<int> E[N];
void dfs(int u, int fa) {
d1[u] = d2[u] = 0;
for (int v : E[u]) {
if (v == fa) continue;
dfs(v, u);
int t = d1[v] + 1;
if (t > d1[u])
d2[u] = d1[u], d1[u] = t;
else if (t > d2[u])
d2[u] = t;
}
d = max(d, d1[u] + d2[u]);
}
int main() {
scanf("%d %d", &n,&m);
for (int i = 2; i <= n; i++) {
int u;
scanf("%d", &u);
E[u].push_back(i), E[i].push_back(u);
}
for(int i = n + 1; i <= n + m; i++)
{
int u;
scanf("%d", &u);
E[u].push_back(i), E[i].push_back(u);
}
dfs(1, 0);
printf("%d\n", d);
return 0;
}