Codeforces Round #549 (Div. 2) A—C

本文解析了三道算法竞赛题目:快速关门问题、数字乘积最大化问题及树状结构中的节点删除顺序问题,提供了清晰的思路与高效代码实现。
A. The Doors
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Three years have passes and nothing changed. It is still raining in London, and Mr. Black has to close all the doors in his home in order to not be flooded. Once, however, Mr. Black became so nervous that he opened one door, then another, then one more and so on until he opened all the doors in his house.

There are exactly two exits from Mr. Black's house, let's name them left and right exits. There are several doors in each of the exits, so each door in Mr. Black's house is located either in the left or in the right exit. You know where each door is located. Initially all the doors are closed. Mr. Black can exit the house if and only if all doors in at least one of the exits is open. You are given a sequence in which Mr. Black opened the doors, please find the smallest index kk such that Mr. Black can exit the house after opening the first kk doors.

We have to note that Mr. Black opened each door at most once, and in the end all doors became open.

Input

The first line contains integer nn (2n2000002≤n≤200000) — the number of doors.

The next line contains nn integers: the sequence in which Mr. Black opened the doors. The ii-th of these integers is equal to 00 in case the ii-th opened door is located in the left exit, and it is equal to 11 in case it is in the right exit.

It is guaranteed that there is at least one door located in the left exit and there is at least one door located in the right exit.

Output

Print the smallest integer kk such that after Mr. Black opened the first kk doors, he was able to exit the house.

Examples
input
Copy
5
0 0 1 0 0
output
Copy
3
input
Copy
4
1 0 0 1
output
Copy
3
Note

In the first example the first two doors are from the left exit, so when Mr. Black opened both of them only, there were two more closed door in the left exit and one closed door in the right exit. So Mr. Black wasn't able to exit at that moment.

When he opened the third door, all doors from the right exit became open, so Mr. Black was able to exit the house.

In the second example when the first two doors were opened, there was open closed door in each of the exit.

With three doors opened Mr. Black was able to use the left exit.

弱智题目 记录最后一个0和1的位置 输出比较小的那个下标就行

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const int INF=1e9+5;

typedef pair<int,int> pii;
int d[maxn],vis[maxn],cnt[maxn],pre[maxn],sum[maxn],val[maxn];
vector<pii> e[maxn];
int n,k;




int main()
{
    cin>>n;
    int a[maxn];
    int x=0,y=0,px,py;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]==0) {x++;px=i;}
        if(a[i]==1) {y++;py=i;}
    }
    if(px>py) cout<<py+1<<endl;
    else cout<<px+1<<endl;
    return 0;
}
View Code

 

B. Nirvana
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Kurt reaches nirvana when he finds the product of all the digits of some positive integer. Greater value of the product makes the nirvana deeper.

Help Kurt find the maximum possible product of digits among all integers from 11 to nn.

Input

The only input line contains the integer nn (1n21091≤n≤2⋅109).

Output

Print the maximum product of digits among all integers from 11 to nn.

Examples
input
Copy
390
output
Copy
216
input
Copy
7
output
Copy
7
input
Copy
1000000000
output
Copy
387420489
Note

In the first example the maximum product is achieved for 389389 (the product of digits is 389=2163⋅8⋅9=216).

In the second example the maximum product is achieved for 77 (the product of digits is 77).

In the third example the maximum product is achieved for 999999999999999999 (the product of digits is 99=38742048999=387420489).

 给一个数n 输出1-n之内数位乘积最大的乘积。

这个题目最方便的方法就是递归,边递归边比较,可以避免888这样近两位比较出现888优于799的情况。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const int INF=1e9+5;

typedef pair<int,int> pii;
int d[maxn],vis[maxn],cnt[maxn],pre[maxn],sum[maxn],val[maxn];
vector<pii> e[maxn];
int n,k;

