Upload Bab 10-13 & Kompetitif Dasar Bab 1-8

master
Zydhan Linnar Putra 2020-04-15 12:28:14 +07:00
parent 19758af46f
commit 35c04ff58e
58 changed files with 2394 additions and 0 deletions

View File

@ -0,0 +1,61 @@
#include <iostream>
using namespace std;
bool cekprima(int a) {
int i=2;
bool prima=1;
if(a==1) {
prima = 0;
} else if(a==2) {
prima = 1;
} else if((a>2) && ((a%2)==0)) {
prima=0;
} else {
while((i*i)<=a) {
if((a%i)==0) {
prima = 0;
break;
}
i++;
}
}
return prima;
}
int main() {
int n,i,pangkat,faktorke=1,m;
cin>>n;
i=2;
while(i<=n) {
pangkat=0;
if(cekprima(i)) {
while((n%i)==0){
pangkat++;
n/=i;
}
}
if(faktorke==1) {
if(pangkat==1)
cout<<i;
else if(pangkat>1)
cout<<i<<"^"<<pangkat;
else faktorke--;
} else {
if(pangkat==1)
cout<<" x "<<i;
else if(pangkat>1)
cout<<" x "<<i<<"^"<<pangkat;
else faktorke--;
}
i++;
faktorke++;
}
cout<<endl;
return 0;
}

View File

@ -0,0 +1,34 @@
#include <iostream>
using namespace std;
int abs(int mutlak){
if(mutlak>=0) {
return mutlak;
} else {
mutlak= mutlak * (-1);
return mutlak;
}
}
int fungsi(int a, int b, int k, int x) {
int hasil;
if(k>1) {
k--;
hasil=abs((a*x)+b);
fungsi(a,b,k,hasil);
} else if(k==1) {
hasil=abs((a*x)+b);
return hasil;
}
}
int main() {
int a,b,k,x;
cin>>a>>b>>k>>x;
cout<<fungsi(a,b,k,x)<<endl;
return 0;
}

View File

@ -0,0 +1,28 @@
#include <iostream>
using namespace std;
unsigned int reverse(unsigned int x) {
unsigned int temp = x;
unsigned int ret = 0;
while (temp > 0) {
ret = (ret * 10) + (temp % 10);
temp = temp / 10;
}
return ret;
}
int main() {
unsigned int a,b,c;
cin>>a>>b;
c= reverse(a) + reverse(b);
c= reverse(c);
cout<<c<<endl;
return 0;
}

View File

@ -0,0 +1,50 @@
#include <iostream>
using namespace std;
int abs(int a) {
if (a>=0)
return a;
else return (a*(-1));
}
int pow(int a, int b) {
int n=a;
while (b>1) {
n*=a;
b--;
}
return n;
}
int kedekatan(int xj, int xi, int yj, int yi, int d) {
return (pow(abs(xj-xi), d)+pow(abs(yj-yi), d));
}
int main() {
int n,d,i,j,hasil,terbesar=0,terkecil=0;
int param[1001][3];
cin>>n>>d;
for(i=0; i<n;i++) {
for(j=0; j<2; j++) {
cin>>param[i][j];
}
}
for(i=0; i<n;i++) {
for(j=i+1; j<n; j++) {
hasil=kedekatan(param[j][0],param[i][0],param[j][1],param[i][1], d);
if((terkecil==0) && (terbesar==0)){
terbesar=hasil;
terkecil=hasil;
}
if(hasil>terbesar)
terbesar=hasil;
if(hasil<terkecil)
terkecil=hasil;
}
}
cout<<terkecil<<" "<<terbesar<<endl;
return 0;
}

View File

@ -0,0 +1,44 @@
#include <cstdio>
int N;
int Q;
int ar[2][1001];
void swap(int &a, int &b) {
int temp;
temp=a;
a=b;
b=temp;
}
int main() {
scanf("%d", &N);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < N; j++) {
scanf("%d", &ar[i][j]);
}
}
scanf("%d", &Q);
for (int i = 0; i < Q; i++) {
char buff1[5], buff2[5];
int x, y;
scanf("%s %d %s %d", buff1, &x, buff2, &y);
int p = buff1[0] - 'A';
int q = buff2[0] - 'A';
x--;
y--;
swap(ar[p][x], ar[q][y]);
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < N; j++) {
printf("%d", ar[i][j]);
if (j+1 < N) {
printf(" ");
}
}
printf("\n");
}
}

View File

@ -0,0 +1,20 @@
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char s[101];
int i;
cin>>s;
for(i=0; i<strlen(s); i++) {
if((s[i]>='A') && (s[i]<'a'))
s[i]= s[i]-'A'+'a';
else s[i]= s[i]-'a'+'A';
}
cout<<s<<endl;
return 0;
}

View File

@ -0,0 +1,16 @@
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
int k;
ios_base::sync_with_stdio(false);
cin.tie();
cin>>s;
cin>>k;
for(int i=0; i<s.length(); i++)
s[i] = ((s[i] - 'a' + k) % 26) + 'a';
cout<<s<<'\n';
return 0;
}

View File

@ -0,0 +1,56 @@
#include <iostream>
#include <string>
using namespace std;
void camel(string *mem) {
string s=*mem;
int i;
for(i=0; i<s.length(); i++) {
if((s[i]>='A') && (s[i]<='Z')) {
s[i]=s[i]-'A'+'a';
s.insert(i,"_");
}
}
*mem=s;
}
void snake(string *mem) {
int ada=0;
int dimana[255];
string s=*mem;
string und="_";
int panjang=s.length();
int temudi=s.find(und);
while((temudi>=0) && (temudi<panjang)) {
dimana[ada]=temudi;
ada++;
s.erase(temudi,1);
temudi=s.find(und);
panjang=s.length();
}
for(int i=0; i<ada;i++) {
if(!(dimana[i]>=s.length())) {
s[dimana[i]]-=32;
}
}
*mem=s;
}
int main() {
string s;
string * mem;
mem=&s;
cin>>s;
if((s.find("_")>=0) && ((s.find("_")<s.length())))
snake(mem);
else camel(mem);
cout<<s<<endl;
return 0;
}

View File

@ -0,0 +1,16 @@
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1,s2,s3,s4;
cin>>s1>>s2>>s3>>s4;
s1.erase(s1.find(s2), s2.length());
s1.insert((s1.find(s3)+s3.length()), s4);
cout<<s1<<endl;
return 0;
}

View File

@ -0,0 +1,15 @@
#include <iostream>
#include <string>
using namespace std;
int main() {
string a,b;
cin>>a>>b;
while((a.find(b)<a.length()) && (a.find(b)>=0)) {
a.erase(a.find(b), b.length());
}
cout<<a<<endl;
return 0;
}

View File

@ -0,0 +1,22 @@
#include <iostream>
using namespace std;
int faktorial(int x) {
if (x == 1) {
return 1;
} else if (x % 2 == 0) {
return x/2 * faktorial(x-1);
} else {
return x * faktorial(x-1);
}
}
int main() {
int n;
cin>>n;
cout<<faktorial(n)<<endl;
return 0;
}

View File

