T1

解题思路

这题应该不是很难,主要是题意理解问题。

注意给出的两个数组里映射关系已经对应好了,只要判断是否为双射即可

参考程序

#include <bits/stdc++.h>
using namespace std; class RelationClassifier {
public:
string isBijection( vector <int> domain, vector <int> range );
};
int reflect[110], used[110];
string RelationClassifier::isBijection(vector <int> domain, vector <int> range) {
int n = domain.size();
memset(reflect, 255, sizeof(reflect));
memset(used, 255, sizeof(used));
for(int i = 0; i < n; i++){
if(reflect[domain[i]] != -1 && reflect[domain[i]] != range[i]) return "Not";
if(used[range[i]] != -1 && reflect[domain[i]] != range[i]) return "Not";
reflect[domain[i]] = range[i];
used[range[i]] = 1;
}
return "Bijection";
}

T2

解题思路

首先理解题目中的两个操作的意思。

将点都向同一个方向移动相等距离,就是人可以在坐标系里随意走;可以将所有点旋转一个角度,就是人可以任意转动。那么我们只要找出一组互相垂直的直线,使它们穿过尽量多的点。

由于n的范围只有50,我们考虑最为暴力的做法。

首先枚举两个相异点 AB,确定一条直线,再取异于AB的点C,确定另一条垂线,最后扫一遍所有点就好了。

注意这里的特殊情况:AB横或纵坐标相等时特判(斜率的做法,同时还要注意精度);n小于等于3的时候答案就是n

据说有大佬会此题不用浮点运算的做法,欢迎评论指出。

参考程序

#include <bits/stdc++.h>
using namespace std; class PlaneGame {
public:
int bestShot( vector <int> x, vector <int> y );
};
long double k1, b1, k2, b2;
int t, ans;
int PlaneGame::bestShot(vector <int> x, vector <int> y) {
int n = x.size();
if(n <= 2) return n;//特判
ans = 0;
for(int i = 0; i < n - 1; i++)
for(int j = i + 1; j < n; j++)
for(int k = 0; k < n; k++){
if(i == k || j == k) continue;
if(x[i] != x[j] && y[i] != y[j]){
k1 = (long double)(y[j] - y[i]) / (long double)(x[j] - x[i]);
b1 = (long double)y[i] - k1 * (long double)x[i];
k2 = -1.0/k1;
b2 = (long double)y[k] - k2 * (long double)x[k];//两条直线的信息
t = 0;
for(int l = 0; l < n; l++)
if(abs((long double)y[l] - (k1 * x[l] + b1)) <= 0.0001 || abs((long double)y[l] - (k2 * x[l] + b2)) <= 0.0001) t++;//注意精度误差
ans = max(ans, t);
continue;
}
if(y[i] == y[j]){//特判
t = 0;
for(int l = 0; l < n; l++)
if(y[l] == y[i] || x[l] == x[k]) t++;
ans = max(ans, t);
continue;
}
if(x[i] == x[j]){
t = 0;
for(int l = 0; l < n; l++)
if(x[l] == x[i] || y[l] == y[k]) t++;
ans = max(ans, t);
continue;
}
}
return ans;
}

T3

解题思路

首先我们简化问题。如果祖先关系形成一棵树,那么问题就变成了十分简单的树形DP,相信大家都会做。

那么我们看看这道题的麻烦之处。这里有至多15个点有两个父亲。所以再按照原先的做法,有两个父亲的节点的子树就会被计算两次。

如何解决这个问题?最朴素的想法,可能就是切掉和其中一个父亲的关系,使它变为一棵树。但是问题又来了,这样无法维护和那个父亲间的关系(如果那个父亲不选,那么这个点就必须选啊qwq)。所以,这时候15这个数就派上用场了。由于它比较小,所以我们直接暴力枚举有两个父亲的节点,规定它们必须选,或者必须不选。如果我们认定这个节点必须不选,那么它的两个父亲就必须选了。

解决了?不,还有一个问题。仔细思考一下,发现删边也不能乱删。我们必须删指向深度较小的父亲的那条边。

至此,问题解决。最坏时间复杂度 \(O(2^{15}*100)\) 然而由于十分暴力,最慢的点跑了1.2秒(然而时限是两秒qwq)。如果有大佬有更好的做法,可以在品论区提出。

参考程序

程序中通过按最大深度排序及打标记实现删边。

