首页 技术 正文
技术 2022年11月18日
0 收藏 316 点赞 3,534 浏览 4683 个字

A.Xenia and Divisors

题意:给定N个数,每个数的取值范围为1-7,N是3的倍数,判定是否能够恰好将N个数分成若干三元组,使得一个组中的元素a,b,c满足 a < b < c 并且 a|b && b|c(整除)。

分析:满足这种条件的三元组只有[1, 2, 4], [1, 2, 6], [1, 3, 6]因此如果有5或者是7肯定是不行的,然后是1的个数要等于4和6的个数之和,最后就是2的数量必须4的数量。

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;const int N = ;
int n;
int cnt[];int main() {
int x;
bool flag = false;
scanf("%d", &n);
for (int i = ; i < n; ++i) {
scanf("%d", &x);
if (x == || x == ) flag = true;
cnt[x]++;
}
if (flag || cnt[] != cnt[]+cnt[] || cnt[] < cnt[]) {
puts("-1");
return ;
}
for (int i = ; i < cnt[]; ++i) puts("1 2 4");
for (int i = ; i < cnt[]; ++i) puts("1 3 6");
for (int i = ; i < cnt[]-cnt[]; ++i) puts("1 2 6");
return ;
}

B.Xenia and Spies

题意:有N个人站成一行,现在有一个情报要从编号S传到编号F,每次每个人只能够向左或者向右边的一个人传送。给定一个时间表,某一段时间内某个区间的人使不能够接收和传送情报,要求输出花最小时间的代价的传送方案。

分析:可以得到一个结论,如果S < F,那么情报不可能向左边传,顶多是停在某个人手中,S > F 同理,因此直接模拟即可。

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;const int M = ;
int n, m, s, f;struct interval {
int l, r, ti;
void read() {
scanf("%d %d %d", &ti, &l, &r);
}
bool judge(int x) {
if (x >= l && x <= r) return false;
else return true;
}
}q[M];void solve() {
int dir = s < f ? : -;
char ch = s < f ? 'R' : 'L';
int ti = ;
int X = ;
while (s != f) {
++ti;
if (ti == q[X].ti) {
if (q[X].judge(s) && q[X].judge(s+dir)) putchar(ch), s += dir;
else putchar('X');
X++;
} else {
putchar(ch);
s += dir;
}
}
}int main() {
scanf("%d %d %d %d", &n, &m, &s, &f);
for (int i = ; i < m; ++i) {
q[i].read();
}
solve();
return ;
}

C.Cupboard and Balloons

题意:问一个橱柜中能够放置多少个球。

分析:其实就是一个分段函数,可以分别在顶端放1个,放2个,放3个的h取值。

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;int r, h;
const double eps = 1e-;int sign(double x) {
return x < -eps ? - : x > eps ? : ;
}int get(double x) {
if (sign(*x - sqrt()*r) >= ) return ;
else if (sign(*x - r) >= ) return ;
else return ;
}int main() {
scanf("%d %d", &r, &h);
printf("%d\n", h/r* + get(h-h/r*r));
return ;
}

D.Xenia and Dominoes

题意:给定一个3*N的棋盘,其中某些位置不能够放置,并且有一个点是空的,问使用1*2的方格来平铺能够有多少种方案,要求一个空格周围有1*2的方格可能移动。