@ -0,0 +1,33 @@
#include <iostream>
#include <string>
using namespace std;
int length;
bool palindrom(string s) {
if(length==1) {
return 1;
}
if(length==2) {
if(s[0]==s[1])
return 1;
else return 0;
}
bool masih=(s[0]==s[length-1]);
s.erase(length-1,1);
s.erase(0,1);
length=s.length();
return masih && palindrom(s);
}
int main() {
string s;
cin>>s;
length= s.length();
if(palindrom(s))
cout<<"YA"<<endl;
else cout<<"BUKAN"<<endl;
return 0;
}

View File

@ -0,0 +1,25 @@
#include <iostream>
using namespace std;
int a,b,x;
int abs(int a) {
if (a>=0)
return a;
else return (a*(-1));
}
int fungsi(int k){
if(k==1)
return abs((a*x+b));
else return abs((a*fungsi(k-1)+b));
}
int main() {
int k;
cin>>a>>b>>k>>x;
cout<<fungsi(k)<<endl;
return 0;
}

View File

@ -0,0 +1,20 @@
#include <iostream>
using namespace std;
string biner(int N) {
if (N==1)
return "1";
else if (N % 2 == 1)
return biner(N/2) + "1";
else
return biner(N/2) + "0";
}
int main() {
int N;
cin>>N;
cout<<biner(N)<<endl;
return 0;
}

View File

