Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some cpp #25

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
56 changes: 56 additions & 0 deletions volume007/714 - Copying Books(v2).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <iostream>
#include <cstring>
using namespace std;

const int maxn = 505;
long long num[maxn], sum[maxn];
char idx[maxn],a[maxn];
long long leftt,rightt,mid;
int m,k,n;

inline bool binSearch(){
long long val = 0; int cnt = 0;
memset(idx, 0, sizeof(idx));
for(int i = m; i >= 1; --i){
if(val+num[i] <= mid && i >= k-cnt)
val += num[i];
else{
cnt++;
val = num[i];
idx[i] = 1;
}
}
if(cnt == k-1){
memcpy(a, idx ,sizeof(idx));
return true;
}
return false;
}

int main(){
ios::sync_with_stdio(false);
cin.tie(0);

int cas;
cin >> cas;
while(cas--){
rightt = leftt = 0;
cin >> m >> k;
for(int i = 1; i <= m; ++i){
cin >> num[i];
rightt += num[i];
leftt = num[i]>leftt ? num[i]:leftt;
}
while(rightt >= leftt){
mid = (leftt+rightt) >> 1;
if(binSearch()) rightt = mid -1;
else leftt = mid+1;
}
for(int i = 1; i < m; ++i){
cout << num[i] << ' ';
if (a[i] == 1) cout << '/' << ' ';
}
cout << num[m] << '\n';
}
return 0;
}
36 changes: 36 additions & 0 deletions volume014/1442 - Cav.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <iostream>
#include <cstdio>
using namespace std;

const int maxn = 1e6+5;
int cas, n, cnt;
int ceiling[maxn], floor[maxn];

int main() {
ios::sync_with_stdio(false);
cin.tie(0);
//freopen("input.txt" , "r", stdin );
//freopen("output.txt", "w", stdout);
cin >> cas;
while(cas--) {
cnt = 0;
cin >> n;
for (int i = 0; i < n; ++i) cin >> floor[i];
for (int i = 0; i < n; ++i) cin >> ceiling[i];
int tmp = maxn;
for (int i = 0; i < n; ++i) {
tmp = min(tmp, ceiling[i]);
tmp = max(tmp, floor[i]);
ceiling[i] = tmp;
}
tmp = maxn;
for (int i = n-1; i > -1; --i) {
tmp = min(tmp, ceiling[i]);
tmp = max(tmp, floor[i]);
ceiling[i] = tmp;
cnt += ceiling[i] - floor[i];
}
cout << cnt << '\n';
}
return 0;
}
51 changes: 51 additions & 0 deletions volume014/1451 - Average(v2).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <iostream>
#include <string>
using namespace std;

const int maxn = 100005;
int n, L, start, ending,temp;
double maxd;
int DNA[maxn],cav[maxn];
string str;

inline int cntAverage(int L, int r, int LL, int rr, int DNA[]){
return (DNA[r]-DNA[L])*(rr-LL) - (DNA[rr]-DNA[LL])*(r-L);
}

inline void changePoint(int pits,int bump){
double temp;
temp = cntAverage(pits, bump, start-1, ending, DNA);
if (temp < 0) return;
if (temp || (bump-pits) < (ending-start+1)){
maxd = temp;
start = pits + 1;
ending = bump;
}
}

int main(){
ios::sync_with_stdio(false);
cin.tie(0);

int cas;
cin >> cas;
while (cas--){
int find = 0, cor = -1;
cin >> n >> L; cin.get();
getline(cin, str);
for (int i = 1; i <= n; ++i)
DNA[i] = DNA[i-1] + (str[i-1] == '1');
maxd = DNA[L] / L, start = 1, ending = L;
for (int i = L; i <= n; ++i){
temp = i - L;
while (find < cor && cntAverage(cav[cor], temp, cav[cor - 1], cav[cor], DNA) <= 0)
--cor;
cav[++cor] = temp;
while (find < cor && cntAverage(cav[find], i, cav[find + 1], i, DNA) <= 0)
++find;
changePoint(cav[find],i);
}
cout << start << ' ' << ending << '\n';
}
return 0;
}
41 changes: 41 additions & 0 deletions volume014/1471 - Defense Lines.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

const int maxn = 2*1e5+1;
int n,ans;
int a[maxn],g[maxn],f[maxn],bin[maxn];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);

int cas;
cin >> cas;
while (cas--){
cin >> n;
for (int i = 1; i <= n; ++i){
cin >> a[i];
if (a[i]>a[i - 1])
g[i] = g[i - 1] + 1;
else g[i] = 1;
}
a[n + 1] = 0;
for (int i = n; i > 0; --i){
if (a[i] < a[i + 1])
f[i] = f[i + 1] + 1;
else f[i] = 1;
}

ans = 0;
memset(bin, 0x7f, sizeof(bin));
bin[0] = -1;
for (int i = 1; i <= n; ++i){
ans = max(ans, int(f[i]+lower_bound(bin, bin+n, a[i])-bin-1));
bin[g[i]] = min(bin[g[i]], a[i]);
}

cout << ans << '\n';
}
return 0;
}
68 changes: 68 additions & 0 deletions volume016/1606 - Amphiphilic Carbon Molecules.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;

