-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
277 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.vscode/ | ||
|
||
test/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <vector> | ||
#include <iostream> | ||
#include <string> | ||
|
||
using namespace std; | ||
|
||
/* ldistance of string x(i),y(j) | ||
/ max(i,j) if min(i,j) = 0 | ||
ldiatance(i,j) = | / ldistance(i-1, j) + 1 (delete at the end of x or pend at the end of y) | ||
| min | ldistance(i-1, j-1) + 1(x(i)!=y(j)) (substitute one of the x or y) else | ||
\ \ ldistance(i, j-1) + 1 (delete at the end of y or pend at the end of x) | ||
*/ | ||
|
||
int ldistance(const string& s1, const string& s2){ | ||
vector<vector<int>> dp(s1.length()+1, vector<int>(s2.length()+1, 0)); | ||
for (int i = 0; i <= s1.length(); i++){ | ||
for (int j = 0; j <= s2.length(); j++){ | ||
if(min(i, j) == 0) dp[i][j] = max(j,j); | ||
else { | ||
int ind = s1[i-1] == s2[j-1] ? 0 : 1; | ||
dp[i][j] = min(dp[i-1][j] + 1, min(dp[i][j-1] + 1, dp[i-1][j-1] + ind)); | ||
} | ||
} | ||
} | ||
return dp[s1.length()][s2.length()]; | ||
} | ||
|
||
int main(){ | ||
string s1, s2; | ||
cin >> s1 >> s2; | ||
cout << ldistance(s1, s2) << endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
board = [[0, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 0]] | ||
|
||
def print_board(board, cnt=-1): | ||
print(f"Board {cnt}:" ) | ||
for row in board: | ||
print(row) | ||
print() | ||
|
||
def canplace(board, row, col): | ||
for i in range(row): | ||
if board[row][i] == 1 or board[i][col] == 1: | ||
return False | ||
for i in range(row): | ||
for j in range(8): | ||
if (i+j == row+col) or (i-j == row-col): | ||
if board[i][j] == 1: | ||
return False | ||
return True | ||
|
||
count = 0 | ||
iter_count = 0 | ||
def recurse(board, row): | ||
if row == 8: | ||
global count | ||
count += 1 | ||
print_board(board, count) | ||
return | ||
for i in range(8): | ||
if canplace(board, row, i): | ||
global iter_count | ||
iter_count += 1 | ||
board[row][i] = 1 | ||
recurse(board, row+1) | ||
board[row][i] = 0 | ||
|
||
if __name__ == '__main__': | ||
print_board(board) | ||
recurse(board, 0) | ||
print("count:", count) | ||
print("iter:", iter_count) | ||
print("Done") | ||
print_board(board) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include <iostream> | ||
#include <algorithm> | ||
#include <unordered_map> | ||
|
||
using namespace std; | ||
|
||
|
||
class Solution { | ||
public: | ||
vector<int> twoSum(vector<int>& nums, int target) { | ||
unordered_map<int, int> htable; | ||
for(auto i = 0; i < nums.size(); i++){ | ||
auto it = htable.find(target - nums[i]); | ||
if (it != htable.end()) return {it->second, i}; | ||
htable[nums[i]] = i; | ||
} | ||
return {}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <unordered_map> | ||
#include <iostream> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
vector<vector<int>> fourSum(vector<int> nums, int target) { | ||
auto n = nums.size(); | ||
if(n < 4) return {}; | ||
|
||
sort(nums.begin(), nums.end()); | ||
|
||
unordered_map<long, int> hmap; | ||
vector<vector<int>> ret = {}; | ||
for(int i = 0; i < n-3; ++i){ | ||
if(i > 0 && nums[i] == nums[i-1]) continue; | ||
for(int j = i+1; j < n-2; ++j){ | ||
auto psum = (long)nums[i] + (long)nums[j]; | ||
if(psum + (long)nums[j+1] + (long)nums[j+2] > (long)target) break; | ||
if(psum + (long)nums[n-2] + (long)nums[n-1] < (long)target) continue; | ||
if(j > i+1 && nums[j] == nums[j-1]) continue; | ||
hmap.clear(); | ||
pair<int,int> lastpair; | ||
bool firsttime = true; | ||
for(int k = j+1; k < n; ++k){ | ||
//if(k > j+2 && nums[k] == nums[k-1]) continue; | ||
//if(nums[i] + nums[k] + nums[j] + nums[hmap[nums[k]]] > target) break; //hmap means a past k | ||
if(hmap.find((long)nums[k]) != hmap.end()){ | ||
if(!firsttime && lastpair.first == nums[k] && lastpair.second == nums[hmap[nums[k]]]) continue; | ||
ret.push_back({nums[i], nums[j], nums[k], nums[hmap[(long)nums[k]]]}); | ||
lastpair.first = nums[k]; | ||
lastpair.second = nums[hmap[(long)nums[k]]]; | ||
if(firsttime) firsttime = false; //初始化的pair不会被考虑 | ||
} | ||
else{ | ||
hmap[(long)target - (long)nums[k] - (long)nums[i] - (long)nums[j]] = k; | ||
} | ||
} | ||
} | ||
} | ||
return ret; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include <unordered_map> | ||
#include <iostream> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
#include <unordered_map> | ||
#include <unordered_set> | ||
#include <iostream> | ||
#include <algorithm> | ||
#include <functional> | ||
|
||
using namespace std; | ||
|
||
struct v4hash | ||
{ | ||
size_t operator()(const vector<int>& v) const | ||
{ | ||
auto h = hash<int>(); | ||
std::size_t seed = v.size(); | ||
for(auto& i : v) { | ||
seed ^= h(i) + 0x9e3779b9 + (seed << 6) + (seed >> 2); | ||
} | ||
return seed; | ||
} | ||
}; | ||
|
||
class Solution { | ||
public: | ||
vector<vector<int>> fourSum(vector<int> nums, int target) { | ||
unordered_set<vector<int>, v4hash> rets; | ||
unordered_map<long, int> hmap; | ||
|
||
for(int i = 0; i < nums.size(); ++i){ | ||
for(int j = i+1; j < nums.size(); ++j){ | ||
hmap.clear(); | ||
for(int k = j+1; k < nums.size(); ++k){ | ||
if(hmap.find((long)nums[k]) != hmap.end()){ | ||
vector<int> temp{nums[i], nums[j], nums[k], nums[hmap[(long)nums[k]]]}; | ||
sort(temp.begin(), temp.end()); //确保不同顺序的四元组不会重复 | ||
rets.insert(temp); | ||
} else { | ||
hmap[(long)target - (long)nums[k] - (long)nums[i] - (long)nums[j]] = k; | ||
} | ||
} | ||
} | ||
} | ||
vector<vector<int>> ret(rets.begin(), rets.end()); | ||
return ret; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <unordered_map> | ||
#include <iostream> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
vector<vector<int>> fourSum(vector<int>& nums, int target) { | ||
// 双指针法 | ||
int n = nums.size(); | ||
vector<vector<int>> result; | ||
sort(nums.begin(), nums.end()); | ||
for (int i = 0; i < n - 3; i++) { | ||
if (i > 0 && nums[i] == nums[i - 1]) continue; //去重 | ||
if (nums[i] > max(0, target)) break; //special case: (分类讨论) 如果target为负数,nums[i] > 0,直接break; 如果target为正数,nums[i] > target,直接break | ||
for (int j = i + 1; j < n - 2; j++) { | ||
long x = nums[i] + nums[j]; | ||
if (x + nums[j + 1] + nums[j + 2] > target) break; //special case | ||
if (x + nums[n - 2] + nums[n - 1] < target) continue; //special case | ||
if (j > i + 1 && nums[j] == nums[j - 1]) continue; //去重 | ||
int left = j + 1, right = n - 1; | ||
while (left < right) { | ||
long s = x + nums[left] + nums[right]; | ||
if (s < target) left++; | ||
else if (s > target) right--; | ||
else { | ||
result.push_back({nums[i], nums[j], nums[left], nums[right]}); | ||
for (++left; left < right && nums[left] == nums[left - 1]; left++); //去重 | ||
for (--right; right > left && nums[right] == nums[right + 1]; right--); //去重 | ||
} | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <algorithm> | ||
#include <cmath> | ||
#include <cstdio> | ||
#include <cstring> | ||
#include <iostream> | ||
#include <queue> | ||
using namespace std; | ||
|
||
struct f { | ||
long long d; | ||
long long p; | ||
} a[100005]; | ||
|
||
bool cmp(f A, f B) { return A.d < B.d; } | ||
|
||
priority_queue<long long, vector<long long>, greater<long long> > | ||
q; // 小根堆维护最小值 | ||
|
||
int main() { | ||
long long n, i; | ||
cin >> n; | ||
for (i = 1; i <= n; i++) { | ||
scanf("%lld%lld", &a[i].d, &a[i].p); | ||
} | ||
sort(a + 1, a + n + 1, cmp); | ||
long long ans = 0; | ||
for (i = 1; i <= n; i++) { | ||
if (a[i].d <= (int)q.size()) { // 超过截止时间 | ||
if (q.top() < a[i].p) { // 后悔 | ||
ans += a[i].p - q.top(); | ||
q.pop(); | ||
q.push(a[i].p); | ||
} | ||
} else { // 直接加入队列 | ||
ans += a[i].p; | ||
q.push(a[i].p); | ||
} | ||
} | ||
cout << ans << endl; | ||
return 0; | ||
} |