@ -0,0 +1,39 @@
#include <bits/stdc++.h>
using namespace std;
int N, K;
int catat[1000];
bool pernah[1000] = {0};
void tulis(int kedalaman) {
if(kedalaman >= K) {
bool menaik = true;
for(int i=0; i<K-1; i++) {
if(catat[i] > catat[i+1]) menaik = false;
}
if(menaik) {
for(int i=0; i<K; i++) {
cout<<catat[i];
if(i < K-1) cout<<" ";
}
cout<<'\n';
}
} else {
for(int i=1; i<=N; i++) {
if(!pernah[i]) {
pernah[i] = true;
catat[kedalaman] = i;
tulis(kedalaman+1);
pernah[i] = false;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie();
cin>>N>>K;
tulis(0);
return 0;
}

View File

@ -0,0 +1,34 @@
#include <bits/stdc++.h>
using namespace std;
short movex[] = {0, 0, 1, -1};
short movey[] = {1, -1, 0, 0};
int nilai;
short bola[25][25];
short m, n;
void count(short x, short y, short ball) {
if((bola[x][y] != ball) || (y < 0) || (x < 0) || (y >= 25) || (x >= 25)) return;
else {
nilai++;
bola[x][y] = -1;
for(int i=0; i < (sizeof(movex) / sizeof(short)); i++) {
count(x + movex[i], y + movey[i], ball);
}
}
}
int main() {
nilai = 0;
cin>>m>>n;
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++)
cin>>bola[i][j];
}
short b, k;
cin>>b>>k;
count(b, k, bola[b][k]);
cout<<((nilai - 1) * nilai)<<endl;
return 0;
}

View File

@ -0,0 +1,25 @@
#include <iostream>
using namespace std;
void gambar(int n) {
if(n==1)
cout<<"*"<<endl;
else {
gambar(n - 1);
for(int i=0; i<n; i++) {
cout<<"*";
}
cout<<endl;
gambar(n - 1);
}
}
int main() {
int n;
cin>>n;
gambar(n);
return 0;
}

View File

@ -0,0 +1,37 @@
#include <bits/stdc++.h>
using namespace std;
int N;
int catat[1000];
bool pernah[1000] = {0};
void tulis(int kedalaman) {
if(kedalaman >= N) {
bool zigzag = true;
for(int i=1; i<N-1; i++) {
if(!((catat[i-1] < catat[i] && catat[i+1] < catat[i])
|| (catat[i-1] > catat[i] && catat[i+1] > catat[i]))) zigzag = false;
}
if(zigzag) {
for(int i=0; i<N; i++) cout<<catat[i];
cout<<'\n';
}
} else {
for(int i=1; i<=N; i++) {
if(!pernah[i]) {
pernah[i] = true;
catat[kedalaman] = i;
tulis(kedalaman+1);
pernah[i] = false;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie();
cin>>N;
tulis(0);
return 0;
}

View File

@ -0,0 +1,64 @@
#include <bits/stdc++.h>
using namespace std;
int matrix[128][128];
int kodeindex;
string kode[16384];
bool homogen(int r, int c, int size) {
for(int i = r; i < (r + size); i++) {
for(int j = c; j < (c + size); j++) {
if(matrix[r][c] != matrix[i][j])
return false;
}
}
return true;
}
bool ispowertwo(int n) {
if(n == 2 || n == 4 || n == 8 || n == 16 || n == 32 || n == 64 || n == 128) return true;
else return false;
}
void quadtree(int r, int c, int size, string kodenow) {
if(homogen(r, c, size)) {
if(matrix[r][c]) {
kode[kodeindex] = "1" + kodenow;
kodeindex++;
}
} else {
quadtree(r, c, (size >> 1), kodenow + "0");
quadtree(r, c + (size >> 1), (size >> 1), kodenow + "1");
quadtree(r + (size >> 1), c, (size >> 1), kodenow + "2");
quadtree(r + (size >> 1), c + (size >> 1), (size >> 1), kodenow + "3");
}
}
int main() {
kodeindex = 0;
int r, c;
cin>>r>>c;
for(int i=0; i<r; i++) {
for(int j=0; j<c; j++) {
cin>>matrix[i][j];
}
}
int rtemp = r, ctemp = c;
if(!(ispowertwo(r) && ispowertwo(c))) {
while(!ispowertwo(rtemp)) rtemp++;
while(!ispowertwo(ctemp)) ctemp++;
}
for(int i=r; i<rtemp; i++) {
for(int j=c; j<ctemp; j++) {
matrix[i][j] = 0;
}
}
r = ((rtemp > ctemp) ? rtemp : ctemp);
c = r;
string s;
quadtree(0, 0, r, s);
cout<<kodeindex<<'\n';
for(int i = 0; i < kodeindex; i++)
cout<<kode[i]<<'\n';
return 0;
}

View File

@ -0,0 +1,83 @@
#include <bits/stdc++.h>
using namespace std;
int matrix[128][128];
int r, c, n;
string code[128*128];
bool ispowertwo(int n) {
if(n == 2 || n == 4 || n == 8 || n == 16 || n == 32 || n == 64 || n == 128) return true;
else return false;
}
void changeto1(int r, int c, int size, string kode) {
if(kode.length() == 0) {
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++)
matrix[i][j] = 1;
}
return;
}
if(kode.length() == 1) {
if(kode == "0") {
for(int i = r; i < (r + (size >> 1)); i++) {
for(int j = c; j < (c + (size >> 1)); j++)
matrix[i][j] = 1;
}
} else if(kode == "1") {
for(int i = r; i < (r + (size >> 1)); i++) {
for(int j = c + (size >> 1); j < (c + size); j++)
matrix[i][j] = 1;
}
} else if(kode == "2") {
for(int i = r + (size >> 1); i < (r + size); i++) {
for(int j = c; j < (c + (size >> 1)); j++)
matrix[i][j] = 1;
}
} else {
for(int i = r + (size >> 1); i < (r + size); i++) {
for(int j = c + (size >> 1); j < (c + size); j++)
matrix[i][j] = 1;
}
}
} else {
char pos = kode[0];
kode.erase(kode.begin());
if(pos == '0') changeto1(r, c, (size >> 1), kode);
else if(pos == '1') changeto1(r, c + (size >> 1), (size >> 1), kode);
else if(pos == '2') changeto1(r + (size >> 1), c, (size >> 1), kode);
else changeto1(r + (size >> 1), c + (size >> 1), (size >> 1), kode);
}
}
int main() {
cin>>n;
for(int i = 0; i < n; i++)
cin>>code[i];
cin>>r>>c;
int rtemp = r, ctemp = c;
if(!(ispowertwo(r) && ispowertwo(c))) {
while(!ispowertwo(rtemp)) rtemp++;
while(!ispowertwo(ctemp)) ctemp++;
}
rtemp = (ctemp > rtemp) ? ctemp : rtemp;
ctemp = rtemp;
for(int i = 0; i < rtemp; i++) {
for(int j = 0; j < ctemp; j++)
matrix[i][j] = 0;
}
if(n != 0) {
for(int i = 0; i < n; i++) {
code[i].erase(code[i].begin());
changeto1(0, 0, rtemp, code[i]);
}
}
for(int i = 0; i < r; i++) {
for(int j = 0; j < c; j++) {
cout<<matrix[i][j];
if(j == c - 1) cout<<'\n';
else cout<<" ";
}
}
return 0;
}

View File

@ -0,0 +1,19 @@
#include <bits/stdc++.h>
using namespace std;
int main() {
string a, b;
cin>>a;
cin>>b;
for(int i=0; i < a.length(); i++) {
string tmp = a;
tmp.erase(i, 1);
if(tmp == b) {
cout<<"Tentu saja bisa!\n";
return 0;
}
}
cout<<"Wah, tidak bisa :(\n";
return 0;
}

View File

@ -0,0 +1,59 @@
#include <bits/stdc++.h>
using namespace std;
string papan[20];
string satu, nol;
int r, c;
bool masihruntuh() {
for(int i=0; i<r; i++) {
if(satu == papan[i]) return true;
}
return false;
}
void runtuhkan() {
short lastsatu = 0;
for(int i=0; i<r; i++) {
if(papan[i] == satu) {
papan[i] = nol;
lastsatu = i;
}
}
for(int i=0; i<c; i++) {
short berapasatu = 0, lowestsatu = r;
for(int j=0; j<lastsatu; j++) {
if(papan[j][i] == '1') {
berapasatu++;
papan[j][i] = '0';
}
}
for(int j=(lastsatu+1); j < r; j++) {
if(papan[j][i] == '1') {
lowestsatu = j;
break;
}
}
for(int j=(lowestsatu-1); ((j>=0) && (berapasatu > 0)); j--) {
berapasatu--;
papan[j][i] = '1';
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>r>>c;
for(int i=0; i<r; i++)
cin>>papan[i];
for(int i=0; i<c; i++) {
satu += "1";
nol += "0";
}
while(masihruntuh())
runtuhkan();
for(int i=0; i<r; i++)
cout<<papan[i]<<endl;
return 0;
}

View File

@ -0,0 +1,56 @@
#include <bits/stdc++.h>
#define us unsigned short
using namespace std;
typedef struct a {
string id;
us a, b, c;
}peserta;
void swap(peserta &a, peserta &b) {
peserta tmp = a;
a = b;
b = tmp;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie();
us t;
cin>>t;
for(us i=0; i<t; i++) {
us n, m;
cin>>n>>m;
string search;
cin>>search;
peserta * olim = new peserta[n];
for(us j=0; j<n; j++) {
cin>>olim[j].id>>olim[j].a>>olim[j].b>>olim[j].c;
}
for(us j=0; j<m; j++) {
for(us k=0; k<n; k++) {
if(j==k) continue;
if(olim[k].c > olim[j].c) {
swap(olim[k], olim[j]);
} else if(olim[k].c == olim[j].c) {
if(olim[k].b > olim[j].b) {
swap(olim[k], olim[j]);
} else if(olim[k].b == olim[j].b) {
swap(olim[k], olim[j]);
}
}
}
}
bool found = false;
for(us j=0; j<m; j++) {
if(search==olim[j].id) {
found = true;
break;
}
}
if(found) cout<<"YA"<<'\n';
else cout<<"TIDAK"<<'\n';
}
return 0;
}

View File

@ -0,0 +1,45 @@
#include <bits/stdc++.h>
using namespace std;
int main() {
string a;
int n;
cin>>a;
cin>>n;
if(a == "*") {
while (n--) {
string tmp;
cin>>tmp;
cout<<tmp<<endl;
}
} else if(a[0] == '*') {
a.erase(0, 1);
while(n--) {
string tmp;
cin>>tmp;
if(tmp.find(a, (tmp.length() - a.length())) != string::npos) cout<<tmp<<endl;
}
} else if(a[a.length()-1] == '*') {
a.erase(a.length()-1, 1);
while(n--) {
string tmp;
cin>>tmp;
if(tmp.find(a) == 0) cout<<tmp<<endl;
}
} else {
string awal = a, akhir = a;
int star = a.find("*");
awal.erase(star);
akhir.erase(0, star + 1);
while(n--) {
string tmp;
cin>>tmp;
if(tmp.length() >= (awal.length() + akhir.length())) {
if((tmp.find(awal) == 0) && (tmp.find(akhir, (tmp.length() - akhir.length())) != string::npos))
cout<<tmp<<endl;
}
}
}
return 0;
}

View File

@ -0,0 +1,32 @@
#include <bits/stdc++.h>
using namespace std;
bool isprime(int n) {
for(int i = 2; i * i <= n; i++) {
if(n % i == 0) return false;
}
return true;
}
int main() {
int n, nasli, i = 2;
cin>>n;
nasli = n;
while(n > 1) {
while(!isprime(i)) {
i++;
}
if((n != nasli) && (n % i == 0)) cout<<" x ";
int div = 0;
while(n % i == 0) {
div++;
n /= i;
}
if(div == 1) cout<<i;
else if(div > 1) cout<<i<<'^'<<div;
i++;
}
cout<<endl;
return 0;
}

View File

@ -0,0 +1,31 @@
#include <bits/stdc++.h>
using namespace std;
int fpb(int a, int b) {
if(a % b == 0) return b;
else return fpb(b, a % b);
}
int main() {
int n, gcd, a, b, kpk;
cin>>n;
for(int i=0; i<n; i++) {
if(i==0) {
cin>>a;
continue;
} else {
cin>>b;
}
if(i==1) {
if(a > b) gcd = fpb(a, b);
else gcd = fpb(b, a);
kpk = a * b / gcd;
} else if(i > 1) {
if(kpk > b) gcd = fpb(kpk, b);
else gcd = fpb(b, kpk);
kpk = kpk * b / gcd;
}
}
cout<<kpk<<endl;
return 0;
}

View File

@ -0,0 +1,20 @@
#include <bits/stdc++.h>
#define ull unsigned long long
using namespace std;
ull fpb(ull a, ull b) {
if(a % b == 0) return b;
else return fpb(b, a % b);
}
int main() {
ull a, b, c, d, gcd, e, f;
cin>>a>>b;
cin>>c>>d;
e =(a * d) + (b * c);
f = b * d;
if(e > f) gcd = fpb(e, f);
else gcd = fpb(f, e);
cout<<(e / gcd)<<" "<<(f / gcd)<<endl;
return 0;
}

View File

@ -0,0 +1,44 @@
#include <bits/stdc++.h>
using namespace std;
int prime[77777];
bool prima[990000];
bool isprime(int n) {
for(int i=2; i*i<=n; i++) {
if((n % 2) == 0) return false;
}
return true;
}
void sieve() {
int in = 0;
memset(prima, true, 990000);
for(int i = 2; i <= 989999; i++) {
if (prima[i]) {
if(isprime(i)) {
prime[in] = i;
in++;
int j = 2;
while ((j * i) <= 989999){
prima[j * i] = false;
j++;
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
sieve();
int t;
cin>>t;
while (t--) {
int n;
cin>>n;
cout<<prime[n-1]<<'\n';
}
return 0;
}

View File

@ -0,0 +1,28 @@
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
unsigned short quality[100000];
unsigned int n;
float median();
int main() {
cin>>n;
for(unsigned int i=0; i<n; i++) {
cin>>quality[i];
}
sort(quality, quality+n);
cout<<fixed<<setprecision(1)<<median()<<endl;
return 0;
}
float median() {
if(n%2==1) {
return (float)quality[n/2];
} else {
return ((float)(quality[n/2]+quality[(n-1)/2])/2);
}
}

View File

@ -0,0 +1,27 @@
#include <iostream>
#include <cmath>
#include <limits.h>
using namespace std;
int main() {
int n,x;
int diff[1000];
cin>>n>>x;
int min = INT_MAX;
int minDiff = INT_MAX;
int coupon[1000];
for (int i = 0; i<n; i++) {
cin>>coupon[i];
diff[i] = abs(x-coupon[i]);
if (diff[i]<minDiff) {
minDiff = diff[i];
min = INT_MAX;
}
if(diff[i]==minDiff && coupon[i]<min) {
min = coupon[i];
}
}
cout<<min<<endl;
return 0;
}

View File

@ -0,0 +1,71 @@
#include <iostream>
#include <iomanip>
struct beras {
unsigned short w;
unsigned short c;
double CostPerWeight;
};
typedef beras Beras;
using std::cout;
using std::cin;
using std::endl;
Beras B[1000];
Beras BSorted[1000];
void insertionSort(unsigned short);
int main() {
unsigned short n;
unsigned int x;
cin>>n>>x;
for(unsigned short i=0; i<n;i++) {
cin>>B[i].w;
}
for(unsigned short i=0; i<n;i++) {
cin>>B[i].c;
B[i].CostPerWeight=((double)B[i].c)/B[i].w;
insertionSort(i);
}
unsigned short i=0;
while(x>0 && i<n) {
if(i!=0) {
if(BSorted[i].w < x) {
BSorted[i].CostPerWeight=(BSorted[i].CostPerWeight*BSorted[i].w)+BSorted[i-1].CostPerWeight;
x-=BSorted[i].w;
} else {
BSorted[i].CostPerWeight=(BSorted[i].CostPerWeight*x)+BSorted[i-1].CostPerWeight;
break;
}
} else {
if(BSorted[i].w < x) {
BSorted[i].CostPerWeight=BSorted[i].CostPerWeight*BSorted[i].w;
x-=BSorted[i].w;
} else {
BSorted[i].CostPerWeight=BSorted[i].CostPerWeight*x;
break;
}
}
i++;
}
if(i==n) i--;
cout<<std::setprecision(5)<<std::fixed<<BSorted[i].CostPerWeight<<endl;
return 0;
}
void insertionSort(unsigned short n) {
unsigned short i = n;
BSorted[n]=B[n];
if(i!=0) {
while(i>0 && BSorted[i-1].CostPerWeight<BSorted[i].CostPerWeight) {
Beras tmp = BSorted[i-1];
BSorted[i-1]=BSorted[i];
BSorted[i]=tmp;
i--;
}
}
}

View File

@ -0,0 +1,42 @@
#include <iostream>
using namespace std;
unsigned n, weight[100000];
int binarysearchrange(int);
int main() {
unsigned q, x, y;
cin>>n;
for(unsigned i = 0; i<n; i++) {
cin>>weight[i];
}
cin>>q;
for(unsigned i=0; i<q; i++) {
cin>>x>>y;
cout<<binarysearchrange(y)-binarysearchrange(x)<<endl;
}
return 0;
}
int binarysearchrange(int a) {
unsigned first=0;
int last=n-1, index=-1;
while(first<=last && index==-1) {
int mid=(last+first)/2;
if(weight[mid]<=a) {
first=mid+1;
if(first>n-1) return first;
if(weight[first]>a) {
index=first;
}
} else if(weight[mid]>a) {
last=mid-1;
if(last<0) return 0;
if(weight[last]<a) {
index=last+1;
}
}
}
return index;
}

View File

@ -0,0 +1,46 @@
#include <iostream>
#include <cstring>
using namespace std;
unsigned short n;
char nama[500][11];
void insertandsort(const char*, unsigned short);
void swap(char*, char*);
int main() {
cin>>n;
for(unsigned short i = 0; i<n; i++) {
char tmp[11];
cin>>tmp;
insertandsort(tmp, i);
}
for(unsigned short i = 0; i<n; i++) {
cout<<nama[i]<<endl;
}
}
void insertandsort(const char* tmp, unsigned short n) {
unsigned short i = n;
strcpy(nama[i], tmp);
if(i!=0) {
while (strlen(nama[i-1])>strlen(nama[i]) && i>0) {
swap(nama[i-1], nama[i]);
i--;
}
while (strcmp(nama[i-1], nama[i])>0 && i>0) {
if(strlen(nama[i-1])==strlen(nama[i])) {
swap(nama[i-1], nama[i]);
i--;
} else break;
}
}
}
void swap(char * str1, char* str2) {
char tmp[11];
strcpy(tmp, str1);
strcpy(str1, str2);
strcpy(str2, tmp);
}

View File

@ -0,0 +1,38 @@
#include <iostream>
#include <cstring>
using namespace std;
char Name[1000][11];
unsigned short n;
unsigned short getPos(char *, unsigned short);
void swap(char*, char*);
int main() {
cin>>n;
for(unsigned short i = 0; i<n; i++) {
char temp[11];
cin>>temp;
cout<<getPos(temp, i)+1<<endl;
}
return 0;
}
unsigned short getPos(char * nama, unsigned short n) {
unsigned short i = n;
strcpy(Name[i], nama);
if(i!=0) {
while (strcmp(Name[i-1], Name[i])>0 && i>0) {
swap(Name[i-1], Name[i]);
i--;
}
}
return i;
}
void swap(char * str1, char* str2) {
char tmp[11];
strcpy(tmp, str1);
strcpy(str1, str2);
strcpy(str2, tmp);
}

View File

@ -0,0 +1,61 @@
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
unsigned short petak[100][100];
int main() {
unsigned short n,m;
unsigned int k;
cin>>n>>m>>k;
for(unsigned short i=0; i<n; i++) {
for(unsigned short j=0; j<m; j++){
cin>>petak[i][j];
}
}
bool menarik=false;
unsigned short i, j;
for(j=0; j<m; j++) {
for(i=0; i<n; i++){
unsigned int kemenarikan;
if(m==1 && n==1) {
kemenarikan=0;
} else if(m==1) {
kemenarikan=((i==0) ? 0:petak[i-1][0]) * ((i==n-1) ? 0:petak[i+1][0]);
} else if(n==1) {
kemenarikan=((j==0) ? 0:petak[0][j-1]) * ((j==m-1) ? 0:petak[0][j+1]);
} else if(i==0 && j==0) {
kemenarikan=petak[1][0]*petak[0][1];
} else if(i==n-1 && j==0) {
kemenarikan=petak[i-1][0]*petak[i][1];
} else if(i==0 && j==m-1) {
kemenarikan=petak[0][j-1]*petak[1][j];
} else if(i==n-1 && j==m-1) {
kemenarikan=petak[i][j-1]*petak[i-1][j];
} else if(i==0) {
kemenarikan=petak[i][j-1]*petak[i][j+1]*petak[i+1][j];
} else if(i==n-1) {
kemenarikan=petak[i][j-1]*petak[i][j+1]*petak[i-1][j];
} else if(j==0) {
kemenarikan=petak[i-1][j]*petak[i+1][j]*petak[i][j+1];
} else if(j==m-1) {
kemenarikan=petak[i-1][j]*petak[i+1][j]*petak[i][j-1];
} else {
kemenarikan=petak[i][j-1]*petak[i][j+1]*petak[i-1][j]*petak[i+1][j];
}
if(kemenarikan==k) {
menarik=true;
break;
}
}
if(menarik) break;
}
if(menarik) {
cout<<i+1<<" "<<j+1<<endl;
} else {
cout<<"0 0"<<endl;
}
return 0;
}

View File

@ -0,0 +1,27 @@
#include <iostream>
using namespace std;
int main() {
short n,k;
bool height[100001] = {0};
cin>>n>>k;
for(int i=1; i<=n; i++) {
int arrayIndex;
cin>>arrayIndex;
height[arrayIndex] = 1;
}
short count = 0;
int arrayIndex;
for(int i=1; i<=100000; i++) {
if(height[i] == 1) {
count++;
} else continue;
if(count==k) {
arrayIndex = i;
break;
}
}
cout<<arrayIndex<<endl;
return 0;
}

View File

@ -0,0 +1,46 @@
#include <iostream>
#include <cstring>
using namespace std;
struct phonebook {
char name[11];
char num[7];
};
typedef struct phonebook Phonebook;
Phonebook PB[100001];
unsigned int n;
void searchPhonebook(char *);
int main() {
unsigned int q;
cin>>n>>q;
for(unsigned int i=1; i<=n; i++) {
cin>>PB[i].name>>PB[i].num;
}
for(unsigned int i=1; i<=q; i++) {
char search[11];
cin>>search;
searchPhonebook(search);
}
return 0;
}
void searchPhonebook(char search[]) {
unsigned int kiri = 1, kanan = n;
char num[7] = "NIHIL";
while(kiri<=kanan && (strcmp(num, "NIHIL") == 0)) {
unsigned int tengah = (kanan+kiri)/2;
if(strcmp(search, PB[tengah].name) > 0) {
kiri = tengah+1;
} else if (strcmp(search, PB[tengah].name) < 0) {
kanan = tengah-1;
} else {
strcpy(num, PB[tengah].num);
}
}
cout<<num<<endl;
}

View File

@ -0,0 +1,43 @@
#include <bits/stdc++.h>
#define nextteam valid(a, b + 1)
#define us unsigned short
using namespace std;
us n, score[5] = {0}, scoredata[5] = {0};
bool valid(us a, us b) {
bool mungkin = false;
if(a == (n - 1)) {
mungkin = true;
for(us i=0; i<n; i++) {
if(score[i] != scoredata[i]) return false;
}
} else if(b == n) return (mungkin || valid(a + 1, a + 2));
else {
scoredata[a] += 3;
mungkin |= nextteam;
scoredata[a] -= 3;
scoredata[a]++;
scoredata[b]++;
mungkin |= nextteam;
scoredata[a]--;
scoredata[b]--;
scoredata[b] += 3;
mungkin |= nextteam;
scoredata[b] -= 3;
}
return mungkin;
}
int main() {
us t;
cin>>t;
while(t--) {
cin>>n;
for(us i=0; i<n; i++) cin>>score[i];
if(valid(0,1)) cout<<"YES\n";
else cout<<"NO\n";
}
return 0;
}

View File

@ -0,0 +1,41 @@
#include <bits/stdc++.h>
using namespace std;
short movex[] = {0, 0, 1, -1};
short movey[] = {1, -1, 0, 0};
int nilai, maks;
bool visited[25][25] = {0};
short bola[25][25];
short m, n;
void count(short x, short y, short ball) {
if((bola[x][y] != ball) || visited[x][y] || (y < 0) || (x < 0) || (y >= 25) || (x >= 25)) return;
else {
nilai++;
visited[x][y] = true;
for(int i=0; i < (sizeof(movex) / sizeof(short)); i++) {
count(x + movex[i], y + movey[i], ball);
}
}
}
int main() {
maks = -1;
cin>>m>>n;
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++)
cin>>bola[i][j];
}
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
if(!visited[i][j]) {
nilai = 0;
count(i, j, bola[i][j]);
if (nilai > maks) maks = nilai;
}
}
}
cout<<((maks - 1) * maks)<<endl;
return 0;
}

View File

@ -0,0 +1,77 @@
#include <bits/stdc++.h>
#define sizebool(x) (sizeof(x)/sizeof(bool))
#define sizeshort(x) (sizeof(x)/sizeof(short))
#define set0(x, z) memset(x, 0, z)
using namespace std;
short movex[] = {0, 0, 1, -1}, movey[] = {1, -1, 0, 0}, m, n;
int nilai, maks = -1;
void count(short x, short y, short ball, short bola[25][25], bool visited[25][25], bool runtuhkan) {
if((bola[x][y] != ball) || ((runtuhkan) ? false : visited[x][y]) || (y < 0) || (x < 0) || (y >= 25) || (x >= 25)) return;
else {
nilai++;
if(runtuhkan) bola[x][y] = -1;
else visited[x][y] = true;
for(int i=0; i < sizeshort(movex); i++) {
count(x + movex[i], y + movey[i], ball, bola, visited, runtuhkan);
}
}
}
void runtuh(short bola[25][25]) {
for(int i=0; i<n; i++) {
bool ada = true;
while(ada) {
ada = false;
for(int j=0; j<(m - 1); j++) {
if(bola[j][i] != -1 && bola[j+1][i] == -1) {
ada = true;
bola[j][i] ^= bola[j+1][i];
bola[j+1][i] ^= bola[j][i];
bola[j][i] ^= bola[j+1][i];
}
}
}
}
}
void printmatrix(short bola[25][25]) {
for(int a=0; a<m; a++) {
for(int b=0; b<n; b++) {
if(bola[a][b] != -1) cout<<bola[a][b];
else cout<<'.';
if(b < n - 1) cout<<' ';
else cout<<'\n';
}
}
}
int main() {
short bola[25][25];
bool visited[25][25] = {0};
int x, y;
cin>>m>>n;
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++)
cin>>bola[i][j];
}
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
if(!visited[i][j]) {
nilai = 0;
count(i, j, bola[i][j], bola, visited, false);
if(nilai > maks) {
maks = nilai;
x = i;
y = j;
}
}
}
}
set0(visited, sizebool(visited));
count(x, y, bola[x][y], bola, visited, true);
runtuh(bola);
printmatrix(bola);
return 0;
}

