第一场apc,5H的持久战,我当然水几个题就睡了

A - Two Integers


Time limit : 2sec / Memory limit : 256MB

Score : 100 points

Problem Statement

You are given positive integers X and Y. If there exists a positive integer not greater than 1018 that is a multiple of X but not a multiple of Y, choose one such integer and print it. If it does not exist, print −1.

Constraints

  • 1≤X,Y≤109
  • X and Y are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print a positive integer not greater than 1018 that is a multiple of X but not a multiple of Y, or print −1 if it does not exist.


Sample Input 1

Copy
8 6

Sample Output 1

Copy
16

For example, 16 is a multiple of 8 but not a multiple of 6.


Sample Input 2

Copy
3 3

Sample Output 2

Copy
-1

A multiple of 3 is a multiple of 3.

这个是特判题的

输出一个数是x的倍数但并不是y的倍数

emmmm,模拟一下?不,a是b的倍数的话不存在,否则输出a就好了啊

#include<bits/stdc++.h>
using namespace std;
int main()
{
int x,y;
cin>>x>>y;
if (x%y==)printf("-1");
else printf("%d",x);
}

给了16,所以我再猜最大公约数*a,太naive了,想了数据把自己hack了

话说直接暴力模拟就是直接在输出x

B - Two Arrays


Time limit : 2sec / Memory limit : 256MB

Score : 300 points

Problem Statement

You are given two integer sequences of length Na1,a2,..,aN and b1,b2,..,bN. Determine if we can repeat the following operation zero or more times so that the sequences a and b become equal.

Operation: Choose two integers i and j (possibly the same) between 1 and N (inclusive), then perform the following two actions simultaneously:

  • Add 2 to ai.
  • Add 1 to bj.

Constraints

  • 1≤N≤10 000
  • 0≤ai,bi≤109 (1≤iN)
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N
a1 a2 .. aN
b1 b2 .. bN

Output

If we can repeat the operation zero or more times so that the sequences a and b become equal, print Yes; otherwise, print No.


Sample Input 1

Copy
3
1 2 3
5 2 2

Sample Output 1

Copy
Yes

For example, we can perform three operations as follows to do our job:

  • First operation: i=1 and j=2. Now we have a={3,2,3}b={5,3,2}.
  • Second operation: i=1 and j=2. Now we have a={5,2,3}b={5,4,2}.
  • Third operation: i=2 and j=3. Now we have a={5,4,3}b={5,4,3}.

Sample Input 2

Copy
5
3 1 4 1 5
2 7 1 8 2

Sample Output 2

Copy
No

Sample Input 3

Copy
5
2 7 1 8 2
3 1 4 1 5

Sample Output 3

Copy
No

说是可以加任意次,但是你要让两个序列相等,最多次数就是sumb-suma,每次操作会让suma+2,sumb+1,相当于差少了1

然后去模拟让两个相等,需要分下奇偶。

#include<bits/stdc++.h>
using namespace std;
const int N=1e4+;
int a[N],b[N];
int main()
{
int n;
cin>>n;
for(int i=; i<n; i++)
cin>>a[i];
long long s=;
for(int i=; i<n; i++)
cin>>b[i],s+=a[i]-b[i];
if(s>)
printf("No");
else
{
long long ta=-s,tb=-s;
for(int i=; i<n; i++)
{
if(a[i]>b[i])
tb-=a[i]-b[i];
else if(a[i]<b[i])
{
if((b[i]-a[i])&)
ta-=(b[i]-a[i])/+,tb--;
else ta-=(b[i]-a[i])/;
}
}
if(*ta==tb&&ta>=&&tb>=)
printf("Yes");
else printf("No");
}
return ;
}

当然ta,tb也可以省掉,我就是模拟这个次数就可以了

#include<bits/stdc++.h>
using namespace std;
const int N=1e4+;
int a[N],b[N];
int main()
{
int n;
cin>>n;
for(int i=; i<n; i++)
cin>>a[i];
long long s=;
for(int i=; i<n; i++)
cin>>b[i];
for(int i=; i<n; i++)
{
if(a[i]>b[i])
s-=a[i]-b[i];
else s+=(b[i]-a[i])/;
}
if(s>=)
printf("Yes");
else printf("No");
return ;
}

C - Vacant Seat


