574a-bear and Elections
Test instructions
Enter a number n, and then enter n positive integers.
The title requires the first integer to be greater than the remaining integers, and the remaining integers can be reduced by 1 and added to the first number at a time.
Ask at least how many times it takes to meet the requirements.
Ideas:
Use the priority queue to maintain it.
#include <stdio.h>#include <algorithm>#include <iostream>#include <string.h>#include <queue>using namespace STD;intMain () {intN, A; while(~scanf("%d", &n)) {scanf("%d", &a); priority_queue<int> PQ; for(inti =1; I < n; i++) {inttmpscanf("%d", &tmp); Pq.push (TMP); }intc =0;inttemp = Pq.top (); while(Temp >= a) {Pq.pop (); ++a; --temp; Pq.push (temp); ++c; temp = Pq.top (); }printf("%d\n", c); }}
574b-bear and Three Musketeers
Test instructions
Enter two digits n, M. Represents the relationship of N man, M to man.
Then enter the relationship between the M-line two people.
The number of the existing three persons is a,b,c.
Make the a,b,c relationship a loop and ask them the minimum number of three people who have relationships with others?
Ideas:
As the M maximum value is 4000, you can use VECOTR to store the relationship between two people, and use a deep search method to find the loop, take the minimum value.
#include <stdio.h>#include <algorithm>#include <iostream>#include <string.h>#include <queue>#include <vector>#include <stdlib.h>using namespace STD; vector<int>v[4001];#define INF 2000000000inta[Ten];intResBOOLvis[4001];voidDfsintDeepints) {if(Deep = =3&& Binary_search (V[s].begin (), V[s].end (), a[0])) {//printf ("%d%d%d\n", a[0], a[1], a[2]);//printf ("%d%d%d\n", V[a[0]].size (), V[a[1]].size (), v[a[2]].size ()); intf = v[a[0]].size () + v[a[1]].size () + v[a[2]].size ()-6;//printf ("[%d]\n", f);res = min (res, f);return; }if(Deep = =3)return;intSZ = V[s].size (); for(inti =0; I < sz; i++) {if(Vis[v[s][i]]) {Vis[v[s][i]] =false; A[deep] = V[s][i]; DFS (deep +1, V[s][i]); Vis[v[s][i]] =true; } }}intMain () {intN, M; while(~scanf("%d%d", &n, &m)) { for(inti =1; I <= N; i++) {v[i].clear (); } for(inti =0; I < m; i++) {intx, y;scanf("%d%d", &x, &y); V[x].push_back (y); V[y].push_back (x); } for(inti =1; I <= N; i++) {sort (V[i].begin (), V[i].end ()); } res = INF;memset(Vis,true,sizeof(VIS)); for(inti =1; I <= N; i++) {if(V[i].size ()) {a[0] = i; Vis[i] =false; Dfs1, i); Vis[i] =true; } }if(res = = INF)puts("-1");Else printf("%d\n", res); }}
573a-bear and Poker
Test instructions
Enter a number n, and then enter n positive integers.
Each integer AI can become ai * 2k or AI * 3k, or it can be unchanged.
Ask if the n numbers can be transformed and all the same.
Ideas:
Remove the 2 and 3 factors from each number to see if they are the same.
#include <stdio.h>#include <algorithm>#include <iostream>#include <string.h>#include <queue>using namespace STD;Long Longa[100005];intMain () {intN while(~scanf("%d", &n)) { for(inti =0; I < n; i++) {scanf("%i64d", &a[i]); while(A[i]%3==0) {A[i]/=3; } while(A[i]%2==0) {A[i]/=2; }} sort (A, a+n);if(a[0] = = a[n-1])puts("Yes");Else puts("No"); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Codeforces Round #318 (Div. 2) A, B, C