View File

@ -0,0 +1,86 @@
#include <bits/stdc++.h>
using namespace std;
short movex[] = {0, 0, 1, -1}, movey[] = {1, -1, 0, 0}, m, n;
int nilai;
void count(short x, short y, short ball, short bola[25][25], bool visited[25][25], bool marked) {
if((bola[x][y] != ball) || (visited[x][y])|| (y < 0) || (x < 0) || (y >= n) || (x >= m)) return;
else {
nilai++;
if(marked) bola[x][y] = -1;
visited[x][y] = true;
for(int i=0; i < 4; i++) {
count(x + movex[i], y + movey[i], ball, bola, visited, marked);
}
}
}
void runtuh(short bola[25][25]) {
for(int i=0; i<n; i++) {
bool ada = true;
while(ada) {
ada = false;
for(int j=0; j<(m - 1); j++) {
if(bola[j][i] != -1 && bola[j+1][i] == -1) {
ada = true;
bola[j][i] ^= bola[j+1][i];
bola[j+1][i] ^= bola[j][i];
bola[j][i] ^= bola[j+1][i];
}
}
}
}
}
void copyball(short dest[25][25], short source[25][25]) {
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
dest[i][j] = source[i][j];
}
}
}
int step2(short bola[25][25]) {
bool visited[25][25] = {0};
short ball[25][25];
copyball(ball, bola);
int max = 0;
for(int i=0; i<m; i++) {
for(int j=0; j<m; j++) {
if(visited[i][j] || ball[i][j] == -1) continue;
nilai = 0;
count(i, j, ball[i][j], ball, visited, false);
if(nilai == 1) continue;
max = (nilai > max) ? nilai : max;
}
}
return max;
}
int main() {
bool visited[25][25] = {0};
short bola[25][25] = {0};
int maks = 0, x, y;
cin>>m>>n;
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++)
cin>>bola[i][j];
}
short ball[25][25];
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
if(visited[i][j]) continue;
nilai = 0;
copyball(ball, bola);
count(i, j, ball[i][j], ball, visited, true);
if(nilai == 1) continue;
runtuh(ball);
int tmp = nilai * (nilai - 1);
int nilai2 = step2(ball);
tmp += (nilai2 * (nilai2 - 1));
maks = (tmp > maks) ? tmp : maks;
}
}
cout<<maks<<endl;
return 0;
}