Time limit : 2sec / Memory limit : 256MB

Score : 500 points

Problem Statement

This is an interactive task.

Let N be an odd number at least 3.

There are N seats arranged in a circle. The seats are numbered 0 through N−1. For each i (0≤iN−2), Seat i and Seat i+1are adjacent. Also, Seat N−1 and Seat 0 are adjacent.

Each seat is either vacant, or oppupied by a man or a woman. However, no two adjacent seats are occupied by two people of the same sex. It can be shown that there is at least one empty seat where N is an odd number at least 3.

You are given N, but the states of the seats are not given. Your objective is to correctly guess the ID number of any one of the empty seats. To do so, you can repeatedly send the following query:

  • Choose an integer i (0≤iN−1). If Seat i is empty, the problem is solved. Otherwise, you are notified of the sex of the person in Seat i.

Guess the ID number of an empty seat by sending at most 20 queries.

Constraints

  • N is an odd number.
  • 3≤N≤99 999

Input and Output

First, N is given from Standard Input in the following format:

N

Then, you should send queries. A query should be printed to Standart Output in the following format. Print a newline at the end.

i

The response to the query is given from Standard Input in the following format:

s

Here, s is VacantMale or Female. Each of these means that Seat i is empty, occupied by a man and occupied by a woman, respectively.

Notice

  • Flush Standard Output each time you print something. Failure to do so may result in TLE.
  • Immediately terminate the program when s is Vacant. Otherwise, the verdict is indeterminate.
  • The verdict is indeterminate if more than 20 queries or ill-formatted queries are sent.

Sample Input / Output 1

In this sample, N=3, and Seat 012 are occupied by a man, occupied by a woman and vacant, respectively.

Input Output
3  
  0
Male  
  1
Female  
  2
Vacant  

C是个交互题,记得在每次输出后fflush(stdout);

#include<bits/stdc++.h>
using namespace std;
map<string,int>M;
int x[];
int main()
{
M["Male"]=;
M["Female"]=;
int n;
scanf("%d",&n);
string s;
cout<<<<endl;
fflush(stdout);
cin>>s;
if(s=="Vacant")return ;
int lval=M[s];
cout<<n-<<endl;
fflush(stdout);
cin>>s;
if(s=="Vacant")return ;
int rval=M[s];
int l=,r=n-;
while(true)
{
int mi=(l+r)/;
cout<<mi<<endl;
fflush(stdout);
cin>>s;
if(s=="Vacant")return ;
int val=M[s];
if(!((mi-l+)%&)&&val==lval)
{
r=mi;
rval=val;
}
else if((mi-l+)&&&val!=lval)
{
r=mi;
rval=val;
}
else if(!((r-mi+)%)&&val==rval)
{
l=mi;
lval=val;
}
else if((r-mi+)&&&val!=rval)
{
l=mi;
lval=val;
}
}
return ;
}

大神的牛逼代码

#include<iostream>
using namespace std;
string s,t;
main()
{
int n,f,l,m;cin>>n;f=;l=n;
cout<<<<endl;
cin>>s;
if(s=="Vacant")return ;
while(t!="Vacant"){
m=(f+l)/;
cout<<m<<endl;
cin>>t;
if(m%^s==t)f=m;
else l=m;
}
}

D - Forest


Time limit : 2sec / Memory limit : 256MB

Score : 600 points

Problem Statement

You are given a forest with N vertices and M edges. The vertices are numbered 0 through N−1. The edges are given in the format (xi,yi), which means that Vertex xi and yi are connected by an edge.

Each vertex i has a value ai. You want to add edges in the given forest so that the forest becomes connected. To add an edge, you choose two different vertices i and j, then span an edge between i and j. This operation costs ai+aj dollars, and afterward neither Vertex i nor j can be selected again.

Find the minimum total cost required to make the forest connected, or print Impossible if it is impossible.

Constraints

  • 1≤N≤100,000
  • 0≤MN−1
  • 1≤ai≤109
  • 0≤xi,yiN−1
  • The given graph is a forest.
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N M
a0 a1 .. aN−1
x1 y1
x2 y2
:
xM yM

Output

Print the minimum total cost required to make the forest connected, or print Impossible if it is impossible.


Sample Input 1

Copy
7 5
1 2 3 4 5 6 7
3 0
4 0
1 2
1 3
5 6

