Question:
Greedy, looking for a point that satisfies at least two heights next to it, then dropping one height point until a total drop of K points.
Code:
/*�ջ�*/#include<iostream>#include<cstdlib>#include<vector>#include<map>#include<cstring>#include<set>#include<string>#include<algorithm>#include<sstream>#include<ctype.h>#include<fstream>#include<string.h>#include<stdio.h>#include<math.h>#include<stack>#include<queue>#include<ctime>//#include<conio.h>using namespace std;const int INF_MAX=0x7FFFFFFF;const int INF_MIN=-(1<<30);const double eps=1e-10;const double pi=acos(-1.0);#define pb push_back //a.pb( )#define chmin(a,b) ((a)<(b)?(a):(b))#define chmax(a,b) ((a)>(b)?(a):(b))template<class T> inline T gcd(T a,T b)//NOTES:gcd( {if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}template<class T> inline T lcm(T a,T b)//NOTES:lcm( {if(a<0)return lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));}typedef pair<int, int> PII;typedef vector<PII> VPII;typedef vector<int> VI;typedef vector<VI> VVI;typedef long long LL;int dir_4[4][2]={{0,1},{-1,0},{0,-1},{1,0}};int dir_8[8][2]={{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1},{1,0},{1,1}};//�£����£������ϣ��ϣ����ϣ��ң����¡�//******* WATER ****************************************************************int arr[300];int main() { int n, k, nn; while(~scanf("%d%d", &n, &k)) { nn = (n << 1) + 1; for(int i = 1; i <= nn; i++) scanf("%d", &arr[i]); for(int i = 2; i <= nn; i += 2) { if(arr[i] > arr[i - 1] + 1 && arr[i] > arr[i + 1] + 1) { arr[i]--; k--; } if(k == 0) break; } printf("%d", arr[1]); for(int i = 2; i <= nn; i++) printf(" %d", arr[i]); cout << endl; } return 0;}
Question B:
Direct greedy: the application with the highest priority or the smallest priority.
Code:
/* Gains: */# include <iostream> # include <cstdlib> # include <vector> # include <map> # include <cstring> # include <set> # include <string> # include <algorithm> # include <sstream> # include <ctype. h> # include <fstream> # include <string. h> # include <stdio. h> # include <math. h> # include <stack> # include <queue> # include <ctime> // # include <conio. h> using namespace STD; const int inf_max = 0x7fffff; const int inf_min =-(1 <30); const double EPS = 1e-10; Const double Pi = ACOs (-1.0); # define Pb push_back // A. Pb () # define chmin (A, B) (a) <(B )? (A) :( B) # define chmax (A, B) (a)> (B )? (A) :( B) template <class T> inline t gcd (t a, t B) // notes: gcd ({if (a <0) return gcd (-a, B); If (B <0) return gcd (A,-B); Return (B = 0 )? A: gcd (B, A % B);} template <class T> inline t lcm (t a, t B) // notes: lcm ({if (a <0) return lcm (-a, B); If (B <0) return lcm (A,-B); return a * (B/gcd (A, B ));} typedef pair <int, int> PII; typedef vector <PII> vpii; typedef vector <int> VI; typedef vector <VI> VVI; typedef long ll; int dir_4 [4] [2] = {}, {-}, {0,-1 }}; int dir_8 [8] [2] = {}, {-1,-1}, {0,-1 }, {1,-1}, {}, {}; // bottom, bottom left, left, top left, top right, top right, right bottom. ****************************** * ********************************* Int arr [1100]; int n, m; priority_queue <int> PQ; void debug () {for (INT I = 0; I <m; I ++) cout <arr [I]; cout <Endl ;}int get_min () {int nn = N; int ret = 0; // debug (); sort (ARR, arr + M ); // debug (); For (INT I = 0; I <m; I ++) {// cout <"%" <arr [I] <Endl; // cout <RET <Endl; int Cal = (ARR [I] + 1) * arr [I]/ 2; If (NN> arr [I]) {Nn-= arr [I]; RET + = Cal ;} else {RET + = (ARR [I] + arr [I]-nn + 1) * NN/2; return ret ;}} int get_max () {int ret = 0; int CNT = N; while (! PQ. empty () PQ. pop (); For (INT I = 0; I <m; I ++) PQ. push (ARR [I]); While (CNT) {int TMP = PQ. top (); PQ. pop (); // cout <TMP <Endl; RET + = TMP; PQ. push (TMP-1); CNT --;} return ret;} int main () {scanf ("% d", & N, & M ); for (INT I = 0; I <m; I ++) scanf ("% d", & arr [I]); int Minx = get_min (); // cout <Minx <Endl; int Maxx = get_max (); printf ("% d \ n", Maxx, Minx); Return 0 ;}
Question C:
Create a graph and obtain the number of connected subgraphs.
/* Gains: */# include <iostream> # include <cstdlib> # include <vector> # include <map> # include <cstring> # include <set> # include <string> # include <algorithm> # include <sstream> # include <ctype. h> # include <fstream> # include <string. h> # include <stdio. h> # include <math. h> # include <stack> # include <queue> # include <ctime> // # include <conio. h> using namespace STD; const int inf_max = 0x7fffff; const int inf_min =-(1 <30); const double EPS = 1e-10; Const double Pi = ACOs (-1.0); # define Pb push_back // A. Pb () # define chmin (A, B) (a) <(B )? (A) :( B) # define chmax (A, B) (a)> (B )? (A) :( B) template <class T> inline t gcd (t a, t B) // notes: gcd ({if (a <0) return gcd (-a, B); If (B <0) return gcd (A,-B); Return (B = 0 )? A: gcd (B, A % B);} template <class T> inline t lcm (t a, t B) // notes: lcm ({if (a <0) return lcm (-a, B); If (B <0) return lcm (A,-B); return a * (B/gcd (A, B ));} typedef pair <int, int> PII; typedef vector <PII> vpii; typedef vector <int> VI; typedef vector <VI> VVI; typedef long ll; int dir_4 [4] [2] = {}, {-}, {0,-1 }}; int dir_8 [8] [2] = {}, {-1,-1}, {0,-1 }, {1,-1}, {}, {}; // bottom, bottom left, left, top left, top right, top right, right bottom. ****************************** * ********************************* Int A [110]; int B [110]; bool mat [110] [110]; int N; int main () {scanf ("% d", & N ); for (INT I = 0; I <n; I ++) scanf ("% d", & A [I], & B [I]); memset (MAT, 0, sizeof (MAT); For (INT I = 0; I <n; I ++) mat [I] [I] = true; for (INT I = 0; I <n; I ++) {for (Int J = 0; j <n; j ++) {if (a [I] = A [J] | B [I] = B [J]) mat [I] [J] = mat [J] [I] = True ;}}for (int K = 0; k <n; k ++) {for (INT I = 0; I <n; I ++) {for (Int J = 0; j <n; j ++) {If (MAT [I] [k] & mat [k] [J]) mat [I] [J] = mat [J] [I] = true ;}} int ans = 0; For (INT I = 0; I <N; I ++) {bool flag = false; For (Int J = 0; j <I; j ++) {If (MAT [I] [J]) flag = true;} If (! Flag) ans ++;} printf ("% d \ n", ANS-1); Return 0 ;}
You can also use DSU (disjoint set Union set) to merge and query the set.
/* Gains: */# include <iostream> # include <cstdlib> # include <vector> # include <map> # include <cstring> # include <set> # include <string> # include <algorithm> # include <sstream> # include <ctype. h> # include <fstream> # include <string. h> # include <stdio. h> # include <math. h> # include <stack> # include <queue> # include <ctime> // # include <conio. h> using namespace STD; const int inf_max = 0x7fffff; const int inf_min =-(1 <30); const double EPS = 1e-10; Const double Pi = ACOs (-1.0); # define Pb push_back // A. Pb () # define chmin (A, B) (a) <(B )? (A) :( B) # define chmax (A, B) (a)> (B )? (A) :( B) template <class T> inline t gcd (t a, t B) // notes: gcd ({if (a <0) return gcd (-a, B); If (B <0) return gcd (A,-B); Return (B = 0 )? A: gcd (B, A % B);} template <class T> inline t lcm (t a, t B) // notes: lcm ({if (a <0) return lcm (-a, B); If (B <0) return lcm (A,-B); return a * (B/gcd (A, B ));} typedef pair <int, int> PII; typedef vector <PII> vpii; typedef vector <int> VI; typedef vector <VI> VVI; typedef long ll; int dir_4 [4] [2] = {}, {-}, {0,-1 }}; int dir_8 [8] [2] = {}, {-1,-1}, {0,-1 }, {1,-1}, {}, {}; // bottom, bottom left, left, top left, top right, top right, right bottom. ****************************** * ********************************* Int father [110]; int X [110], Y [110]; int get_father (INT p) {return father [p] = P? P: Father [p] = get_father (father [p]);} void mergep (int A, int B) {int c = get_father (); int d = get_father (B); father [c] = D; // cout <"here" <Endl ;}int main () {int N, ANS = 0; scanf ("% d", & N); // memset (father,-1, sizeof (father); For (INT I = 0; I <N; I ++) scanf ("% d", & X [I], & Y [I]); For (INT I = 0; I <N; I ++) Father [I] = I; for (INT I = 0; I <n; I ++) {for (Int J = 0; j <I; j ++) {If (X [I] = x [J] | Y [I] = Y [J]) mergep (I, j );}} for (INT I = 0; I <n; I ++) {If (father [I] = I | father [I] =-1) {ans ++ ;}} printf ("% d \ n", ANS-1); Return 0 ;}