View File

@ -0,0 +1,67 @@
#include <bits/stdc++.h>
using namespace std;
typedef struct dance {
int d;
char t;
}tarian;
int ksum[3628800] = {0}, indx = 0, y, r, n;
tarian bebek[10];
bool pernah[10] = {};
void compute(int t, int k, int now, char before, bool yakin) {
if(t == r) {
ksum[indx] = k;
indx++;
return;
}
for(int i=0; i<n; i++) {
if(pernah[i]) continue;
int tmp = bebek[i].d;
if(before == 'P') tmp <<= 1;
else if(before == 'L') tmp >>= 1;
if(yakin) tmp += y;
pernah[i] = true;
compute(t + 1, k + tmp, i, bebek[i].t, (bebek[i].t == 'Y' || yakin));
pernah[i] = false;
}
}
int binsearch(int left, int right, int n) {
if(left == right || (((right - left) == 1) && (ksum[right] > n))) return left;
if(((right - left) == 1) && (ksum[right] <= n)) return right;
int mid = left + ((right - left) >> 1);
if(ksum[mid] == n) {
if(ksum[mid + 1] > n) return mid;
return binsearch(mid + 1, right, n);
}
else if(ksum[mid] > n) return binsearch(left, mid, n);
else return binsearch(mid, right, n);
}
int main() {
int j, h;
scanf("%*s %*d");
cin>>n>>r>>y>>j;
for(int i=0; i<n; i++) {
cin>>bebek[i].d>>bebek[i].t;
}
for(int i=0; i<n; i++) {
pernah[i] = true;
compute(1, bebek[i].d, i, bebek[i].t, bebek[i].t == 'Y');
pernah[i] = false;
}
sort(ksum, ksum + indx);
for(int i=0; i<j; i++) {
cin>>h;
if(h > ksum[indx - 1]) {
cout<<0<<'\n';
continue;
}
if(h < ksum[0]) {
cout<<indx<<'\n';
continue;
}
cout<<indx - binsearch(0, indx - 1, h) - 1<<'\n';
}
return 0;
}