Sample Output 1

Copy
7

If we connect vertices 0 and 5, the graph becomes connected, for the cost of 1+6=7 dollars.


Sample Input 2

Copy
5 0
3 1 4 1 5

Sample Output 2

Copy
Impossible

We can't make the graph connected.


Sample Input 3

Copy
1 0
5

Sample Output 3

Copy
0

The graph is already connected, so we do not need to add any edges.


 并查集+优先队列的一个很优秀的题目 
有一个森林,现在要把这个森林合并成一棵树。每个节点都回有一个权值。每个添加一条边所付出的代价就是所连接的两个权值的和。每个节点只能连接一次。
每组最小的是一定要的,但是这个只能加一次
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+,INF=0x3f3f3f3f;
int a[N],fa[N],vis[N],mi[N];
pair<int,int>t;
priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > >Q;
int find(int x)
{
return x==fa[x]?x:(fa[x]=find(fa[x]));
}
int main()
{
ios::sync_with_stdio(false);
int n,m;
cin>>n>>m;
for(int i=; i<n; i++)
cin>>a[i],fa[i]=i,mi[i]=INF;
for(int i=,u,v; i<m; i++)
{
cin>>u>>v,u=find(u),v=find(v);
if(u!=v)fa[u]=v;
}
for(int i=; i<n; i++)find(i);
for(int i=; i<n; i++)
mi[fa[i]]=min(a[i],mi[fa[i]]),Q.push(make_pair(a[i],fa[i]));
sort(fa,fa+n);
int tn=unique(fa,fa+n)-fa;
if(tn==)
cout<<;
else
{
long long s=;
for(int i=; i<tn; i++)
s+=mi[fa[i]];
while(!Q.empty())
{
if(tn==)break;
t=Q.top();
Q.pop();
if(!vis[t.second])
{
vis[t.second]=;
continue;
}
s+=t.first,--tn; }
if(tn==)cout<<s;
else cout<<"Impossible";
}
return ;
}

特判下impossible速度并没有更快

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+,INF=0x3f3f3f3f;
int a[N],fa[N],vis[N],mi[N];
typedef pair<int,int> pii;
priority_queue<pii, vector<pii >, greater<pii > >Q;
int find(int x)
{
return x==fa[x]?x:(fa[x]=find(fa[x]));
}
int main()
{
ios::sync_with_stdio(false);
int n,m;
cin>>n>>m;
for(int i=; i<n; i++)
cin>>a[i],fa[i]=i,mi[i]=INF;
for(int i=,u,v; i<m; i++)
{
cin>>u>>v,u=find(u),v=find(v);
if(u!=v)fa[u]=v;
}
for(int i=; i<n; i++)find(i);
for(int i=; i<n; i++)
mi[fa[i]]=min(a[i],mi[fa[i]]),Q.push(make_pair(a[i],fa[i]));
sort(fa,fa+n);
int tn=unique(fa,fa+n)-fa;
if(tn==)
cout<<;
else if((tn-)*>n)
cout<<"Impossible";
else
{
long long s=;
for(int i=; i<tn; i++)
s+=mi[fa[i]];
while(!Q.empty())
{
if(tn==)break;
pii t=Q.top();
Q.pop();
if(!vis[t.second])
{
vis[t.second]=;
continue;
}
s+=t.first,--tn;
}
cout<<s;
}
return ;
}

优化不了了,最后的代码也很棒

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+,INF=0x3f3f3f3f;
typedef pair<int,int> pii;
int fa[N],vis[N],mi[N],V[N];
vector<pii >Q(N);
int find(int x)
{
return x==fa[x]?x:(fa[x]=find(fa[x]));
}
int main()
{
ios::sync_with_stdio(false);
int n,m,tn=;
cin>>n>>m;
for(int i=; i<n; i++)
cin>>Q[i].first,fa[i]=i,mi[i]=INF;
for(int i=,u,v; i<m; i++)
{
cin>>u>>v,u=find(u),v=find(v);
if(u!=v)fa[u]=v;
}
for(int i=; i<n; i++)
{
find(i);
if(!vis[fa[i]])V[tn++]=fa[i];
vis[fa[i]]=,Q[i].second=fa[i],mi[fa[i]]=min(Q[i].first,mi[fa[i]]);
}
if(tn==)cout<<;
else if((tn-)*>n)cout<<"Impossible";
else
{
long long s=;
for(int i=; i<tn; i++)s+=mi[V[i]];
if(tn!=)sort(Q.begin(),Q.begin()+n);
for(int i=; i<n&&tn!=; i++)
{
if(vis[Q[i].second])
{
vis[Q[i].second]=;
continue;
}
s+=Q[i].first,tn--;
}
cout<<s;
}
return ;
}

