Codeforces #263 div2 solution report
A. Appleman and Easy Task
Resolution:
To determine whether each cell is surrounded by an even number of adjacent cells.
Code:
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; # define Lowbit (x) & (-(x ))) # define ll long # define mp make_pair # define ff first # define ss second # define pb push_backconst int MAXN = 1005; ll a [30]; int n; char str [105] [105]; bool check (int I, int j) {int tmp = 0; if (I> 0 & str [I-1] [j] = 'O') + + tmp; if (I
0 & str [I] [J-1] = 'O') + + tmp; if (j
B. Appleman and Card Game
Resolution:
Two questions, greedy problem, count the number of times each letter appears directly, then sort, get the maximum each time.
However, pay attention to the data range. The result is represented by long, which requires forced type conversion during the calculation process. k must be of the long type in the calculation.
Code:
//#define LOCAL#include
#include
#include
#include
#include
#include #include
#include
#include
using namespace std;#define Lowbit(x) ((x)&(-(x)))#define ll long long#define mp make_pair#define ff first#define ss second#define pb push_backconst int MAXN=1005;ll a[30];char str[100010];int main(){ #ifdef LOCAL freopen(1.in, r,stdin); //freopen(1.out, w, stdout); #endif int n; ll k; scanf(%d%I64d, &n, &k); scanf(%s, str); int len = strlen(str); memset(a, 0, sizeof(a)); for(int i=0; i
=0&&k>0; --i){ if(k>=a[i]){ sum += a[i]*a[i]; k -= a[i]; } else{ sum += k*k; k-=k; } } printf(%I64d, sum); return 0;}
C. Appleman and Toastman
Resolution:
Three questions: greedy. Each time you separate the smallest number, you can use sort or priority_queue.
Code:
//#define LOCAL#include
#include
#include
#include
#include
#include #include
#include
#include
using namespace std;#define Lowbit(x) ((x)&(-(x)))#define ll long long#define mp make_pair#define ff first#define ss second#define pb push_backconst int MAXN=1005;int main(){ #ifdef LOCAL freopen(1.in, r,stdin); //freopen(1.out, w, stdout); #endif int i,n; ll sum = 0; ll score = 0,tmp; priority_queue< ll, vector
, greater
>pq; scanf(%d, &n); for(i=0; i
1){ score += sum; tmp = pq.top(); pq.pop(); sum -= tmp; } printf(%I64d, score); return 0;}
D. Appleman and Tree
Resolution:
This is a DP problem. tree-like DP is used;
The color of a tree and each node is given. 1 indicates black and 0 indicates white. The requirement is that if k trees are split, each tree has a black node.
Dp [v] [0] indicates the number of non-black node Subtrees with v as the root.
Dp [v] [1] indicates the number of black node Subtrees with v as the root
To be honest, I was still quite guilty when I encountered DP, so I found this was a DP problem during the competition, so I was too lazy to use the drunk brain, and I went straight to GG.
Code:
// # Define LOCAL # include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; # define Lowbit (x) & (-(x ))) # define ll long # define mp make_pair # define ff first # define ss second # define pb push_back # define mod 1000000007 const int MAXN = 100010; ll dp [MAXN] [2]; vector
X [MAXN]; int c [MAXN]; void dfs (int v, int p) {dp [v] [0] = 1; dp [v] [1] = 0; for (int I = 0; I
E. Appleman and a Sheet of Paper
Resolution:
To be honest, this question does not need to be read much at all. You can see the analysis of the example to understand the question. Is to simply fold the paper, and then query the total thickness of the paper in the interval
BIT (tree array) or line segment tree can be used here.
The code here is a tree array.
A clever answer to this question is that if the stack on the left is long, we can fold the stack on the right in turn, but the left and right directions of the paper will be switched, therefore, a flag is used to mark the left and right directions. The other part is the same as the common tree array.
Code:
// # Define LOCAL # include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; # define Lowbit (x) & (-(x ))) // # define ll long # define mp make_pair # define ff first # define ss second # define pb push_back # define mod 1000000007 const int MAXN = 100010; int c [MAXN], s [MAXN], n; void ADD (int p, int val) {s [p] + = val; while (p <= n) {c [p] + = val; p + = Lowbit (p) ;}} int getsum (int p) {int sum = 0; while (p> 0) {sum + = c [p]; p-= Lowbit (p);} return sum;} int main () {# ifdef LOCAL freopen (1.in, r, stdin ); // freopen (1.out, w, stdout); # endif int I, p; scanf (% d, & n, & p); memset (c, 0, sizeof (c); memset (s, 0, sizeof (s); for (I = 1; I <= n; ++ I) ADD (I, 1 ); int l = 1, r = n; int x, y, z; int flag = 0; for (int k = 0; k
(R-l + 1); int mid; if (flag) mid = r-y; else mid = l + Y-1; int ll = mid-l + 1; int rr = r-mid; if (ll <= rr) {for (I = l; I <= mid; ++ I) ADD (2 * mid + 1-i, s [I]); l = mid + 1;} else {for (I = mid + 1; I <= r; ++ I) ADD (2 * mid + 1-i, s [I]); r = mid;} flag ^ = fg; // mark. If the left side is long, stack it to the left side and read it from the right to the left; // If the left is short, stack it to the right and read it from the left to the right. } Else {scanf (% d, & y, & z); if (flag) printf (% d, getsum (r-y) -getsum (r-z); else printf (% d, getsum (l + Z-1)-getsum (l + Y-1) ;}} return 0 ;}