第一场apc,5H的持久战,我固然水几个题就睡了ios
Time limit : 2sec / Memory limit : 256MBc++
Score : 100 pointsapp
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.ide
Input is given from Standard Input in the following format:优化
X Y
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.ui
8 6
16
For example, 16 is a multiple of 8 but not a multiple of 6.this
3 3
-1
A multiple of 3 is a multiple of 3.spa
这个是特判题的rest
输出一个数是x的倍数但并非y的倍数code
emmmm,模拟一下?不,a是b的倍数的话不存在,不然输出a就行了啊
#include<bits/stdc++.h> using namespace std; int main() { int x,y; cin>>x>>y; if (x%y==0)printf("-1"); else printf("%d",x); }
给了16,因此我再猜最大公约数*a,太naive了,想了数据把本身hack了
话说直接暴力模拟就是直接在输出x
Time limit : 2sec / Memory limit : 256MB
Score : 300 points
You are given two integer sequences of length N: a1,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:
Input is given from Standard Input in the following format:
N a1 a2 .. aN b1 b2 .. bN
If we can repeat the operation zero or more times so that the sequences a and b become equal, print Yes
; otherwise, print No
.
3 1 2 3 5 2 2
Yes
For example, we can perform three operations as follows to do our job:
5 3 1 4 1 5 2 7 1 8 2
No
5 2 7 1 8 2 3 1 4 1 5
No
说是能够加任意次,可是你要让两个序列相等,最屡次数就是sumb-suma,每次操做会让suma+2,sumb+1,至关于差少了1
而后去模拟让两个相等,须要分下奇偶。
#include<bits/stdc++.h> using namespace std; const int N=1e4+5; int a[N],b[N]; int main() { int n; cin>>n; for(int i=0; i<n; i++) cin>>a[i]; long long s=0; for(int i=0; i<n; i++) cin>>b[i],s+=a[i]-b[i]; if(s>0) printf("No"); else { long long ta=-s,tb=-s; for(int i=0; i<n; i++) { if(a[i]>b[i]) tb-=a[i]-b[i]; else if(a[i]<b[i]) { if((b[i]-a[i])&1) ta-=(b[i]-a[i])/2+1,tb--; else ta-=(b[i]-a[i])/2; } } if(2*ta==tb&&ta>=0&&tb>=0) printf("Yes"); else printf("No"); } return 0; }
固然ta,tb也能够省掉,我就是模拟这个次数就能够了
#include<bits/stdc++.h> using namespace std; const int N=1e4+5; int a[N],b[N]; int main() { int n; cin>>n; for(int i=0; i<n; i++) cin>>a[i]; long long s=0; for(int i=0; i<n; i++) cin>>b[i]; for(int i=0; i<n; i++) { if(a[i]>b[i]) s-=a[i]-b[i]; else s+=(b[i]-a[i])/2; } if(s>=0) printf("Yes"); else printf("No"); return 0; }
Time limit : 2sec / Memory limit : 256MB
Score : 500 points
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≤i≤N−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:
Guess the ID number of an empty seat by sending at most 20 queries.
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 Vacant
, Male
or Female
. Each of these means that Seat i is empty, occupied by a man and occupied by a woman, respectively.
TLE
.Vacant
. Otherwise, the verdict is indeterminate.In this sample, N=3, and Seat 0, 1, 2 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[999999]; int main() { M["Male"]=1; M["Female"]=0; int n; scanf("%d",&n); string s; cout<<0<<endl; fflush(stdout); cin>>s; if(s=="Vacant")return 0; int lval=M[s]; cout<<n-1<<endl; fflush(stdout); cin>>s; if(s=="Vacant")return 0; int rval=M[s]; int l=0,r=n-1; while(true) { int mi=(l+r)/2; cout<<mi<<endl; fflush(stdout); cin>>s; if(s=="Vacant")return 0; int val=M[s]; if(!((mi-l+1)%2&1)&&val==lval) { r=mi; rval=val; } else if((mi-l+1)&1&&val!=lval) { r=mi; rval=val; } else if(!((r-mi+1)%2)&&val==rval) { l=mi; lval=val; } else if((r-mi+1)&1&&val!=rval) { l=mi; lval=val; } } return 0; }
大神的牛逼代码
#include<iostream> using namespace std; string s,t; main() { int n,f,l,m;cin>>n;f=0;l=n; cout<<0<<endl; cin>>s; if(s=="Vacant")return 0; while(t!="Vacant"){ m=(f+l)/2; cout<<m<<endl; cin>>t; if(m%2^s==t)f=m; else l=m; } }
Time limit : 2sec / Memory limit : 256MB
Score : 600 points
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.
Input is given from Standard Input in the following format:
N M a0 a1 .. aN−1 x1 y1 x2 y2 : xM yM
Print the minimum total cost required to make the forest connected, or print Impossible
if it is impossible.
7 5 1 2 3 4 5 6 7 3 0 4 0 1 2 1 3 5 6
7
If we connect vertices 0 and 5, the graph becomes connected, for the cost of 1+6=7 dollars.
5 0 3 1 4 1 5
Impossible
We can't make the graph connected.
1 0 5
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+5,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=0; i<n; i++) cin>>a[i],fa[i]=i,mi[i]=INF; for(int i=0,u,v; i<m; i++) { cin>>u>>v,u=find(u),v=find(v); if(u!=v)fa[u]=v; } for(int i=0; i<n; i++)find(i); for(int i=0; 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==1) cout<<0; else { long long s=0; for(int i=0; i<tn; i++) s+=mi[fa[i]]; while(!Q.empty()) { if(tn==2)break; t=Q.top(); Q.pop(); if(!vis[t.second]) { vis[t.second]=1; continue; } s+=t.first,--tn; } if(tn==2)cout<<s; else cout<<"Impossible"; } return 0; }
特判下impossible速度并无更快
#include<bits/stdc++.h> using namespace std; const int N=1e5+5,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=0; i<n; i++) cin>>a[i],fa[i]=i,mi[i]=INF; for(int i=0,u,v; i<m; i++) { cin>>u>>v,u=find(u),v=find(v); if(u!=v)fa[u]=v; } for(int i=0; i<n; i++)find(i); for(int i=0; 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==1) cout<<0; else if((tn-1)*2>n) cout<<"Impossible"; else { long long s=0; for(int i=0; i<tn; i++) s+=mi[fa[i]]; while(!Q.empty()) { if(tn==2)break; pii t=Q.top(); Q.pop(); if(!vis[t.second]) { vis[t.second]=1; continue; } s+=t.first,--tn; } cout<<s; } return 0; }
优化不了了,最后的代码也很棒
#include<bits/stdc++.h> using namespace std; const int N=1e5+5,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=0; cin>>n>>m; for(int i=0; i<n; i++) cin>>Q[i].first,fa[i]=i,mi[i]=INF; for(int i=0,u,v; i<m; i++) { cin>>u>>v,u=find(u),v=find(v); if(u!=v)fa[u]=v; } for(int i=0; i<n; i++) { find(i); if(!vis[fa[i]])V[tn++]=fa[i]; vis[fa[i]]=1,Q[i].second=fa[i],mi[fa[i]]=min(Q[i].first,mi[fa[i]]); } if(tn==1)cout<<0; else if((tn-1)*2>n)cout<<"Impossible"; else { long long s=0; for(int i=0; i<tn; i++)s+=mi[V[i]]; if(tn!=2)sort(Q.begin(),Q.begin()+n); for(int i=0; i<n&&tn!=2; i++) { if(vis[Q[i].second]) { vis[Q[i].second]=0; continue; } s+=Q[i].first,tn--; } cout<<s; } return 0; }
Time limit : 2sec / Memory limit : 256MB
Score : 900 points
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≤K≤N). Then, he chooses K different vertices, x0, x1, ..., xK−1, on which he installs Antenna 0, 1, ..., 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:
Find the minumum value of K, the number of antennas, when the condition is satisfied.
Input is given from Standard Input in the following format:
N a0 b0 a1 b1 : aN−2 bN−2
Print the minumum value of K, the number of antennas, when the condition is satisfied.
5 0 1 0 2 0 3 3 4
2
For example, install an antenna on Vertex 1 and 3. Then, the following five vectors are distinct:
2 0 1
1
For example, install an antenna on Vertex 0.
10 2 8 6 0 4 1 7 6 2 3 8 6 6 9 2 4 5 8
3
For example, install an antenna on Vertex 0, 4, 9.
一颗树让你找最少k个点放监控,使得全部节点到这个节点的k维空间向量(无方向)两两不一样
#include <bits/stdc++.h> using namespace std; const int N=1e5+5; vector<int>G[N]; int n,dp[N],f=-1; void dfs(int i,int p) { int F=0; for(auto j:G[i]) { if(j==p)continue; dfs(j,i); dp[i]+=dp[j]; if(!dp[j]) { if(!F)F=1; else dp[i]++; } } } int main() { scanf("%d",&n); for(int i=1,u,v; i<n; i++) scanf("%d%d",&u,&v),G[u].push_back(v),G[v].push_back(u); for(int i=0; i<n; i++) if(G[i].size()>2) { f=i; break; } if(f==-1)printf("1\n"); else dfs(f,-1),printf("%d\n",dp[f]); return 0; }