E - Antennas on Tree


Time limit : 2sec / Memory limit : 256MB

Score : 900 points

Problem Statement

We have a tree with N vertices. The vertices are numbered 0 through N−1, and the i-th edge (0≤i<N−1) comnnects Vertex aiand bi. For each pair of vertices u and v (0≤u,v<N), we define the distance d(u,v) as the number of edges in the path u-v.

It is expected that one of the vertices will be invaded by aliens from outer space. Snuke wants to immediately identify that vertex when the invasion happens. To do so, he has decided to install an antenna on some vertices.

First, he decides the number of antennas, K (1≤KN). Then, he chooses K different vertices, x0x1, ..., xK−1, on which he installs Antenna 01, ..., K−1, respectively. If Vertex v is invaded by aliens, Antenna k (0≤k<K) will output the distance d(xk,v). Based on these K outputs, Snuke will identify the vertex that is invaded. Thus, in order to identify the invaded vertex no matter which one is invaded, the following condition must hold:

  • For each vertex u (0≤u<N), consider the vector (d(x0,u),…,d(xK−1,u)). These N vectors are distinct.

Find the minumum value of K, the number of antennas, when the condition is satisfied.

Constraints

  • 2≤N≤105
  • 0≤ai,bi<N
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a0 b0
a1 b1
:
aN−2 bN−2

Output

Print the minumum value of K, the number of antennas, when the condition is satisfied.


Sample Input 1

Copy
5
0 1
0 2
0 3
3 4

Sample Output 1

Copy
2

For example, install an antenna on Vertex 1 and 3. Then, the following five vectors are distinct:

  • (d(1,0),d(3,0))=(1,1)
  • (d(1,1),d(3,1))=(0,2)
  • (d(1,2),d(3,2))=(2,2)
  • (d(1,3),d(3,3))=(2,0)
  • (d(1,4),d(3,4))=(3,1)

Sample Input 2

Copy
2
0 1

Sample Output 2

Copy
1

For example, install an antenna on Vertex 0.


Sample Input 3

Copy
10
2 8
6 0
4 1
7 6
2 3
8 6
6 9
2 4
5 8

Sample Output 3

Copy
3

For example, install an antenna on Vertex 049.


一颗树让你找最少k个点放监控,使得所有节点到这个节点的k维空间向量(无方向)两两不同

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+;
vector<int>G[N];
int n,dp[N],f=-;
void dfs(int i,int p)
{
int F=;
for(auto j:G[i])
{
if(j==p)continue;
dfs(j,i);
dp[i]+=dp[j];
if(!dp[j])
{
if(!F)F=;
else dp[i]++;
}
}
}
int main()
{
scanf("%d",&n);
for(int i=,u,v; i<n; i++)
scanf("%d%d",&u,&v),G[u].push_back(v),G[v].push_back(u);
for(int i=; i<n; i++)
if(G[i].size()>)
{
f=i;
break;
}
if(f==-)printf("1\n");
else dfs(f,-),printf("%d\n",dp[f]);
return ;
}

