Skip to content

Commit 408f6d1

Browse files
committed
🚀 18-Nov-2020
1 parent 029e6ca commit 408f6d1

11 files changed

+818
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
You are given four integers n, c0, c1 and h and a binary string s of length n.
3+
4+
A binary string is a string consisting of characters 0 and 1.
5+
6+
You can change any character of the string s (the string should be still binary after the change). You should pay h coins for each change.
7+
8+
After some changes (possibly zero) you want to buy the string. To buy the string you should buy all its characters. To buy the character 0 you should pay c0 coins, to buy the character 1 you should pay c1 coins.
9+
10+
Find the minimum number of coins needed to buy the string.
11+
12+
Input
13+
The first line contains a single integer t (1≤t≤10) — the number of test cases. Next 2t lines contain descriptions of test cases.
14+
15+
The first line of the description of each test case contains four integers n, c0, c1, h (1≤n,c0,c1,h≤1000).
16+
17+
The second line of the description of each test case contains the binary string s of length n.
18+
19+
Output
20+
For each test case print a single integer — the minimum number of coins needed to buy the string.
21+
22+
Example
23+
inputCopy
24+
6
25+
3 1 1 1
26+
100
27+
5 10 100 1
28+
01010
29+
5 10 1 1
30+
11111
31+
5 1 10 1
32+
11111
33+
12 2 1 10
34+
101110110101
35+
2 100 1 10
36+
00
37+
outputCopy
38+
3
39+
52
40+
5
41+
10
42+
16
43+
22
44+
Note
45+
In the first test case, you can buy all characters and pay 3 coins, because both characters 0 and 1 costs 1 coin.
46+
47+
In the second test case, you can firstly change 2-nd and 4-th symbols of the string from 1 to 0 and pay 2 coins for that.
48+
Your string will be 00000. After that, you can buy the string and pay 5⋅10=50 coins for that. The total number of coins paid will be 2+50=52.
49+
*/
50+
51+
52+
53+
54+
55+
56+
#include<bits/stdc++.h>
57+
using namespace std;
58+
59+
int main()
60+
{
61+
ios_base::sync_with_stdio(false);
62+
cin.tie(NULL);
63+
64+
int t;
65+
cin>>t;
66+
while(t--){
67+
int n, c0, c1, h;
68+
cin>>n>>c0>>c1>>h;
69+
string s;
70+
cin>>s;
71+
72+
long long sum=0;
73+
74+
for(auto &ch: s){
75+
if(ch=='0') sum+=min(c0, h+c1);
76+
else sum+=min(c1, h+c0);
77+
}
78+
79+
cout<<sum<<endl;
80+
}
81+
82+
83+
return 0;
84+
}
85+
86+
87+
88+
89+
90+
91+
/* Less Efficient
92+
#include<bits/stdc++.h>
93+
using namespace std;
94+
95+
int main()
96+
{
97+
ios_base::sync_with_stdio(false);
98+
cin.tie(NULL);
99+
100+
int t;
101+
cin>>t;
102+
while(t--){
103+
int n, c0, c1, h;
104+
cin>>n>>c0>>c1>>h;
105+
string s;
106+
cin>>s;
107+
108+
long long sum=0;
109+
110+
if(c0==c1){
111+
sum+=n*c0;
112+
} else if(c0 > c1){
113+
if(h<c0){
114+
for(int i=0;i<n;i++){
115+
if(s[i]=='0'){
116+
// s[i]='1';
117+
sum+=h;
118+
}
119+
}
120+
sum+=n*c1;
121+
} else {
122+
for(int i=0;i<n;i++){
123+
if(s[i]=='0') sum+=c0;
124+
else sum+=c1;
125+
}
126+
}
127+
} else {
128+
if(h<c1){
129+
for(int i=0;i<n;i++){
130+
if(s[i]=='1'){
131+
// s[i]='0';
132+
sum+=h;
133+
}
134+
}
135+
sum+=n*c0;
136+
} else {
137+
for(int i=0;i<n;i++){
138+
if(s[i]=='0') sum+=c0;
139+
else sum+=c1;
140+
}
141+
}
142+
}
143+
144+
long long nc=0;
145+
146+
for(int i=0;i<n;i++){
147+
if(s[i]=='0') nc+=c0;
148+
else nc+=c1;
149+
}
150+
151+
sum=min(sum, nc);
152+
153+
cout<<sum<<endl;
154+
}
155+
156+
157+
return 0;
158+
}
159+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
A median of an array of integers of length n is the number standing on the ⌈n2⌉ (rounding up) position in the non-decreasing ordering of its elements. Positions are numbered starting with 1. For example, a median of the array [2,6,4,1,3,5] is equal to 3. There exist some other definitions of the median, but in this problem, we will use the described one.
3+
4+
Given two integers n and k and non-decreasing array of nk integers. Divide all numbers into k arrays of size n, such that each number belongs to exactly one array.
5+
6+
You want the sum of medians of all k arrays to be the maximum possible. Find this maximum possible sum.
7+
8+
Input
9+
The first line contains a single integer t (1≤t≤100) — the number of test cases. The next 2t lines contain descriptions of test cases.
10+
11+
The first line of the description of each test case contains two integers n, k (1≤n,k≤1000).
12+
13+
The second line of the description of each test case contains nk integers a1,a2,…,ank (0≤ai≤109) — given array. It is guaranteed that the array is non-decreasing: a1≤a2≤…≤ank.
14+
15+
It is guaranteed that the sum of nk for all test cases does not exceed 2⋅105.
16+
17+
Output
18+
For each test case print a single integer — the maximum possible sum of medians of all k arrays.
19+
20+
Example
21+
inputCopy
22+
6
23+
2 4
24+
0 24 34 58 62 64 69 78
25+
2 2
26+
27 61 81 91
27+
4 3
28+
2 4 16 18 21 27 36 53 82 91 92 95
29+
3 4
30+
3 11 12 22 33 35 38 67 69 71 94 99
31+
2 1
32+
11 41
33+
3 3
34+
1 1 1 1 1 1 1 1 1
35+
outputCopy
36+
165
37+
108
38+
145
39+
234
40+
11
41+
3
42+
Note
43+
The examples of possible divisions into arrays for all test cases of the first test:
44+
45+
Test case 1: [0,24],[34,58],[62,64],[69,78]. The medians are 0,34,62,69. Their sum is 165.
46+
47+
Test case 2: [27,61],[81,91]. The medians are 27,81. Their sum is 108.
48+
49+
Test case 3: [2,91,92,95],[4,36,53,82],[16,18,21,27]. The medians are 91,36,18. Their sum is 145.
50+
51+
Test case 4: [3,33,35],[11,94,99],[12,38,67],[22,69,71]. The medians are 33,94,38,69. Their sum is 234.
52+
53+
Test case 5: [11,41]. The median is 11. The sum of the only median is 11.
54+
55+
Test case 6: [1,1,1],[1,1,1],[1,1,1]. The medians are 1,1,1. Their sum is 3.
56+
*/
57+
58+
59+
60+
61+
62+
63+
64+
#include<bits/stdc++.h>
65+
using namespace std;
66+
67+
int main()
68+
{
69+
ios_base::sync_with_stdio(false);
70+
cin.tie(NULL);
71+
72+
int t;
73+
cin>>t;
74+
75+
while(t--){
76+
int n, k;
77+
cin>>n>>k;
78+
79+
vector<int> v(n*k);
80+
81+
for(int i=0;i<n*k;i++) cin>>v[i];
82+
83+
int mid=ceil(n/2.0);
84+
85+
int start=0, last=n*k-1;
86+
87+
long long sum=0;
88+
89+
vector<int> a(n);
90+
while(k--){
91+
int i=0, j=n-1;
92+
while(i<mid-1){
93+
a[i]=v[start];
94+
start++;
95+
i++;
96+
}
97+
while(j>=mid-1){
98+
a[j]=v[last];
99+
last--;
100+
j--;
101+
}
102+
103+
sum+=a[mid-1];
104+
}
105+
106+
cout<<sum<<endl;
107+
}
108+
109+
110+
return 0;
111+
}
112+