#include <bits/stdc++.h>
using namespace std; class VampireTreeDiv2 {
public:
int countMinSamples( vector <int> A, vector <int> B );
};
const long long mod = 1000000007;
long long n, cnt;
long long lp, f[1010], lin[2010], nxt[2010], deep[1010];
inline void add(long long x, long long y) { lin[++lp] = y; nxt[lp] = f[x]; f[x] = lp; return; }
void get_deep(long long pos, long long father){
deep[pos] = deep[father] + 1;
for(long long t = f[pos]; t; t = nxt[t])
if(deep[lin[t]] < deep[pos] + 1 && lin[t] != father) get_deep(lin[t], pos);
return;
}
struct Node{
long long deep, pos;
Node(long long deep_ = 0, long long pos_ = 0) { deep = deep_; pos = pos_; return; }
};
Node aa[1010];
bool cmp(Node x, Node y){
return x.deep > y.deep;
}
long long rec[1010], recc[1010];
long long caculated[1010];//标记
long long dp[1010][2], c[1010][2];//dp[i][0]表示i不选,dp[i][1]表示i选,c数组维护方案数
long long t;
long long ans, minnum;
int VampireTreeDiv2::countMinSamples(vector <int> A, vector <int> B) {
n = A.size();
cnt = 0;
memset(f, 0, sizeof(f));
lp = 0;
for(long long i = 0; i < n; i++){
add(A[i], i + 1);
if(B[i] != -1){
add(B[i], i + 1);
rec[++cnt] = i + 1;
}
}
ans = 0; minnum = 100010;
memset(deep, 0, sizeof(deep));
get_deep(0, 0);
for(long long i = 0; i <= n; i++) aa[i] = Node(deep[i], i);
sort(aa, aa + n + 1, cmp);//按最大深度排序
for(long long i = 0; i < (1 << cnt); i++){
memset(recc, 0, sizeof(recc));
memset(caculated, 0, sizeof(caculated));
for(long long j = 1; j <= cnt; j++)
if((i >> (j - 1)) & 1)
recc[rec[j]] = 1; else recc[rec[j]] = 2;
for(long long j = 0; j <= n; j++){
t = aa[j].pos;
dp[t][0] = 0; dp[t][1] = 1;
c[t][0] = 1; c[t][1] = 1;
for(long long k = f[t]; k; k = nxt[k]){
if(caculated[lin[k]]){
if(dp[lin[k]][1] > 100000){
dp[t][0] = 100010;
c[t][0] = 0;
}
continue;
}
if(recc[lin[k]] != 0) caculated[lin[k]] = 1;
dp[t][0] += dp[lin[k]][1];
c[t][0] = (c[t][0] * c[lin[k]][1]) % mod;
if(dp[lin[k]][0] < dp[lin[k]][1]){
dp[t][1] += dp[lin[k]][0];
c[t][1] = (c[t][1] * c[lin[k]][0]) % mod;
}
if(dp[lin[k]][0] > dp[lin[k]][1]){
dp[t][1] += dp[lin[k]][1];
c[t][1] = (c[t][1] * c[lin[k]][1]) % mod;
}
if(dp[lin[k]][0] == dp[lin[k]][1]){
dp[t][1] += dp[lin[k]][1];
c[t][1] = (c[t][1] * ((c[lin[k]][0] + c[lin[k]][1]) % mod)) % mod;
}
}
if(recc[t] == 1) dp[t][0] = 100010, c[t][0] = 0;
if(recc[t] == 2) dp[t][1] = 100010, c[t][1] = 0;
}
if(dp[0][0] < minnum){
minnum = dp[0][0];
ans = c[0][0];
} else
if(dp[0][0] == minnum) ans = (ans + c[0][0]) % mod;
if(dp[0][1] < minnum){
minnum = dp[0][1];
ans = c[0][1];
} else
if(dp[0][1] == minnum) ans = (ans + c[0][1]) % mod;
}
return ans;
}