AtCoder Petrozavodsk Contest 001的更多相关文章

  1. 【AtCoder】AtCoder Petrozavodsk Contest 001

    A - Two Integers 如果\(X\)是\(Y\)的倍数的话不存在 可以输出\(X \cdot (\frac{Y}{gcd(X,Y)} - 1)\) 代码 #include <bits ...

  2. AtCoder Petrozavodsk Contest 001 B - Two Arrays

    Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement You are given two inte ...

  3. AtCoder Petrozavodsk Contest 001 A - Two Integers

    Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement You are given positive ...

  4. AtCoder Grand Contest 001 C Shorten Diameter 树的直径知识

    链接:http://agc001.contest.atcoder.jp/tasks/agc001_c 题解(官方): We use the following well-known fact abou ...

  5. AtCoder Grand Contest 001 D - Arrays and Palindrome

    题目传送门:https://agc001.contest.atcoder.jp/tasks/agc001_d 题目大意: 现要求你构造两个序列\(a,b\),满足: \(a\)序列中数字总和为\(N\ ...

  6. Atcoder Grand Contest 001 D - Arrays and Palindrome(构造)

    Atcoder 题面传送门 洛谷题面传送门 又是道思维题,又是道把我搞自闭的题. 首先考虑对于固定的 \(a_1,a_2,\dots,a_n;b_1,b_2,\dots,b_m\) 怎样判定是否合法, ...

  7. Atcoder Grand Contest 001 F - Wide Swap(拓扑排序)

    Atcoder 题面传送门 & 洛谷题面传送门 咦?鸽子 tzc 来补题解了?奇迹奇迹( 首先考虑什么样的排列可以得到.我们考虑 \(p\) 的逆排列 \(q\),那么每次操作的过程从逆排列的 ...

  8. AtCoder Grand Contest 001

    B - Mysterious Light 题意:从一个正三角形边上一点出发,遇到边和已走过的边则反弹,问最终路径长度 思路:GCD 数据爆long long #pragma comment(linke ...

  9. 【刷题】AtCoder Regular Contest 001

    A.センター採点 题意:给一个只包含1.2.3.4的字符串,求出现次数最多和最少的字符 做法:还能怎么做... #include<bits/stdc++.h> #define ui uns ...

随机推荐

  1. Outlook 2016 自动发送/接收无法正常工作

    如果您的自动/发送接收由于某种原因停止工作,可能会非常令人沮丧,因为您必须记住手动执行发送/接收(F9).如果您遇到Outlook无法自动发送或接收电子邮件的问题,可以尝试以下几项操作. #1 发送/ ...

  2. javaSe数据类型

    在学完了java程序的结构以及注释后呢按照一般的教程我们应该学点什么呢?   没错就是变量和数据类型[其实我的内心是拒绝的,又是无聊的一大堆,不仅无聊,还得掌握] 好了首先介绍什么是变量: 变量:变量 ...

  3. HDU 3652 B-number (数位DP,入门)

    题意: 如果一个整数能被13整除,且其含有子串13的,称为"B数",问[1,n]中有多少个B数? 思路: 这题不要用那个DFS的模板估计很快秒了. 状态设计为dp[位数][前缀][ ...

  4. Tarjan的学习笔记 求割边求割点

    博主图论比较弱,搜了模版也不会用... 所以决心学习下tarjan算法. 割点和割边的概念不在赘述,tarjan能在线性时间复杂度内求出割边. 重要的概念:时间戟,就是一个全局变量clock记录访问结 ...

  5. JavaScript实现页面到滚动到指定位置执行某些操作

    比如 页面中 某个DOM, 希望点击按钮后页面直接跳转到 这个DOM所在的位置, 1. 获取DOM离屏幕的高度 var hTop = $('#box').offset().top; $('body,h ...

  6. MySQL内置函数:IP地址点分式与数字转换函数(INET_ATON/INET_NTOA)

    前后转换,相比代码内部在进行移位简单太多了 SELECT INET_ATON('209.207.224.40'); SELECT INET_NTOA('578950');

  7. JavaScript onkeydown事件入门实例(键盘某个按键被按下)

    JavaScript onkeydown 事件 用户按下一个键盘按键时会触发 onkeydown 事件.与 onkeypress事件不同的是,onkeydown 事件是响应任意键按下的处理(包括功能键 ...

  8. 数据库-SQL语法:把一个字段的值设为随机整数

     update test2 set zuig = (cast ( ceiling (rand()*9) as int))  

  9. 简单的cocos2dx笔试题

    1.参数传递有几种方式?值传递.指针传递.引用传递 2.指针和引用有什么分别:如果传引用比传指针安全,为什么?如果我使用常量指针难道不行吗? 1.指针是一个变量,存储一个地址,指向内存的一个存储单元: ...

  10. Race condition

    在很多门课上都接触到race condition, 其中也举了很多方法解决这个问题.于是想来总结一下这些方法. Race condition 它旨在描述一个系统或者进程的输出依赖于不受控制的事件出现顺 ...