分析:与其他的题目稍有不同的就是棋盘多了一些限制并且有一个点周围的点至少有一个1*2的方格能够移动。对于前者,在dp的时候做个位运算即可,后者我的做法是用总的方案数减去不合法的方案数。

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;const int mod = int(1e9)+;
vector<int>vt[];
int dp[][];
int mp[];
int ocp[];
int n, cx, cy;void pre() {
vt[].push_back();
vt[].push_back();
vt[].push_back();
vt[].push_back(), vt[].push_back();
vt[].push_back();
vt[].push_back();
vt[].push_back(), vt[].push_back();
vt[].push_back(), vt[].push_back(), vt[].push_back();
}int cal_1() { // 计算全部的情况
dp[][] = ;
for (int i = ; i <= n; ++i) {
for (int j = ; j < ; ++j) {
if ((j & mp[i]) != j) continue;
for (int k = ; k < (int)vt[j].size(); ++k) {
dp[i][j|ocp[i]] = (1LL*dp[i][j|ocp[i]] + dp[i-][vt[j][k]])%mod;
}
}
}
return dp[n][];
}int cal_2() { // 计算非法的情况
memset(dp, , sizeof (dp));
dp[][] = ;
for (int i = ; i <= n; ++i) {
if (i+ == cx) { // 如果是空点的上一行
for (int j = ; j < ; ++j) {
if ((j & mp[i]) != j) continue;
for (int k = ; k < (int)vt[j].size(); ++k) {
if (!(vt[j][k] & ( << cy-))) continue;
dp[i][j|ocp[i]] = (1LL*dp[i][j|ocp[i]] + dp[i-][vt[j][k]])%mod;
}
}
} else if (i == cx) { // 如果是空点的所在行
for (int j = ; j < ; ++j) {
if ((j & mp[i]) != j) continue;
for (int k = ; k < (int)vt[j].size(); ++k) {
if (cy==&&(j&(<<cy-))&&(j&(<<cy-))&&(vt[j][k]&(<<cy-))&&(vt[j][k]&(<<cy-))) continue;
if (cy==&&(j&(<<cy))&&(j&(<<cy+))&&(vt[j][k]&(<<cy))&&(vt[j][k]&(<<cy+))) continue;
dp[i][j|ocp[i]] = (1LL*dp[i][j|ocp[i]] + dp[i-][vt[j][k]])%mod;
}
}
} else if (i- == cx) { // 如果是空点的下数第二行
for (int j = ; j < ; ++j) {
if ((j & mp[i]) != j) continue;
for (int k = ; k < (int)vt[j].size(); ++k) {
if (!(vt[j][k] & ( << cy-))) continue;
dp[i][j|ocp[i]] = (1LL*dp[i][j|ocp[i]] + dp[i-][vt[j][k]])%mod;
}
}
} else {
for (int j = ; j < ; ++j) {
if ((j & mp[i]) != j) continue;
for (int k = ; k < (int)vt[j].size(); ++k) {
dp[i][j|ocp[i]] = (1LL*dp[i][j|ocp[i]] + dp[i-][vt[j][k]])%mod;
}
}
}
}
return dp[n][];
}int main() {
pre();
scanf("%d", &n);
char str[];
for (int i = ; i <= ; ++i) {
scanf("%s", str+);
for (int j = ; j <= n; ++j) {
mp[j] |= (int)(str[j]=='.') << (i-);
ocp[j] |= (int)(str[j]!='.') << (i-);
if (str[j] == 'O') cx = j, cy = i;
}
}
printf("%d\n", (cal_1()-cal_2()+mod)%mod);
return ;
}

E.Xenia and Tree

题意:给定一棵树,初始化1号节点为红色,其他点为蓝色。有两种操作:1.将某个点从蓝色变成红色;2.询问某个节点到最近的红色的点的距离。

分析:对于1操作保留要改变的点,当有询问时在bfs更新。不过觉得这种做法在树退化成一条链时时间复杂度为O(N^2)。

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef unsigned int uint;
const int N = ;
int dis[N];
char vis[N];
int n, m;
vector<int>vt;
queue<int>q;struct Edge {
int v, nxt;
}e[N<<];
int idx, head[N];void addedge(int a, int b) {
e[idx].v = b, e[idx].nxt = head[a];
head[a] = idx++;
}void spread() {
memset(vis, , sizeof (vis));
for (uint i = ; i < vt.size(); ++i) {
dis[vt[i]] = ;
vis[vt[i]] = ;
q.push(vt[i]);
}
vt.clear();
while (!q.empty()) {
int u = q.front(); q.pop();
vis[u] = ;
for (int i = head[u]; ~i; i = e[i].nxt) {
int v = e[i].v;
if (dis[v] > dis[u] + ) {
dis[v] = dis[u] + ;
if (!vis[v]) vis[v] = , q.push(v);
}
}
}
}int main() {
int a, b;
idx = ;
memset(head, 0xff, sizeof (head));
memset(dis, 0x3f, sizeof (dis));
scanf("%d %d", &n, &m);
for (int i = ; i < n; ++i) {
scanf("%d %d", &a, &b);
addedge(a, b), addedge(b, a);
}
vt.push_back();
for (int i = ; i < m; ++i) {
scanf("%d %d", &a, &b);
if (a == ) vt.push_back(b);
else spread(), printf("%d\n", dis[b]);
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898