Codeforces Round # Pi (Div. 2) (STL Session ),
Codeforces Round # Pi (Div. 2)
A-Lineland Mail
Questions, fight for speed.
/** @ Author Novicer * language: C ++/C */# include <iostream> # include <sstream> # include <fstream> # include <vector> # include <list> # include <deque> # include <queue> # include <stack> # include <map> # include <set> # include <bitset> # include <algorithm> # include <cstdio> # include <cstdlib> # include <cstring> # include <cctype> # include <cmath> # include <ctime> # include <iomanip> # define INF 2147483647 # define cls (x) memset (x, 0, sizeof (x) # define rise (I, a, B) for (int I = a; I <= B; I ++) using namespace std; const double eps (1e-8); typedef long lint; const int maxn = 100000 + 5; lint x [maxn]; int main () {int n; cin> n; for (int I = 1; I <= n; I ++) scanf ("% I64d", & x [I]); sort (x + 1, x + n + 1 ); // actually, no sorting is required: cout <x [2]-x [1] <"" <x [n]-x [1] <endl; for (int I = 2; I <n; I ++) {lint mi = min (x [I]-x [I-1], x [I + 1]-x [I]); lint ma = max (x [n]-x [I], x [I]-x [1]); printf ("% I64d % I64d \ n", mi, ma );} cout <x [n]-x [n-1] <"" <x [n]-x [1] <endl; return 0 ;}
B-Berland National Library
"-X" that has not appeared at the beginning of preprocessing. Add "+ x" at the beginning"
Then, use set to simulate the process of entry and exit, and output the maximum set capacity.
/** @author Novicer* language : C++/C*/#include<iostream>#include<sstream>#include<fstream>#include<vector>#include<list>#include<deque>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cctype>#include<cmath>#include<ctime>#include<iomanip>#define INF 2147483647#define cls(x) memset(x,0,sizeof(x))#define rise(i,a,b) for(int i = a ; i <= b ; i++)using namespace std;const double eps(1e-8);typedef long long lint;bool vis[1000005];int del[105],add[105],s[105];set<int>lib;int main(){//freopen("input.txt","r",stdin);int n;cin >> n;cls(vis);cls(del);cls(add);cls(s);int ans = 0;int tmp;int x = 0;int ans1 = 0 , ans2 = 0;for(int i = 1 ; i <= n ; i++){char c;cin >> c;if(c == '+'){cin >> add[i];vis[add[i]] = true;}else{int num;cin >> num;if(!vis[num]){x++;s[x] = num;del[i] = num;}else{del[i] = num;}}}for(int i = 1 ; i <= x ; i++) lib.insert(s[i]);ans = lib.size();for(int i = 1 ; i <= n ; i++){if(add[i] != 0){lib.insert(add[i]);}else if(del[i] != 0){lib.erase(del[i]);}tmp = lib.size();ans = max(tmp , ans);}cout << ans << endl;return 0;}
C-Geometric Progression
Enumerate each element a [I] as the second item of the proportional sequence. If a [I]/k and a [I] * k exist, multiply the number of a [I]/k and a [I] * k and accumulate them.
It seems that the explanation is clearer ..
Let's solve this problem for fixed middle element of progression. This means that if we fix elementAIThen the progression must consistAIBytes/second/KAndAI·KElements. It cocould not be possible, for example, ifAIIs not divisibleK().
For fixed middle element one cocould find the number of sequences by counting how manyAIBytes/second/KElements are placed left from fixed element and how manyAI·KAre placed right from it, and then multiplying this numbers. To do this, one cocould use two associative arraysALAndAR, Where for each keyXWill be stored count of occurencesXPlaced left (or right respectively) from current element. This cocould be done with map structure.
Sum of values calculated as described above will give the answer to the problem.
/** @author Novicer* language : C++/C*/#include<iostream>#include<sstream>#include<fstream>#include<vector>#include<list>#include<deque>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cctype>#include<cmath>#include<ctime>#include<iomanip>#define INF 2147483647#define cls(x) memset(x,0,sizeof(x))#define rise(i,a,b) for(int i = a ; i <= b ; i++)using namespace std;const double eps(1e-8);typedef long long lint;const int maxn = 2 * 100000 + 5;lint a[maxn];map<lint , lint> l;map<lint , lint> r;int main(){//freopen("input.txt","r",stdin);int n;lint k;cin >> n >> k;for(int i = 1 ; i<= n ; i++){scanf("%I64d",&a[i]);r[a[i]]++;}lint ans = 0;for(int i = 1 ; i <= n ; i++){r[a[i]]--;if(a[i] % k == 0)ans += l[a[i]/k] * r[a[i]*k];l[a[i]]++;}cout << ans << endl;return 0;}
D-One-dimen1_battle Ships
An interesting question is that the space with a length of n contains k ships with a length of a, and the ships do not overlap. Alice gives you m positions that claim miss (that is, no ship, judge whether Alice is lying. If Alice is lying, the output can identify the location of the conflict for the first time.
Ideas:
The solution is awesome! Simply put, Alice's location is determined to divide the space into two parts. It is calculated that each of these two parts can accommodate several ships. In addition, if they are greater than k, they will lie.
For problem D, simply use a map structure to record the free space (a consecutive empty square without shotpoint) on a shotpoint's left and right.
You can use the lower_bound and upper_bound functions to find the nearest shotpoint, and update them.
When a shot happens a free space will be broken into 2, so calculate the sum of max number of ships of those 2 spaces and subtract it from the global sum of max number of ships.
Calculate the maximum number of ships cocould be placed in the space: (space + 1)/(a + 1)
(Global sum of max number of ships = (n + 1)/(a + 1) before the 1st shot .)
When the global sum <k just print the current operation number and exit.
/** @ Author Novicer * language: C ++/C */# include <iostream> # include <sstream> # include <fstream> # include <vector> # include <list> # include <deque> # include <queue> # include <stack> # include <map> # include <set> # include <bitset> # include <algorithm> # include <cstdio> # include <cstdlib> # include <cstring> # include <cctype> # include <cmath> # include <ctime> # include <iomanip> # define INF 2147483647 # define cls (x) memset (x, 0, sizeof (x) # de Fine rise (I, a, B) for (int I = a; I <= B; I ++) using namespace std; const double eps (1e-8 ); typedef long lint; map <int, int> ship; int n, k, a; int f (int x) {return (x + 1) /(a + 1) ;}; // The calculated length is x range, which can accommodate several ships int main () {// freopen ("input.txt", "r ", stdin); while (cin> n> k> a) {ship. clear (); int m; cin> m; ship [0] = ship [n + 1] = 1; int max_sum = f (n); bool cheat = false; for (int I = 1; I <= m; I ++) {int shot; scanf (" % D ", & shot); if (ship. find (shot )! = Ship. end () continue; ship [shot] = 1; int l, r; map <int, int >:: iterator low = ship. lower_bound (shot); map <int, int >:: iterator up = ship. upper_bound (shot); // The left and right positions of low-1 and up are respectively closest to shot. l = (-- low)-> first; r = up-> first; max_sum-= f (r-l-1); max_sum + = f (r-shot-1) + f (shot-l-1); if (max_sum <k) {cheat = true; cout <I <endl; return 0 ;}} if (! Cheat) cout <-1 <endl;} return 0 ;}
Copyright statement: the blogger authorizes everyone to reprint everything :)