View File

@ -0,0 +1,64 @@
#include <bits/stdc++.h>
using namespace std;
int m, n, *freq, periode;
bool *batas, ketemu = false;
void resetbatas() {
for(int i=0; i<m; i++)
batas[i] = false;
}
bool cekbatas() {
for(int i=0; i<m; i++) {
if(batas[i] && freq[i] == freq[i - 1])
return false;
}
for(int i=(m - 1); i>=0; i--) {
if(batas[i]) {
if(i == 0 || ((m - i) != periode && (m - i) != (periode + 1)))
return false;
break;
}
}
return true;
}
void setbatas(int indexbatas, int jumlahbatas) {
if(ketemu) return;
batas[indexbatas] = true;
if(jumlahbatas == (n - 1)) {
ketemu = cekbatas();
if(!ketemu) batas[indexbatas] = false;
return;
}
setbatas(indexbatas + periode, jumlahbatas + 1);
setbatas(indexbatas + periode + 1, jumlahbatas + 1);
if(!ketemu) batas[indexbatas] = false;
}
int main() {
cin>>m;
freq = new int[m];
batas = new bool[m];
resetbatas();
for(int i=0; i<m; i++)
cin>>freq[i];
cin>>n;
sort(freq, freq + m);
periode = (m / n);
if((m % n) == 0) {
for(int i=periode; i<m; i += periode) {
cout<<freq[i];
if((i + periode) < m) cout<<' ';
}
cout<<'\n';
} else {
setbatas(periode, 1);
setbatas(periode + 1, 1);
int counter = 1;
for(int i=0; i<m; i++) {
if(batas[i])
cout<<freq[i]<<((counter == (n - 1) ? '\n' : ' '));
}
}
return 0;
}

View File

@ -0,0 +1,83 @@
#include <iostream>
using namespace std;
unsigned short matrixa[75][75],matrixb[75][75], xa,ya,xb,yb;
bool identik() {
if(xa!=xb || ya!=yb) return 0;
else {
for(unsigned short i=0; i<xa; i++) {
for(unsigned short j=0; j<ya; j++) {
if(matrixa[i][j]!=matrixb[i][j]) return 0;
}
}
}
return 1;
};
bool vertikal() {
if(xa!=xb || ya!=yb) return 0;
else {
for(unsigned short i=0; i<xa; i++) {
for(unsigned short j=0; j<ya; j++) {
if(matrixa[i][j]!=matrixb[i][ya-1-j]) return 0;
}
}
}
return 1;
};
bool horizontal() {
if(xa!=xb || ya!=yb) return 0;
else {
for(unsigned short i=0; i<xa; i++) {
for(unsigned short j=0; j<ya; j++) {
if(matrixa[i][j]!=matrixb[xa-1-i][j]) return 0;
}
}
}
return 1;
};
bool kananbawah() {
if(xa!=yb || ya!=xb) return 0;
else {
for(unsigned short i=0; i<xa; i++) {
for(unsigned short j=0; j<ya; j++) {
if(matrixa[i][j]!=matrixb[j][i]) return 0;
}
}
}
return 1;
};
bool kiribawah() {
if(xa!=yb || ya!=xb) return 0;
else {
for(unsigned short i=0; i<xa; i++) {
for(unsigned short j=0; j<ya; j++) {
if(matrixa[i][j]!=matrixb[yb-1-j][xb-1-i]) return 0;
}
}
}
return 1;
};
int main() {
cin>>xa>>ya;
for(unsigned short i=0; i<xa; i++) {
for(unsigned short j=0; j<ya; j++) {
cin>>matrixa[i][j];
}
}
cin>>xb>>yb;
for(unsigned short i=0; i<xa; i++) {
for(unsigned short j=0; j<ya; j++) {
cin>>matrixb[i][j];
}
}
if(identik()) cout<<"identik"<<endl;
else if(vertikal()) cout<<"vertikal"<<endl;
else if(horizontal()) cout<<"horisontal"<<endl;
else if(kananbawah()) cout<<"diagonal kanan bawah"<<endl;
else if(kiribawah()) cout<<"diagonal kiri bawah"<<endl;
else cout<<"tidak identik"<<endl;
return 0;
}

