-
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> number;
vector<char> charter;
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
int temp;
char c;
cin >> temp;
number.push_back(temp);
if (i != n - 1) {
cin >> c;
charter.push_back(c);
}
}
int k = 0;
for (int i = 0; i < n - 1; ++i) {
if ((charter[i] == '+')) {
if ((NULL == charter[i + 1]) && NULL == (charter[i - 1]) ||
((NULL == charter[i + 1]) && (charter[i - 1] == '+'))) {
sort(number.begin() + k, number.begin() + i + 2);
} else if ('+' == charter[i + 1]) {
continue;
} else if ('-' == charter[i + 1]) {
if (NULL == charter[i - 1]) {
sort(number.begin() + k, number.begin() + i + 2);
k = i + 1;
continue;
} else if ('/' != charter[i - 1] && '*' != charter[i - 1]) {
sort(number.begin() + k, number.begin() + i + 2);
k = i + 1;
}
} else if (('/' == charter[i + 1]) || ('*' == charter[i + 1]) && ('+' == charter[i - 1])) {
sort(number.begin() + k, number.begin() + i + 1);
k = i + 1;
} else
k = i + 1;
} else if (charter[i] == '-') {
if ((NULL == charter[i + 1]) && ('-' == charter[i - 1])) {
sort(number.begin() + k + 1, number.begin() + i + 2);
} else if ('-' == charter[i + 1]) {
continue;
} else if ('+' == charter[i + 1] && '-' == charter[i - 1]) {
sort(number.begin() + k + 1, number.begin() + i + 2);
k = i + 1;
} else if (((charter[i + 1] == '/') || (charter[i + 1] == '*')) && '-' == charter[i - 1]) {
sort(number.begin() + k + 1, number.begin() + i + 1);
k = i + 1;
} else
k = i + 1;
} else if (charter[i] == '*') {
if ((NULL == charter[i + 1]) && NULL == (charter[i - 1]) || NULL == charter[i + 1]) {
sort(number.begin() + k, number.begin() + i + 2);
} else if ('*' == charter[i + 1]) {
continue;
} else if ('/' == charter[i + 1]) {
sort(number.begin() + k, number.begin() + i + 2);
k = i + 1;
} else if ((charter[i + 1] == '+') || (charter[i + 1] == '-')) {
sort(number.begin() + k, number.begin() + i + 2);
k = i + 1;
}
} else if (charter[i] == '/') {
if ((NULL == charter[i + 1]) && ('/' == charter[i - 1])) {
sort(number.begin() + k, number.begin() + i + 2);
} else if ('/' == charter[i + 1]) {
continue;
} else if ('*' == charter[i + 1] && ('/' == charter[i - 1])) {
sort(number.begin() + k, number.begin() + i + 2);
k = i + 1;
} else if ((('+' == charter[i + 1]) || ('-' == charter[i + 1])) && ('/' == charter[i - 1])) {
sort(number.begin() + k, number.begin() + i + 2);
k = i + 1;
} else
k = i + 1;
}
}
for (int i = 0; i < n; ++i) {
cout << number[i] << ' ';
if (i != n - 1) {
cout << charter[i] << ' ';
}
}
return 0;
}
-
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n, k;
cin>> n>> k;
vector<int>a;
int count=0;
for (int i = 0; i < n; ++i) {
int temp;cin>>temp;
if ( temp % 3 == 0 ){
count++;
}
else
a.push_back(temp);
}
for (int i = 0; k && (i < a.size()); ++i) {
for (int j = i+1; j < a.size(); ++j) {
if ((a[i]+a[j]) % 3 == 0){
a.erase(a.begin()+i);
a.erase(a.begin()+j);
if(k--)
count++;
}
}
}
for (int i = 0; k && (i < a.size()); ++i) {
for (int j = i + 1; j < a.size(); ++j) {
for (int t = j + 1; t < a.size(); ++t) {
if ((a[i] + a[j]+ a[k]) % 3 == 0) {
a.erase(a.begin() + i);
a.erase(a.begin() + j);
a.erase(a.begin() + t);
k=k-2;
if(k)
count++;
}
}
}
}
cout << count << endl;
return 0;
}
-
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
long long functionsum(long long n, int a ,int b ,int c, int f0 ){
if(n < 0 ){
return 0;
}
else if(n == 0){
return f0;
}
else
return a*functionsum(n-1, a , b ,c, f0) + b*functionsum(n-2, a , b ,c, f0)+c*functionsum(n-3, a , b ,c, f0)+2*n*n-n+32767;
}
int main() {
long long n;
int a ,b,c, f0;
cin>>n>>a>>b>>c>>f0;
cout<<functionsum(n,a,b,c,f0);
return 0;
}
-
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void markMatrix(vector<vector<int>> &label, vector<vector<int>> &mark, int x, int y, int n, int m) {
if (x < 0 || y < 0 || x > n || y > m)
return;
if ((label[x][y] == 'X') || (mark[x][y] == 1))
return;
else {
mark[x][y] = 1;
if ((x - 1 >= 0) && (label[x - 1][y] == '.') && (!(mark[x - 1][y]) == 1))
markMatrix(label, mark, x - 1, y, n, m);
if ((y - 1 >= 0) && (label[x][y - 1] == '.') && (!(mark[x][y - 1]) == 1))
markMatrix(label, mark, x, y - 1, n, m);
if ((y + 1 <= m) && (label[x][y + 1] == '.') && (!(mark[x][y + 1]) == 1))
markMatrix(label, mark, x, y + 1, n, m);
if ((x + 1 <= n) && (label[x + 1][y] == '.') && (!(mark[x + 1][y]) == 1))
markMatrix(label, mark, x + 1, y, n, m);
}
}
int main() {
int t;
cin >> t;
while (t--) {
int n, m;
int x1, y1, x2, y2;
cin >> n >> m;
vector<vector<int>> label(n, vector<int>(m));
vector<vector<int>> mark(n, vector<int>(m));
char c;
for (int i = 0; i < n; ++i) {
getchar();
for (int j = 0; j < m; ++j) {
cin >> c;
label[i][j] = int(c);
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
mark[i][j] = 0;
}
}
cin >> x1 >> y1;
cin >> x2 >> y2;
if ((x1 == x2) && (y1 == y2)) {
cout << "YES" << endl;
} else {
mark[x1 - 1][y1 - 1] = 2;
label[x1 - 1][y1 - 1] = 'O';
markMatrix(label, mark, x1 - 1, y1 - 1, n - 1, m - 1);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cout << mark[i][j];
}
cout << endl;
}
if (mark[x2 - 1][y2 - 1]){
cout << "YES" << endl;
}
else
cout << "NO" << endl;
}
}
return 0;
}
-
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n, m;
vector<int> w;
vector<int> v;
cin >> n >> m;
int temp = 0;
for (int i = 0; i < n; i++) {
cin >> temp;
w.push_back(temp);
}
for (int i = 0; i < n; i++) {
cin >> temp;
v.push_back(temp);
}
int count = *min_element(w.begin(), w.end());
int remin_money = m;
while (remin_money >= 0) {
int min = *min_element(w.begin(), w.end());
for (int i = 0; i < n; i++) {
if (w[i] == min) {
remin_money = remin_money - v[i];
w[i] = w[i] + 1;
}
}
if (remin_money >= 0) count++;
}
cout << count << endl;
}
-
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int n,m,k;
vector<int> people;
vector<int> language;
cin>>n>>m>>k;
for (int i = 0; i < k; ++i) {
int tempp,templ;
cin>>tempp>>templ;
people.push_back(tempp);
language.push_back(templ);
}
sort(language.begin(),language.end());
language.erase(unique(language.begin(),language.end()),language.end());
sort(people.begin(),people.end());
people.erase(unique(people.begin(),people.end()),people.end());
cout<<language.size()-1 + n-people.size();
return 0;
}
-
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int max(int a, int b) {
return a > b ? a : b;
}
int main() {
int i, k;
vector<int> array;
int temp;
cin >> temp;
array.push_back(temp);
while (cin.get() != '\n') {
cin >> temp;
array.push_back(temp);
}
vector<int> dp(array.size());
dp[0] = array[0];
for (i = 1; i < array.size(); ++i) {
dp[i] = max(array[i], dp[i - 1] + array[i]);
}
k = dp[0];
for (i = 1; i < array.size(); ++i) {
if (dp[i] > k) {
k = dp[i];
}
}
cout << k << endl;
return 0;
}
-
//方法1
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int maxProfit(const vector<int> prices) {
if (prices.empty())
return 0;
int buy1 = -prices[0], sell1 = 0, buy2 = buy1, sell2 = 0;
for (int i = 1; i < prices.size(); ++i) {
buy1 = max(buy1, -prices[i]);
sell1 = max(sell1, buy1 + prices[i]);
buy2 = max(buy2, sell1 - prices[i]);
sell2 = max(sell2, buy2 + prices[i]);
}
return max(0, sell2);
}
int main() {
vector<int> prices = {2, 1, 5, 0, 2, 3, 1, 4};
cout << maxProfit(prices) << endl;
return 0;
}
//方法2
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> prices = {2, 1, 5, 0, 2, 3, 1, 4};
vector<int> pre(prices.size());
vector<int> post(prices.size());
pre[0] = 0;
int min_price = prices[0];
for (int i = 1; i < prices.size(); ++i) {
pre[i] = max(pre[i - 1], prices[i] - min_price);
min_price = min_price > prices[i] ? prices[i] : min_price;
}
post[prices.size() - 1] = 0;
int max_price = prices[prices.size() - 1];
for (int i = prices.size() - 2; i >= 0; i--) {
post[i] = max(post[i + 1], max_price - prices[i]);
max_price = max_price < prices[i] ? prices[i] : max_price;
}
int max_profit = pre[0] + post[prices.size() - 1];
for (int i = 1; i < prices.size() - 1; ++i) {
max_profit = max(pre[i] + post[prices.size() - i - 1], max_profit);
}
cout << max_profit << endl;
return 0;
}