int cmp(int n)
{
    if(n==0) return 1;
    else if(n<10) return n;
    else
        return max(n%10*cmp(n/10),9*cmp(n/10-1));
}


int main()
{
    char s[15];
    cin>>n;

    cout<<cmp(n)<<endl;
    return 0;
}
View Code

 

C. Queen
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a rooted tree with vertices numerated from 11 to nn. A tree is a connected graph without cycles. A rooted tree has a special vertex named root.

Ancestors of the vertex ii are all vertices on the path from the root to the vertex ii, except the vertex ii itself. The parent of the vertex ii is the nearest to the vertex ii ancestor of ii. Each vertex is a child of its parent. In the given tree the parent of the vertex ii is the vertex pipi. For the root, the value pipi is 1−1.

An example of a tree with n=8n=8, the root is vertex 55. The parent of the vertex 22 is vertex 33, the parent of the vertex 11 is vertex 55. The ancestors of the vertex 66 are vertices 44 and 55, the ancestors of the vertex 77 are vertices 88, 33 and 55

You noticed that some vertices do not respect others. In particular, if ci=1ci=1, then the vertex ii does not respect any of its ancestors, and if ci=0ci=0, it respects all of them.

You decided to delete vertices from the tree one by one. On each step you select such a non-root vertex that it does not respect its parent and none of its children respects it. If there are several such vertices, you select the one with the smallest number. When you delete this vertex vv, all children of vv become connected with the parent of vv.

An example of deletion of the vertex 77.

Once there are no vertices matching the criteria for deletion, you stop the process. Print the order in which you will delete the vertices. Note that this order is unique.

Input

The first line contains a single integer nn (1n1051≤n≤105) — the number of vertices in the tree.

The next nn lines describe the tree: the ii-th line contains two integers pipi and cici (1pin1≤pi≤n, 0ci10≤ci≤1), where pipi is the parent of the vertex ii, and ci=0ci=0, if the vertex ii respects its parents, and ci=1ci=1, if the vertex ii does not respect any of its parents. The root of the tree has 1−1 instead of the parent index, also, ci=0ci=0 for the root. It is guaranteed that the values pipi define a rooted tree with nn vertices.

Output

In case there is at least one vertex to delete, print the only line containing the indices of the vertices you will delete in the order you delete them. Otherwise print a single integer 1−1.

Examples
input
Copy
5
3 1
1 1
-1 0
2 1
3 0
output
Copy
1 2 4 
input
Copy
5
-1 0
1 1
1 1
2 0
3 0
output
Copy
-1
input
Copy
8
2 1
-1 0
1 0
1 1
1 1
4 0
5 1
7 0
output
Copy
5 
Note

The deletion process in the first example is as follows (see the picture below, the vertices with ci=1ci=1 are in yellow):

  • first you will delete the vertex 11, because it does not respect ancestors and all its children (the vertex 22) do not respect it, and 11 is the smallest index among such vertices;
  • the vertex 22 will be connected with the vertex 33 after deletion;
  • then you will delete the vertex 22, because it does not respect ancestors and all its children (the only vertex 44) do not respect it;
  • the vertex 44 will be connected with the vertex 33;
  • then you will delete the vertex 44, because it does not respect ancestors and all its children (there are none) do not respect it (vacuous truth);
  • you will just delete the vertex 44;
  • there are no more vertices to delete.

In the second example you don't need to delete any vertex:

  • vertices 22 and 33 have children that respect them;
  • vertices 44 and 55 respect ancestors.

In the third example the tree will change this way:

题意就是如果一个点父慈子孝(所有儿子都不respect他,他也不respect他爹)那就把它删掉,从标号最小的点开始删除,比赛的时候弱智了,想着记录所有的儿子,然后看孝子数和儿子数是否一样。现在得得其实只要有一个儿子不是孝子,那就不能删除他。所以反向标记就省事的多,开个数组记录这个点是不是孝子,然后另一个数组记录自己的儿子们有没有真正的孝子,然后判断两个条件是否同时成立就行了,不会打了几十行最后TLE。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const int INF=1e9+5;