Topcoder SRM 674 Div.2题解的更多相关文章

  1. TopCoder SRM 667 Div.2题解

    概览: T1 枚举 T2 状压DP T3 DP TopCoder SRM 667 Div.2 T1 解题思路 由于数据范围很小,所以直接枚举所有点,判断是否可行.时间复杂度O(δX × δY),空间复 ...

  2. TopCoder SRM 560 Div 1 - Problem 1000 BoundedOptimization & Codeforces 839 E

    传送门:https://284914869.github.io/AEoj/560.html 题目简述: 定义"项"为两个不同变量相乘. 求一个由多个不同"项"相 ...

  3. [topcoder]SRM 633 DIV 2

    第一题,http://community.topcoder.com/stat?c=problem_statement&pm=13462&rd=16076 模拟就可以了. #includ ...

  4. TopCoder SRM 596 DIV 1 250

    body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...

  5. [topcoder]SRM 646 DIV 2

    第一题:K等于1或者2,非常简单.略.K更多的情况,http://www.cnblogs.com/lautsie/p/4242975.html,值得思考. 第二题:http://www.cnblogs ...

  6. 【topcoder SRM 702 DIV 2 250】TestTaking

    Problem Statement Recently, Alice had to take a test. The test consisted of a sequence of true/false ...

  7. Topcoder SRM 656 (Div.1) 250 RandomPancakeStack - 概率+记忆化搜索

    最近连续三次TC爆零了,,,我的心好痛. 不知怎么想的,这题把题意理解成,第一次选择j,第二次选择i后,只能从1~i-1.i+1~j找,其实还可以从j+1~n中找,只要没有被选中过就行... [题意] ...

  8. Topcoder SRM 648 (div.2)

    第一次做TC全部通过,截图纪念一下. 终于蓝了一次,也是TC上第一次变成蓝名,下次就要做Div.1了,希望div1不要挂零..._(:зゝ∠)_ A. KitayutaMart2 万年不变的水题. # ...

  9. Topcoder SRM 145 DIV 1

    Bonuses 模拟 题意:给你一个序列,按照百分比排序,再将百分比取整,再把剩余百分比加到最大的那几个. 题解:按照题意模拟就好.

随机推荐

  1. Spring Boot 五种热部署方式,极速开发就是生产力!

    1.模板热部署 在 Spring Boot 中,模板引擎的页面默认是开启缓存的,如果修改了页面的内容,则刷新页面是得不到修改后的页面的,因此我们可以在application.properties中关闭 ...

  2. Maven添加镜像仓库、更改本地仓库位置

    添加镜像仓库 在conf目录下的settings.xml文件的145行左右 id表示该镜像的id mirrorOf表示为哪个仓库配置镜像,central为默认的中央仓库的id,也可以使用通配符*,来匹 ...

  3. Linux就该这么学——新手必须掌握的命令之我的第一个命令

    1.Linux操作系统的开机进程(基本过程) (1).内核的引导: BIOS自检,安装BIOS默认设置的启动设备(硬盘)来启动.读取目录/boot目录下的内核文件 (2).运行init: 运行init ...

  4. N4语法

    第一期  授受关系 这里讲的授受关系是指“物的收受”也就是前后两个主体之前的“物的收受”. 请看以下三个基本句型:(从接收者B来分析) 1. AはBに-を         あげる(平辈.晚辈) (A给 ...

  5. 基于Zabbix 3.2.6版本的Discovery

    作用:用于发现某IP网段内存活并且满足一定条件的主机,发现后进行加入到zabbix server进行监控. 操作步骤: 创建[自动发现规则] 为新建的自动发现规则创建[Action]   操作步骤图文 ...

  6. redis blog

    IBM 看到的blog如何 存储在redis种 var ArticleHelper = function () { this.ArticleIDSet = "AIDSet"; // ...

  7. 2019-2020-1 20199319《Linux内核原理与分析》第九周作业

    进程的切换和系统的一般执行过程 进程调度的时机 1.中断:起到切出进程指令流的作用.中断处理程序是与进程无关的内核指令流.中断类型: 硬中断:可屏蔽中断和不可屏蔽中断.高电平说明有中断请求. 软中断/ ...

  8. 代码报错--------EOFError: Compressed file ended before the end-of-stream marker was reached

    背景:运行LeNet识别CIFAR-10的图像的代码时,报错: EOFError: Compressed file ended before the end-of-stream marker was ...

  9. particlesjs

    今天发现一个粒子动画的插件下个笔记做个备用: <!DOCTYPE html> <html lang="en"> <head> <meta ...

  10. CentOS6.5安装zabbix3.0

    Server端 搭建LAMP(Linux+Apache+Mysql+PHP)环境 1.安装MySQL #安装地址:https://dev.mysql.com/downloads/repo/yum/ y ...