Diff for: competitive programming/leetcode/1446. Consecutive Characters.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,21 @@ class Solution {
5656
return maxi;
5757
}
5858
};
59+
60+
61+
62+
63+
class Solution {
64+
public:
65+
int maxPower(string s) {
66+
int n=s.length();
67+
int maxi=0, cnt=0;
68+
for(int i=0;i<n-1;i++){
69+
if(s[i]==s[i+1]){
70+
cnt++;
71+
maxi=max(maxi, cnt);
72+
} else cnt=0;
73+
}
74+
return maxi+1;
75+
}
76+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
Sort a linked list using insertion sort.
2+
3+
4+
A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.
5+
With each iteration one element (red) is removed from the input data and inserted in-place into the sorted list
6+
7+
8+
Algorithm of Insertion Sort:
9+
10+
Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list.
11+
At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there.
12+
It repeats until no input elements remain.
13+
14+
Example 1:
15+
16+
Input: 4->2->1->3
17+
Output: 1->2->3->4
18+
Example 2:
19+
20+
Input: -1->5->3->4->0
21+
Output: -1->0->3->4->5
22+
23+
24+
25+
26+
27+
28+
29+
/**
30+
* Definition for singly-linked list.
31+
* struct ListNode {
32+
* int val;
33+
* ListNode *next;
34+
* ListNode() : val(0), next(nullptr) {}
35+
* ListNode(int x) : val(x), next(nullptr) {}
36+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
37+
* };
38+
*/
39+
class Solution {
40+
public:
41+
ListNode* insertionSortList(ListNode* head) {
42+
if(!head) return head;
43+
44+
ListNode *it=head;
45+
46+
while(it!=NULL){
47+
ListNode *curr=it;
48+
int small=it->val;
49+
ListNode *smallNode=it;
50+
while(curr!=NULL){
51+
if(curr->val<small){
52+
small=curr->val;
53+
smallNode=curr;
54+
}
55+
curr=curr->next;
56+
}
57+
58+
swap(it->val, smallNode->val);
59+
it=it->next;
60+
}
61+
return head;
62+
}
63+
};

0 commit comments

Comments
 (0)