typedef pair<int,int> pii;
int d[maxn],vis[maxn],cnt[maxn],pre[maxn],sum[maxn],val[maxn];
vector<pii> e[maxn];
int n,k;
int res[maxn];
int c[maxn];

int main()
{
    cin>>n;
    for(int i=0;i<=n;i++) res[i]=1;
    for(int i=1;i<=n;i++)
    {
        int x,y;
        cin>>x>>y;
        if(y==1) c[i]=1;
        if(y==0) res[x]=0;
    }
    int cnt=0;
    for(int i=1;i<=n;i++)
    {
        if(res[i]==1 && c[i]==1)
            cout<<i<<" ",cnt++;
    }
    if(cnt==0) cout<<"-1"<<endl;
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/youchandaisuki/p/10630646.html

源码来自:https://pan.quark.cn/s/a3a3fbe70177 AppBrowser(Application属性查看器,不需要越狱! ! ! ) 不需要越狱,调用私有方法 --- 获取完整的已安装应用列表、打开和删除应用操作、应用运行时相关信息的查看。 支持iOS10.X 注意 目前AppBrowser不支持iOS11应用查看, 由于iOS11目前还处在Beta版, 系统API还没有稳定下来。 等到Private Header更新了iOS11版本,我也会进行更新。 功能 [x] 已安装的应用列表 [x] 应用的详情界面 (打开应用,删除应用,应用的相关信息展示) [x] 应用运行时信息展示(LSApplicationProxy) [ ] 定制喜欢的字段,展示在应用详情界面 介绍 所有已安装应用列表(应用icon+应用名) 为了提供思路,这里只用伪代码,具体的私有代码调用请查看: 获取应用实例: 获取应用名和应用的icon: 应用列表界面展示: 应用列表 应用运行时详情 打开应用: 卸载应用: 获取info.plist文件: 应用运行时详情界面展示: 应用运行时详情 右上角,从左往右第一个按钮用来打开应用;第二个按钮用来卸载这个应用 INFO按钮用来解析并显示出对应的LSApplicationProxy类 树形展示LSApplicationProxy类 通过算法,将LSApplicationProxy类,转换成了字典。 转换规则是:属性名为key,属性值为value,如果value是一个可解析的类(除了NSString,NSNumber...等等)或者是个数组或字典,则继续递归解析。 并且会找到superClass的属性并解析,superClass如...
基于遗传算法辅助异构改进的动态多群粒子群优化算法(GA-HIDMSPSO)的LSTM分类预测研究(Matlab代码实现)内容概要:本文研究了一种基于遗传算法辅助异构改进的动态多群粒子群优化算法(GA-HIDMSPSO),并将其应用于LSTM神经网络的分类预测中,通过Matlab代码实现。该方法结合遗传算法的全局搜索能力与改进的多群粒子群算法的局部优化特性,提升LSTM模型在分类任务中的性能表现,尤其适用于复杂非线性系统的预测问题。文中详细阐述了算法的设计思路、优化机制及在LSTM参数优化中的具体应用,并提供了可复现的Matlab代码,属于SCI级别研究成果的复现与拓展。; 适合人群:具备一定机器学习和优化算法基础,熟悉Matlab编程,从事智能算法、时间序列预测或分类模型研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①提升LSTM在分类任务中的准确性与收敛速度;②研究混合智能优化算法(如GA与PSO结合)在神经网络超参数优化中的应用;③实现高精度分类预测模型,适用于电力系统故障诊断、电池健康状态识别等领域; 阅读建议:建议读者结合Matlab代码逐步调试运行,理解GA-HIDMSPSO算法的实现细节,重点关注种群划分、异构策略设计及与LSTM的集成方式,同时可扩展至其他深度学习模型的参数优化任务中进行对比实验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值