A: Smooth Sequences
题目描述
输入
They are positive integers in the range [1, 1000]. Then n integers, separated with space(s),follow in the next line.
Note that the test data file may contain more than one instance. The last instance is followed by a line containing a single 0.
输出
样例输入
3 2
2 4 6
3 1
3 5 7
0
样例输出
Y
N
提示
1.All integers in the sequence have absolute values bounded by 220.
2.1≤n,d≤1000.
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5+5; int main() { int n,d; while(1) { scanf("%d",&n); if(n==0) break; scanf("%d",&d); int a[maxn]; int cnt = 0; scanf("%d",&a[0]); int b[maxn]={0}; for(int i=1;i<n;i++) { scanf("%d",&a[i]); if(abs(a[i]-a[i-1])>d) { b[cnt++] = i; } } if(cnt>=3) { printf("N\n"); continue; } else if(cnt==2) { if(b[0]+1==b[1]) { int t = b[1]; if(a[t]-a[t-2]<=2*d) { printf("Y\n"); continue; } else { printf("N\n"); continue; } } else { printf("N\n"); } } else if(cnt==1) { if(b[0]==0||b[0]==n-1) { printf("Y\n"); continue; } else { int t = b[0]; if(abs(a[t]-a[t-2])<=2*d||(a[t+2]-a[t])<=2*d) { cout<<"Y\n"; continue; } else { printf("N\n"); continue; } } } else { printf("Y\n"); continue; } } return 0; }
思维题
——————————————分割线————————————————
B: Threesome
题目描述
Your task is to write a computer program to compute the sum of their mutual social connectivities of a best threesome.
输入
For each test case, the association S = {a1 , a2 ,..., an } is given with the following format:
The first line contains a positive integer n. In the following n(n−1)/2 lines, each line contains three integers representing i, j, and c(i, j) such that any two integers are separated by a space.
输出
样例输入
2
3
1 2 1
1 3 1
2 3 1
4
1 2 2
1 3 2
1 4 1
2 3 2
2 4 1
3 4 1
样例输出
3
6
提示
• 3 ≤ n ≤ 300
• Each ID is at least 1 and at most n.
• For any two students i and j, 1 ≤ c(i, j) ≤ 10000.
#include<bits/stdc++.h> using namespace std; typedef long long ll; int ans[320][320]; int n; void init() { for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { ans[i][j]=0; } } } int main() { int t,a,b,c; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=1; i<=n*(n-1)/2; i++) { scanf("%d %d %d",&a,&b,&c); ans[a][b]=c; ans[b][a]=c; } int maxx=0; for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { for(int k=1; k<=n; k++) { if(i!=j&&i!=k&&j!=k) maxx=max(maxx,ans[i][j]+ans[j][k]+ans[i][k]); } } } printf("%d\n",maxx); init(); } return 0; }
——————————————分割线————————————————
C: Coefficient Computation
题目描述
n!/(k!(n−k)!). Furthermore,in combinatorics, it is the number of ways to choose a subset of k items from a set of n items given that the order of selection does not matter. There are several ways to compute C(n, k).
The above formula is factorial formula. And the following formula is the recursive formula,
输入
输出
样例输入
3
20 10 3
150 50 8
10 2 7
样例输出
100101102211
354470761204215171415303750634427342737334454
63
提示
1.The number of test cases is no larger than 10.
2.n is an integer and 0 ≤ n ≤ 300.
3.k is an integer and 0 ≤ k ≤ n.
4.d is an integer and 1 < d < 10.
import java.util.*; import java.math.BigInteger; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner cin = new Scanner(System.in); int t = cin.nextInt(); BigInteger ans,tmp,p; int n,k,d; String str; while (t-- != 0) { tmp=BigInteger.valueOf(1); ans=BigInteger.valueOf(1); n=cin.nextInt(); k=cin.nextInt(); d=cin.nextInt(); if(k==0||k==n) { ans=BigInteger.valueOf(1); } else if(k==1) { ans=BigInteger.valueOf(n); } else { for(int i=n;i>=n-k+1;i--) { p=BigInteger.valueOf(i); ans=ans.multiply(p); } for(int i=1;i<=k;i++) { p=BigInteger.valueOf(i); tmp=tmp.multiply(p); } ans=ans.divide(tmp); } str=ans.toString(d); System.out.println(str); } } }
队友写的java 我并不会
——————————————分割线————————————————
D: Network Report
Note that if multiple shortest paths from node i to node j (i ≠ j) exist, they should collectively count as one. For example, though there are two shortest paths from node 0 to node 4, only one path is counted. Please write a program to output the above summary report from an input connected graph.
输入
输出
样例输入
3
3
0 1
1 2
2 0
5
5
0 1
1 2
1 3
2 4
3 4
0
样例输出
1 6
1 10
2 8
3 2
提示
1. 2 ≤ n ≤ 200, nodes are numbered as 0, 1, 2, . . . , n − 1.
2. Any counting number is within the range of integer.
floyd 最短路
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int INF=1e7; int dis[220][220]; int ans[220]; void init(int n) { for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { dis[i][j]=INF; } dis[i][i]=0; } memset(ans,0,sizeof(ans)); } int main() { int n,m; while(1) { scanf("%d",&n); if(n==0) { break; } init(n); scanf("%d",&m); int a,b; for(int i=0; i<m; i++) { scanf("%d %d",&a,&b); dis[a][b]=dis[b][a]=1; } for(int k=0; k<n; k++) { for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); } } } for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { if(dis[i][j]!=INF) { ans[dis[i][j]]++; } } } for(int i=1;i<=n;i++) { if(ans[i]) { printf("%d %d\n",i,ans[i]); } } } return 0; }
——————————————分割线————————————————
H: A Partial Order Relation
题目描述
Let’s define a more restrictive binary relation called greatest divisibility relation . Given a number a. Let div(a) be a set of all divisors of a minus a. Then we define a b if b is a divisor of a but b cannot divide any other numbers in div(a). For example, div(30) = {1, 2, 3, 5, 6, 10, 15}. 30 6 because 6 cannot be used to divide other elements in the set.
Given a number, you can construct relation among the its divisors as in Fig. 1 for 120. The depth of the graph from root node (120) to the terminal node (always 1) is 5 and
the number of edges are 28. Given an integer, please compute the number of edges in its relation graph.
输入
输出
样例输入
2
6
120
样例输出
4 28
分析:(分解质因数问题)
有规律:n = 120时 有3个2的质数子数 1个3 1个5
若一定有2 有 3*2*2种 (3、5可以选也可以不选)
若一定有3 1*4*2
若一定有5 1*4*2
共28种 即有28条边
代码:
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll n; int cnt; const int maxn = 1e5+5; int c[maxn]; void f() { cnt = 0; for(ll i=2;i<=sqrt(n);i++) { if(n%i==0) { cnt++; while(n%i==0) { n/=i; c[cnt]++; } } } if(n>1) { cnt++; c[cnt] = 1; } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int T; cin>>T; while(T--) { memset(c,0,sizeof(c)); cin>>n; if(n==1) { cout<<0<<endl; continue; } f(); // for(int i=1;i<=cnt;i++) // { // cout<<c[i]<<" "; // } // cout<<endl; ll ans = 0; for(int i=1;i<=cnt;i++) { ll tmp = c[i]; for(int j=1;j<=cnt;j++) { if(i!=j) { tmp*=c[j]+1; } } ans+=tmp; } cout<<ans<<endl; } }
——————————————分割线————————————————
I: Tracer Deployment
题目描述
For example, if tracers are deployed at node A and node C, when an attack initiates at node a, passing through node B, and finally attacks node c, no tracer will detect the above attack. On the other hand, if three tracers are deployed at nodes A, B, and b, then any attack will always pass through at least one of the three tracer nodes, and the attack will be detected.
Please write a program to determine the minimum number of tracers need to be deployed to ensure any attck will pass through (and be detected) at least one tracer. Note that the given network is a bipartite network G = (S, T, E) whose nodes can be partitioned into two subsets S and T so that each communication link (in E) has one end in S and one end in T. Also note that the network may be disconnected or even have isolated nodes.
输入
输出
样例输入
4
3
6
0 0
0 1
1 0
1 1
1 2
2 1
4
4
7
0 0
0 2
1 1
1 3
2 0
2 2
3 3
0
样例输出
3
4
#include<bits/stdc++.h> using namespace std; struct node { int to,next; }edge[150]; int head[150],tot,match[150]; bool visit[150]; void init() { memset(head,-1,sizeof(head)); memset(match,-1,sizeof(match)); tot=0; } void addedge(int u,int v) { edge[tot].to=v; edge[tot].next=head[u]; head[u]=tot++; } bool dfs(int u) { for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(!visit[v]) { visit[v]=1; if(match[v]==-1||dfs(match[v])) { match[v]=u; return 1; } } } return 0; } int main() { //freopen("in.txt","r",stdin); int n,m,e; while(1) { init(); int ans=0; scanf("%d",&n); if(n==0) { break; } scanf("%d",&m); scanf("%d",&e); int a,b; for(int i=1;i<=e;i++) { scanf("%d %d",&a,&b); addedge(a,b); } for(int i=0;i<n;i++) { memset(visit,0,sizeof(visit)); if(dfs(i)) { ans++; } } printf("%d\n",ans); } return 0; }
K: Assigning Frequencies
题目描述
However, only three frequencies are available to him.
Please determine whether Bob can assign frequencies to all satellites to avoid any interference.
输入
输出
样例输入
4
6
6
0 3
1 5
3 2
2 5
0 4
1 0
7
12
6 5
0 3
2 6
3 5
5 0
0 4
4 5
6 3
1 4
1 2
3 4
2 3
7
8
6 5
0 3
2 6
3 5
1 4
1 2
3 4
2 3
6
9
0 1
1 2
2 3
5 2
5 3
3 4
2 4
1 4
4 5
样例输出
Y
N
Y
N
染色问题 dfs 看别人代码 一下就会 但是自己就是写不出 自己太菜了
#include <bits/stdc++.h> using namespace std; int n,m; int Map[55][55]; int c[55]; int dfs(int now) { for(int i=0;i<now;i++) { if(Map[now][i]&&now!=i&&c[now]==c[i]) { return false; } } if(now==n-1) return true; for(int i=0;i<3;i++) { c[now+1] = i; if(dfs(now+1)) return true; } } int main() { int T; cin>>T; while(T--) { memset(Map,0,sizeof(Map)); cin>>n>>m; for(int i=0;i<m;i++) { int x,y; cin>>x>>y; Map[x][y] = Map[y][x] = 1; } int flag = dfs(0); if(flag) cout<<"Y"<<endl; else cout<<"N"<<endl; } return 0; }