View File

@ -0,0 +1,31 @@
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll baris[100000] = {0}, y;
ll binsearch(ll left, ll right) {
if(left == right || (((right - left) == 1) && (baris[right] > y))) return left;
if(((right - left) == 1) && (baris[right] <= y)) return right;
ll mid = left + ((right - left) >> 1);
if(baris[mid] == y) return mid;
else if(baris[mid] > y) return binsearch(left, mid);
else return binsearch(mid, right);
}
int main() {
int n, q;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
baris[0] = 1;
for(int i=1; i<=n; i++) {
cin>>baris[i];
baris[i] += baris[i - 1];
}
cin>>q;
while(q--) {
cin>>y;
cout<<binsearch(0, n - 1) + 1<<'\n';
}
return 0;
}

View File

@ -0,0 +1,31 @@
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll n;
ll bigpower(ll a, ll b) {
if(a == 0) return 0;
if(b == 0) return 1;
if(b & 1) return ((bigpower(a, b - 1) % n) * (a % n) % n);
else {
a = bigpower(a, (b >> 1)) % n;
return (a * a) % n;
}
}
ll hadiah(ll a, ll b, ll c) {
if(a == 0) return 0;
if(a == 1 || b == 0) return 1;
if(b == 1 || c == 0) return a % n;
if(c == 1) return bigpower(a, b);
return bigpower(hadiah(a, b, c - 1) % n, b);
}
int main() {
ll a, b, c;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>a>>b>>c>>n;
cout<<hadiah(a % n, b, c) + 1<<'\n';
return 0;
}

View File

@ -0,0 +1,31 @@
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000
long long bigpower(long long a, long long b) {
if(a == 0) return 0;
if(b == 0) return 1;
if(b & 1) return ((bigpower(a, b - 1) % mod) * (a % mod) % mod);
else {
a = bigpower(a, (b >> 1)) % mod;
return (a * a) % mod;
}
}
int main() {
long long a, b;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>a>>b;
long long res = bigpower(a % mod, b);
if(pow(a, b) >= mod) {
long long tmp = res;
short digit = 0;
if(tmp == 0) digit = 1;
for(;tmp > 0; digit++, tmp /= 10);
digit = 6 - digit;
while (digit--) cout<<0;
cout<<res<<'\n';
} else cout<<res<<'\n';
return 0;
}

View File

@ -0,0 +1,52 @@
#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
ull n, m, a[262144] = {0}, pos = 0, low_bound = 0, up_bound = UINT64_MAX;
ull product(ull supply) {
ull sum = 0;
for(int i=0; i<n; i++) sum += (supply / a[i]);
return sum;
}
void binsearch(ull left, ull right) {
while (left <= right) {
ull mid = left + ((right - left) >> 1), sum = product(mid);
if(sum > m) {
right = mid - 1;
} else if(sum < m) {
left = mid + 1;
} else {
if(product(mid - 1) < m) {
low_bound = mid;
break;
}
else right = mid - 1;
}
}
left = low_bound, right = UINT64_MAX;
while (left <= right) {
ull mid = left + ((right - left) >> 1), sum = product(mid);
if(sum > m) {
right = mid - 1;
} else if(sum < m) {
left = mid + 1;
} else {
if(product(mid + 1) > m) {
up_bound = mid;
return;
}
else left = mid + 1;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie();
cin>>n>>m;
for(int i=0; i<n; i++)
cin>>a[i];
binsearch(1, UINT64_MAX);
cout<<up_bound - low_bound + 1<<'\n';
return 0;
}

View File

@ -0,0 +1,22 @@
#include <bits/stdc++.h>
using namespace std;
typedef struct coklat {
unsigned long long h, b;
bool operator < (const coklat &a) const { return h < a.h; };
}cc;
int main() {
cc arr[100000];
unsigned long long d, total = 0, n;
cin>>n>>d;
for(int i=0; i < n; i++)
cin>>arr[i].h>>arr[i].b;
sort(arr, arr + n);
for(int i=0; i < n; i++) {
unsigned long long bought = (arr[i].b * arr[i].h <= d) ? arr[i].b : (d / arr[i].h);
d -= (bought * arr[i].h);
total += bought;
}
cout<<total<<'\n';
return 0;
}

View File

@ -0,0 +1,25 @@
#include <bits/stdc++.h>
using namespace std;
typedef struct friends {
int start, endtime;
bool operator < (const teman &a) const { return endtime < a.endtime; };
}teman;
int main() {
teman a[100000];
int n, start = 0, invited = 0;
cin>>n;
for(int i=0; i<n; i++) {
cin>>a[i].start>>a[i].endtime;
a[i].endtime += a[i].start - 1;
}
sort(a, a + n);
for(int i=0; i < n; i++) {
if(a[i].start > start) {
invited++;
start = a[i].endtime;
}
}
cout<<invited<<'\n';
return 0;
}

View File

@ -0,0 +1,102 @@
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef struct pelat {
ull nomor, harga;
}plat;
ull n, duit;
plat * calon_sorted, * calon;
int * terbeli;
bool compVal(plat a, plat b) {
if(a.harga == b.harga)
return a.nomor > b.nomor;
return a.harga < b.harga;
}
void beliPlat(ull start, ull end, bool isSaveToArray = false) {
for(ull i=start; i<end; i++) {
for(int j=n; j>=0; j--) {
if(calon[j].harga <= duit) {
duit -= calon[j].harga;
if(isSaveToArray) terbeli[i] = j;
cout<<j;
break;
}
}
}
cout<<'\n';
}
void getPlat() {
bool termurahnyanol = (calon_sorted[0].nomor == 0);
ull murahbukannol = ((termurahnyanol) ? calon_sorted[1].harga : calon_sorted[0].harga);
ull termurah = calon_sorted[0].harga;
bool isinyanol = (murahbukannol > duit);
if(isinyanol || n == 0) {
cout<<"1\n0\n0\n";
return;
}
ull length = ((duit - murahbukannol) / termurah) + 1;
cout<<length<<'\n';
duit -= murahbukannol;
duit -= ((length - 1) * termurah);
for(ull i=0; i<=n; i++)
calon[i].harga -= termurah;
if(length < 100) terbeli = new int[length];
duit += murahbukannol;
for(int i=n; i>=0; i--) {
ull currentprice = calon[i].harga + termurah;
if(currentprice <= duit) {
duit -= currentprice;
if(length < 100) terbeli[0] = i;
cout<<i;
break;
}
}
if(length < 100) {
beliPlat(1, ((length <= 50) ? length : 50), true);
if(length <= 50) {
for(int i=0; i<length; i++)
cout<<terbeli[i];
cout<<'\n';
} else {
for(int i=(length - 50); i<50; i++)
cout<<terbeli[i];
ull sisa = length - 50;
beliPlat(0, sisa);
}
} else if(length >= 100) {
beliPlat(1, 50);
ull takterprint = (length - 100), terbelinonprint = 0;
for(int i=n; i>=0 && duit > 0 && (terbelinonprint < takterprint); i--) {
if(calon[i].harga == 0) break;
ull buy = (duit/calon[i].harga);
if((terbelinonprint + buy) > takterprint)
buy = (takterprint - terbelinonprint);
terbelinonprint += buy;
buy *= calon[i].harga;
duit -= ((duit > buy) ? buy : duit);
}
beliPlat(0, 50);
}
}
int main() {
cin>>n;
calon_sorted = new plat[n + 1], calon = new plat[n + 1];
for(ull i=0; i<=n; i++){
calon_sorted[i].nomor = i;
calon[i].nomor = i;
cin>>calon_sorted[i].harga;
calon[i].harga = calon_sorted[i].harga;
}
cin>>duit;
sort(calon_sorted, calon_sorted + n + 1, compVal);
if(calon_sorted[0].harga > duit)
cout<<"0\n";
else
getPlat();
return 0;
}

View File

@ -0,0 +1,18 @@
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x[10000], y[10000];
cin>>n;
for(int i=0; i < n; i++)
cin>>x[i];
for(int i=0; i < n; i++)
cin>>y[i];
sort(x, x + n);
sort(y, y + n, greater<int>());
long long sum = 0;
for(int i=0; i < n; i++)
sum += (long long)x[i] * y[i];
cout<<sum<<'\n';
return 0;
}

View File

@ -0,0 +1,12 @@
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, b, arr[20000], i, sum = 0;
cin>>n>>b;
for(i=0; i<n; i++) cin>>arr[i];
sort(arr, arr + n);
while(sum < b) sum += arr[--i];
cout<<(n - i)<<'\n';
return 0;
}