const int N=1001;

class Point{
public:
int x,y,color;
double rad;
bool operator<(const Point &rhs)const{
return rad<rhs.rad;
}
}dot[N],tempDot[N];

int n, num;

bool isInArea(Point A,Point B){
return A.x*B.y-A.y*B.x>=0;
}

int main(){
ios::sync_with_stdio(false);
cin.tie(0);

while(cin>>n && n){
for(int i=0;i<(n);i++)
cin >> dot[i].x >> dot[i].y >> dot[i].color;
if(n<=2) return 2;
int ans=0;

for(int i=0;i<n;i++){
num=0;
for(int j=0;j<n;j++){
if(j == i) continue;
tempDot[num].x=dot[j].x-dot[i].x;
tempDot[num].y=dot[j].y-dot[i].y;
if(dot[j].color){
tempDot[num].x=-tempDot[num].x;
tempDot[num].y=-tempDot[num].y;
}
tempDot[num].rad=atan2(tempDot[num].y,tempDot[num].x);
num++;
}
sort(tempDot,tempDot+num);

int divi=0,scani=0,cnt=2;
while(divi<num){
if(scani==divi){
scani=scani+1;
if(scani >= num) scani = scani-num;
cnt++;
}
while(scani!=divi && isInArea(tempDot[divi],tempDot[scani])){
scani=scani+1;
if(scani >= num) scani = scani-num;
cnt++;
}
cnt--;
divi++;
ans=max(ans,cnt);
}
}
cout << ans << '\n';
}
return 0;
}
50 changes: 50 additions & 0 deletions volume016/1607 - Gates.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include<iostream>
using namespace std;

#define LL gate[i].in1
#define RR gate[i].in2

class gateClass {
public:
int in1, in2, out;
} gate[200005];
int n, m, all0, all1;

int getOutput(int pos) {
for (int i = 1; i <= m; i++) {
int x = LL>0 ? gate[LL].out:-LL<=pos;
int y = RR>0 ? gate[RR].out:-RR<=pos;
gate[i].out = !(x && y);
}
return gate[m].out;
}

int main() {
ios::sync_with_stdio(false);
cin.tie(0);

int cas , mid;
cin >> cas;
while (cas--) {
cin >> n >> m;
for (int i = 1; i <= m; i++)
cin >> gate[i].in1 >> gate[i].in2;
int all0 = getOutput(0);
int all1 = getOutput(n);
if (all0 == all1) {
for (int i = 1; i <= n; i++) cout << '0';
} else {
int lef = 1, rit = n;
while (lef < rit) {
mid = lef + rit >> 1;
if (getOutput(mid) == all1) rit = mid;
else lef = mid + 1;
}
for (int i = 1; i < rit; i++) cout << '1';
cout << 'x';
for (int i = rit + 1; i <= n; i++) cout << '0';
}
cout << '\n';
}
return 0;
}
69 changes: 69 additions & 0 deletions volume016/1609 - Foul Play.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

const int maxn = 1025;
char team[maxn][maxn];
int n;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
//freopen("input.txt" , "r", stdin );
//freopen("output.txt", "w", stdout);
while(cin >> n){
for(int i = 1; i <= n; ++i) {
cin >> team[i]+1;
}
vector<int> win, lose;
for(int i = 2; i <= n; ++i) {
if(team[1][i] == '1')
win.push_back(i);
else lose.push_back(i);
}
int leftTeam = n;
while (leftTeam > 1) {
vector<int> winSub, loseSub, round3;
for(int i = 0; i < lose.size(); ++i) {
bool matched = false;
for(int j = 0; j < win.size(); ++j){
if(win[j] && team[win[j]][lose[i]] == '1'){
cout << win[j] << ' ' <<lose[i] << '\n';
matched = true;
winSub.push_back(win[j]);
win[j] = 0;
break;
}
}
if(!matched) round3.push_back(lose[i]);
}

bool isOK = false;
for(int i = 0; i < win.size(); ++i) {
if(win[i]) {
if(!isOK){
cout << "1 " << win[i] << '\n';
isOK = true;
} else {
round3.push_back(win[i]);
}
}
}

for(int i = 0; i < round3.size(); i += 2) {
cout << round3[i] << ' ' << round3[i+1] << '\n';
int tmp = round3[i+1];
if(team[round3[i]][tmp] == '1')
tmp = round3[i];
if(team[1][tmp] == '1') winSub.push_back(tmp);
else loseSub.push_back(tmp);
}

win = winSub;
lose = loseSub;
leftTeam >>= 1;
}
}
return 0;
}

Loading