View File

@ -0,0 +1,24 @@
#include <bits/stdc++.h>
using namespace std;
int main() {
short kaki[10001] = {0}, n, m, tmp, used = 0;
cin>>n>>m;
short *sepatu = new short[m];
for(short i=0; i<n; i++) {
cin>>tmp;
kaki[tmp]++;
}
for(short i=0; i<m;i++)
cin>>sepatu[i];
sort(sepatu, sepatu + m);
while(--m >= 0) {
tmp = sepatu[m];
if(kaki[tmp] > 0 || kaki[tmp - 1] > 0) {
kaki[((kaki[tmp] > 0) ? tmp : (tmp - 1))]--;
used++;
}
}
cout<<used<<'\n';
return 0;
}

View File

@ -0,0 +1,37 @@
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool filled[50000] = {0};
ll dp[50000] = {0};
unsigned short n, k, coin[500];
ll tukar(int x) {
if(x == 0) return 0;
if(filled[x - 1]) return dp[x - 1];
if(x < coin[0]) {
filled[x - 1] = true;
dp[x - 1] = INT_MAX;
}
ll min = INT_MAX;
for(unsigned short i=0; i<n; i++) {
if(coin[i] <= x){
ll val = tukar(x - coin[i]) + 1;
if(val < min) min = val;
}
}
filled[x - 1] = true;
dp[x - 1] = min;
return min;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
for(short i=0; i<n; i++)
cin>>coin[i];
cin>>k;
ll val = tukar(k);
cout<<((val >= INT_MAX) ? -1 : val)<<'\n';
return 0;
}

View File

@ -0,0 +1,49 @@
#include <bits/stdc++.h>
using namespace std;
typedef struct queuemember {
int number = 0, banyak = 0;
}member;
int main() {
vector <member> modqueue;
int n, queue_size = 0;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
while(n--) {
string cmd;
cin>>cmd;
if(cmd == "add") {
member tmp;
cin>>tmp.number>>tmp.banyak;
queue_size += tmp.banyak;
modqueue.push_back(tmp);
cout<<queue_size<<'\n';
} else if(cmd == "del") {
int y;
cin>>y;
cout<<modqueue[0].number<<'\n';
queue_size -= y;
if(queue_size <= 0) {
queue_size = 0;
modqueue.clear();
} else {
while(y > 0) {
int elemenawal = modqueue[0].banyak;
modqueue[0].banyak -= y;
if(modqueue[0].banyak <= 0)
modqueue.erase(modqueue.begin());
y -= elemenawal;
}
}
} else {
vector <member> tmpqueue;
for(int i=(modqueue.size() - 1); i >= 0; i--)
tmpqueue.push_back(modqueue[i]);
for(int i=0; i<modqueue.size(); i++)
modqueue[i] = tmpqueue[i];
tmpqueue.clear();
}
}
return 0;
}

View File

@ -0,0 +1,55 @@
#include <bits/stdc++.h>
using namespace std;
typedef struct StackMember {
int nomor, banyak;
} member;
int main() {
vector <member> modstack;
int n, isinya = 0;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
while(n--) {
string command;
cin>>command;
if(command == "add") {
member tmp;
cin>>tmp.nomor>>tmp.banyak;
isinya += tmp.banyak;
cout<<isinya<<'\n';
if(!modstack.empty() && modstack[modstack.size() - 1].nomor == tmp.nomor)
modstack[modstack.size() - 1].banyak += tmp.banyak;
else
modstack.push_back(tmp);
} else if(command == "del") {
int y;
cin>>y;
cout<<modstack[modstack.size() - 1].nomor<<'\n';
isinya -= y;
if(isinya < 0) {
isinya = 0;
modstack.clear();
} else {
while(y > 0) {
int elemenakhir = modstack[modstack.size() - 1].banyak;
modstack[modstack.size() - 1].banyak -= y;
if(modstack[modstack.size() - 1].banyak <= 0)
modstack.erase(modstack.end() - 1);
y -= elemenakhir;
}
}
} else if(command == "dex") {
int x;
cin>>x;
for(int i=0; i < modstack.size(); i++)
modstack[i].nomor -= x;
} else {
int x;
cin>>x;
for(int i=0; i < modstack.size(); i++)
modstack[i].nomor += x;
}
}
return 0;
}

View File

@ -0,0 +1,30 @@
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
vector <int> snq;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
while(n--) {
string cmd;
cin>>cmd;
if(cmd == "push_back") {
int tmp;
cin>>tmp;
snq.push_back(tmp);
} else if(cmd == "push_front") {
int tmp;
cin>>tmp;
if(snq.empty()) snq.push_back(tmp);
else snq.emplace(snq.begin(), tmp);
} else if(cmd == "pop_front")
snq.erase(snq.begin());
else
snq.pop_back();
}
for(int i=0; i < snq.size(); i++)
cout<<snq[i]<<'\n';
return 0;
}