二哥的OJ 1251-final

1262

#include <iostream>
using namespace std;
 
struct node
{
	int arrive, leave;
	node() {}
	node(int a, int l) :arrive(a), leave(l) {}
};
 
int mid(int a, int b, int c)
{
	if (a > b)
	{
		if (b > c)return b;
		else if (c > a)return a;
		else return c;
	}
	else
	{
		if (a > c)return a;
		else if (c > b) return b;
		else return c;
	}
}
 
void partition(node arr[], int begin, int end, int &newEnd, int &newBegin)
{
	int pivot = mid(arr[begin].arrive, arr[end].arrive, arr[begin + end >> 1].arrive);
	int pos = begin;
	while (pos <= end)
	{
		if (arr[pos].arrive < pivot)
		{
			node tmp = arr[begin];
			arr[begin++] = arr[pos];
			arr[pos++] = tmp;
		}
		else if (arr[pos].arrive > pivot)
		{
			node tmp = arr[end];
			arr[end--] = arr[pos];
			arr[pos] = tmp;
		}
		else ++pos;
	}
	newEnd = begin - 1;
	newBegin = end + 1;
}
 
void quickSort(node arr[], int begin, int end)
{
	if (begin < end)
	{
		int newEnd, newBegin;
		partition(arr, begin, end, newEnd, newBegin);
		quickSort(arr, begin, newEnd);
		quickSort(arr, newBegin, end);
	}
}
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	node arr[5005];
	int a[5005], l[5005];
	int cnt = 0;
	int n;
	cin >> n;
	for (int i = 0;i < n;++i) cin >> arr[i].arrive >> arr[i].leave;
	quickSort(arr, 0, n - 1);
	a[0] = arr[0].arrive;
	l[0] = arr[0].leave;
	++cnt;
	for (int i = 1;i < n;++i)
	{
		if (arr[i].arrive <= l[cnt - 1])
		{
			if (arr[i].leave > l[cnt - 1])l[cnt - 1] = arr[i].leave;
		}
		else
		{
			a[cnt] = arr[i].arrive;
			l[cnt++] = arr[i].leave;
		}
	}
	int longestIn = 0, longestOut = 0;
	for (int i = 0;i < cnt;++i)
	{
		if (l[i] - a[i] > longestIn)longestIn = l[i] - a[i];
		if (i > 0 && a[i] - l[i - 1] > longestOut)longestOut = a[i] - l[i - 1];
	}
	cout << longestIn << ' ' <<longestOut << '\n';
	cin.get();
	cin.get();
	return 0;
}

1273

//乱七八糟。烂题
#include <iostream>
using namespace std;
 
int main()
{
int i,n,cnt=1;
cin>>n;
long long tmp,pre;
cin>>pre;
for(i=1;i<n;++i)
{
cin>>tmp;
if(pre<=tmp)
++cnt;
pre=tmp;
}
cout<<cnt-1;
return 0;
}

1283

#include <iostream>
using namespace std;
 
class disjointSet
{
private:
	int sz;
	int cnt;
	int *parent;
public:
	disjointSet(int length) :sz(length), cnt(length), parent(new int[length + 1]())
	{
		for (int i = 1;i <= sz;++i)parent[i] = -1;
	}
	~disjointSet() { delete[]parent; }
	int find(int n)
	{
		if (parent[n] < 0)return n;
		return parent[n] = find(parent[n]);
	}
	void Union(int root1, int root2)
	{
		if (root1 == root2)return;
		if (parent[root1] < parent[root2])
		{
			parent[root1] += parent[root2];
			parent[root2] = root1;
		}
		else
		{
			parent[root2] += parent[root1];
			parent[root1] = root2;
		}
		--cnt;
	}
	int count() const
	{
		return cnt;
	}
};
 
int num[1000] = { 1,0 };
int len = 1;
 
void pow2n(int n)
{
	for (int i = 0;i < n;++i)
	{
		for (int j = 0;j < len;++j) num[j] *= 2;
		for (int j = 0;j < len;++j)
		{
			num[j + 1] += num[j] / 10;
			num[j] = num[j] % 10;
		}
		if (num[len])++len;
	}
}
 
int main()
{
	int n, m;
	cin >> n >> m;
	disjointSet s(n);
	int x, y;
	for (int i = 0;i < m;++i)
	{
		cin >> x >> y;
		s.Union(s.find(x), s.find(y));
	}
	pow2n(n - s.count());
	for (int i = len - 1;i >= 0;--i)
		cout << num[i];
	cout << endl;
	cin.get();
	cin.get();
	return 0;
}

1320

#include <iostream>
using namespace std;
 
int state[1000][1000];
int triangle[1000][1000];
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int R;
	cin >> R;
	for (int i = 0;i < R;++i)
		for (int j = 0;j <= i;++j)
			cin >> triangle[i][j];
	for (int j = 0;j <= R - 1;++j)
		state[R - 1][j] = triangle[R - 1][j];
	for (int i = R - 2;i >= 0;--i)
		for (int j = 0;j <= i;++j)
			state[i][j] = (state[i + 1][j] > state[i + 1][j + 1] ? state[i + 1][j] : state[i + 1][j + 1]) + triangle[i][j];
	cout << state[0][0] << '\n';
	cin.get();
	cin.get();
	return 0;
}

1348

#include <iostream>
#include <iomanip>
using namespace std;
 
int partition(long long* arr, int begin, int end)
{
	long long pivot = arr[begin];
	while (begin < end)
	{
		while (begin < end&&arr[end] >= pivot)--end;
		arr[begin] = arr[end];
		while (begin < end&&arr[begin] <= pivot)++begin;
		arr[end] = arr[begin];
	}
	arr[begin] = pivot;
	return begin;
}
 
void quick_sort(long long *arr, int begin, int end)
{
	if (begin < end)
	{
		int mid = partition(arr, begin, end);
		quick_sort(arr, begin, mid - 1);
		quick_sort(arr, mid + 1, end);
	}
}
 
long long q1[10000000], q2[10000000];
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int h1 = 0, h2 = 0;
	int b1 = 0, b2 = 0;
	int M, N;
	long long li[100000];
	cin >> N;
	for (int i = 0;i < N;++i)cin >> li[i];
	cin >> M;
	int sex;
	long long arriveTime;
	for (int i = 0;i < M;++i)
	{
		cin >> sex >> arriveTime;
		if (sex == 1) q1[b1++] = arriveTime;
		else q2[b2++] = arriveTime;
	}
	quick_sort(q1, 0, b1 - 1);
	quick_sort(q2, 0, b2 - 1);
	long long time = 0;
	double sum1 = 0, sum2 = 0;
	for (int i = 0;i < N - 1;++i)
	{
		while (h1 != b1&&h2 != b2&&q1[h1] <= time&&q2[h2] <= time)
		{
			sum1 += time - q1[h1++];
			sum2 += time - q2[h2++];
		}
		time += li[i];
	}
	while (h1 != b1) sum1 += time - q1[h1++];
	while (h2 != b2) sum2 += time - q2[h2++];
	cout << setprecision(2) << setiosflags(ios::fixed) << sum1 / b1 << ' ' << sum2 / b2 << '\n';
	cin.get();
	cin.get();
	return 0;
}

1357

#include <iostream>
using namespace std;
 
char matrix[5][5];
bool selected[5][5] = { false };
int ans = 0;
int path[7];
 
bool checkInPath(int pos)
{
	if (pos < 0 || pos >= 25) return false;
	if (selected[pos / 5][pos % 5]) return false;
	for (int i = 0;i < 7;++i)
		if (path[i] == pos) return true;
	return false;
}
 
void check(int path[])
{
	int hNum = 0, jNum = 0;
	int start = path[0];
	selected[start / 5][start % 5] = true;
	int queue[7] = { start };
	int front = 0, back = 1;
	while (back > front)
	{
		int pos = queue[front++];
		if (matrix[pos / 5][pos % 5] == 'H') ++hNum;
		else ++jNum;
		if (hNum > 3)break;
		if (checkInPath(pos - 5))
		{
			queue[back++] = pos - 5;
			selected[pos / 5 - 1][pos % 5] = true;
		}
		if (checkInPath(pos + 5))
		{
			queue[back++] = pos + 5;
			selected[pos / 5 + 1][pos % 5] = true;
		}
		if (pos % 5 != 0 && checkInPath(pos - 1))
		{
			queue[back++] = pos - 1;
			selected[(pos - 1) / 5][(pos - 1) % 5] = true;
		}
		if (pos % 5 != 4 && checkInPath(pos + 1))
		{
			queue[back++] = pos + 1;
			selected[(pos + 1) / 5][(pos + 1) % 5] = true;
		}
	}
	for (int i = 0;i < 7;++i)
		selected[path[i] / 5][path[i] % 5] = false;
	if(back==7&&hNum<=3) ++ans;
}
 
int main()
{
	for (int i = 0;i < 5;++i)
		for (int j = 0;j < 5;++j)
			cin >> matrix[i][j];
	for (path[0] = 0;path[0] < 19;++path[0])
		for (path[1] = path[0] + 1;path[1] < 20;++path[1])
			for (path[2] = path[1] + 1;path[2] < 21;++path[2])
				for (path[3] = path[2] + 1;path[3] < 22;++path[3])
					for (path[4] = path[3] + 1;path[4] < 23;++path[4])
						for (path[5] = path[4] + 1;path[5] < 24;++path[5])
							for (path[6] = path[5] + 1;path[6] < 25;++path[6])
								check(path);
	cout << ans << endl;
	cin.get();
	cin.get();
	return 0;
}

1376

#include <iostream>
using namespace std;
 
int n;
 
long long query(long long arr[], int x)
{
	long long sum = 0;
	for (int i = x;i > 0;i -= i&(-i)) sum += arr[i];
	return sum;
}
 
void add(long long arr[], int x, int y)
{
	for (int i = x;i <= n;i += i&(-i)) arr[i] += y;
}
 
void change(long long arr[],int x,int y)
{
	add(arr, x, y - (query(arr, x) - query(arr, x - 1)));
}
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> n;
	long long *a = new long long[n + 1]();
	int m, type, x, y;
	for (int i = 0;i < n;++i)
	{
		cin >> x;
		add(a, i+1, x);//注意为i+1;
	}
	cin >> m;
	for (int i = 0;i < m;++i)
	{
		cin >> type >> x >> y;
		switch (type)
		{
		case 1:
			change(a, x, y);
			break;
		case 2:
			cout << query(a, y) - query(a, x - 1) << '\n';
		}
	}
	delete[]a;
	cin.get();
	cin.get();
	return 0;
}

1509

#include <iostream>
#include <iomanip>
using namespace std;
 
int dw[13] = { 0,0,3,3,6,8,11,13,16,19,21,24,26 };
int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int a, b, c;
 
void printHoliday(int year)
{
	int w1 = (2 + year - 1850 + (year-1) / 4 - (year-1) / 100 + (year-1) / 400 - 1849 / 4 + dw[a]) % 7;
	if (a > 2 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)) w1 = (w1 + 1) % 7;
	int holiday = 1 + (c + 7 - w1) % 7 + (b - 1) * 7;
	if (holiday > days[a])cout << "none\n";
	else cout << year << '/' << setw(2) << a << '/' << setw(2) << holiday << '\n';
}
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cout.fill('0');
	int y1, y2;
	cin >> a >> b >> c >> y1 >> y2;
	c = c % 7;
	for (int i = y1;i <= y2;++i)
		printHoliday(i);
	cin.get();
	cin.get();
	return 0;
}

1515

#include <iostream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
 
int n, m;
int map[1000][1000];
int fromS[1000][1000];
int fromH[1000][1000];
long long sum = LLONG_MAX;
int shopGet;
 
struct pos
{
	int x, y;
	pos() :x(0), y(0) {}
	pos(int a,int b):x(a),y(b){}
};
 
vector<pos> shop;
 
void putWithCheck(queue<pos>& q, pos cur, int dx, int dy, int state[][1000])
{
	if (cur.x + dx < 0 || cur.x + dx >= m || cur.y + dy < 0 || cur.y + dy >= n) return;
	if (map[cur.x + dx][cur.y + dy] != 1 && state[cur.x + dx][cur.y + dy] == INT_MAX)
	{
		q.push(pos(cur.x + dx, cur.y + dy));
		state[cur.x + dx][cur.y + dy] = state[cur.x][cur.y] + 1;
		if (map[cur.x + dx][cur.y + dy] == 4)++shopGet;
	}
}
 
void search(queue<pos>& q, int state[][1000])
{
	shopGet = 0;
	while (!q.empty())
	{
		pos tmp = q.front();
		q.pop();
		putWithCheck(q, tmp, 1, 0, state);
		putWithCheck(q, tmp, -1, 0, state);
		putWithCheck(q, tmp, 0, 1, state);
		putWithCheck(q, tmp, 0, -1, state);
		if (shopGet == shop.size()) break;
	}
}
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> n >> m;
	pos start, home;
	for(int i=0;i<m;++i)
		for (int j = 0;j < n;++j)
		{
			cin >> map[i][j];
			if (map[i][j] == 4)
			{
				shop.push_back(pos(i, j));
				fromS[i][j] = fromH[i][j] = INT_MAX;
			}
			else if (map[i][j] == 2)
			{
				fromS[i][j] = 0;
				start.x = i;
				start.y = j;
				fromH[i][j] = INT_MAX;
			}
			else if (map[i][j] == 3)
			{
				fromH[i][j] = 0;
				home.x = i;
				home.y = j;
				fromS[i][j] = INT_MAX;
			}
			else fromS[i][j] = fromH[i][j] = INT_MAX;
		}
	queue<pos> q1, q2;
	q1.push(start);
	q2.push(home);
	search(q1, fromS);
	search(q2, fromH);
	for (int i = 0;i < shop.size();++i)
	{
		long long tmp = (long long)fromS[shop[i].x][shop[i].y] + fromH[shop[i].x][shop[i].y];
		if (sum > tmp) sum = tmp;
	}
	cout << sum << '\n';
	cin.get();
	cin.get();
	return 0;
}

1533

#include <iostream>
using namespace std;
 
int steps(int n, int x, int y)
{
	if (x == 1) return y;
	if (y == n) return x + n - 1;
	if (x == n) return 3 * n - y - 1;
	if (y == 1) return 4 * n - x - 2;
	return 4 * n - 4 + steps(n - 2, x - 1, y - 1);
}
 
int main()
{
	int n, x, y;
	cin >> n >> x >> y;
	cout << steps(n, x, y) -1 << endl;
	cin.get();
	cin.get();
	return 0;
}

1535

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
 
int digitSum(long long num)
{
	int sum = 0;
	while (num != 0)
	{
		sum += num % 10;
		num /= 10;
	}
	return sum;
}
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	vector<long long> v;
	int k, p, q;
	int l, r;
	cin >> k >> p >> q;
	cin >> l >> r;
	int ll = 0, rr = 9 * 9;
	while (ll <= rr)
	{
		if (pow(ll, k)*p + q >= l)break;
		int mid = (ll + rr) / 2;
		if (pow(mid, k)*p + q < l)ll = mid + 1;
		else rr = mid;
	}
	long long tmp;
	for (int i = ll;(tmp = pow(i, k)*p + q) <= r&&i <= 9 * 9;++i)
	{
		if (digitSum(tmp) == i) v.push_back(tmp);
	}
	cout << v.size() << endl;
	if (v.size() == 0) cout << -1 << '\n';
	else
	{
		for (int i = 0;i < v.size();++i) cout << v[i] << ' ';
		cout << '\n';
	}
	cin.get();
	cin.get();
	return 0;
}

1539

#include <iostream>
using namespace std;
 
int n;
bool y[14] = { false };
bool add[27] = { false };
bool sub[26] = { false };
int ans = 0;
int state[14] = { 0 };
int cnt = 0;
 
void dfs(int x)
{
	if (x > n)
	{
		++ans;
		if (cnt < 3)
		{
			for (int i = 1;i <= n;++i)cout << state[i] << ' ';
			cout << endl;
			++cnt;
		}
		return;
	}
	for (int i = 1;i <= n;++i)
	{
		if (!y[i] && !add[i + x] && !sub[i - x + n])
		{
			y[i] = add[i + x] = sub[i - x + n] = true;
			state[x] = i;
			dfs(x + 1);
			y[i] = add[i + x] = sub[i - x + n] = false;
		}
	}
}
 
int main()
{
	cin >> n;
	dfs(1);
	cout << ans << endl;
	cin.get();
	cin.get();
	return 0;
}

1541

#include <iostream>
using namespace std;
 
int k, n;
int qn[200000];
int qi[200000];
int front = 0;
int back = -1;
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int tmp;
	cin >> k >> n;
	int i;
	for (i = 0;i < k;++i)
	{
		cin >> tmp;
		while (front <= back&&qn[back] <= tmp)--back;
		qn[++back] = tmp;
		qi[back] = i;
	}
	for (;i < n;++i)
	{
		cout << qn[front] << ' ';
		cin >> tmp;
		if (i - qi[front] >= k)++front;
		while (front <= back&&qn[back] <= tmp)--back;
		qn[++back] = tmp;
		qi[back] = i;
	}
	cout << qn[front] << '\n';
	cin.get();
	cin.get();
	return 0;
}

1544

#include <iostream>
using namespace std;
 
int treeArr[11][100001] = { 0 };
int dat[100000];
int assist[100000];
int changed[100000];
int n;
 
int mid(int a, int b, int c)
{
	if (a < b)
	{
		if (b < c) return b;
		else if (c < a) return a;
		else return c;
	}
	else
	{
		if (a < c)return a;
		else if (c < b) return b;
		else return c;
	}
}
 
void partition(int begin, int end, int& newEnd, int& newBegin)
{
	int pivot = mid(dat[assist[begin]], dat[assist[end]], dat[assist[(begin + end) / 2]]);
	int pos = begin;
	while (pos <= end)
	{
		if (dat[assist[pos]] < pivot)
		{
			int tmp = assist[pos];
			assist[pos++] = assist[begin];
			assist[begin++] = tmp;
		}
		else if (dat[assist[pos]] > pivot)
		{
			int tmp = assist[pos];
			assist[pos] = assist[end];
			assist[end--] = tmp;
		}
		else ++pos;
	}
	newEnd = begin - 1;
	newBegin = end + 1;
}
 
void quick_sort(int begin, int end)
{
	if (begin < end)
	{
		int newEnd, newBegin;
		partition(begin, end, newEnd, newBegin);
		quick_sort(begin, newEnd);
		quick_sort(newBegin, end);
	}
}
 
void change(int begin, int end)
{
	quick_sort(begin, end);
	changed[assist[begin]] = 1;
	int j = 1;
	for (int i = begin + 1;i <= end;++i)
	{
		if (dat[assist[i - 1]] == dat[assist[i]])changed[assist[i]] = j;
		else changed[assist[i]] = ++j;
	}
}
 
void add(int *arr, int x, int y)
{
	for (int i = x;i <= n;i += i&(-i))arr[i] = (arr[i] + y) % 1000000007;
}
 
long long query(int *arr, int x)
{
	long long sum = 0;
	for (int i = x;i > 0;i -= i&(-i))sum = (sum + arr[i]) % 1000000007;
	return sum;
}
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int k;
	cin >> n >> k;
	for (int i = 0;i < n;++i)
	{
		cin >> dat[i];
		assist[i] = i;
	}
	change(0, n - 1);
	int sum = 0;
	for (int j = n - 1;j >= 0;--j)
	{
		add(treeArr[2], changed[j], 1);
		for (int i = 3;i <= k;++i)
			add(treeArr[i], changed[j], query(treeArr[i - 1], changed[j] - 1));
		sum = (sum + query(treeArr[k], changed[j] - 1)) % 1000000007;
	}
	cout << sum << '\n';
	cin.get();
	cin.get();
	return 0;
}

1547

#include <iostream>
#include <cstdio>
using namespace std;
 
int a[200000];
 
inline int read()
{
	int k = 0;
	char f = 1;
	char c = getchar();
	while (c<'0' || c>'9')
	{
		if (c == '-') f = -f;
		c = getchar();
	}
	while (c >= '0'&&c <= '9')
	{
		k = k * 10 + c - '0';
		c = getchar();
	}
	return f*k;
}
 
int mid(int a, int b, int c)
{
	if (a > b)
	{
		if (b > c) return b;
		else if (c > a) return a;
		else return c;
	}
	else
	{
		if (a > c)return a;
		else if (c > b) return b;
		else return c;
	}
}
 
void partition(int *arr, int begin, int end, int& newEnd, int& newBegin)
{
	int pivot = mid(arr[begin], arr[end], arr[(begin + end) / 2]);
	int pos = begin;
	while (pos <= end)
	{
		if(arr[pos] < pivot)
		{
			int tmp = arr[begin];
			arr[begin++] = arr[pos];
			arr[pos++] = tmp;
		}
		else if(arr[pos] > pivot)
		{
			int tmp = arr[end];
			arr[end--] = arr[pos];
			arr[pos] = tmp;
		}
		else ++pos;
	}
	newEnd = begin - 1;
	newBegin = end + 1;
}
 
void quick_sort(int* arr, int begin, int end)
{
	if (begin < end)
	{
		int newBegin, newEnd;
		partition(arr, begin, end, newEnd, newBegin);
		quick_sort(arr, begin, newEnd);
		quick_sort(arr, newBegin, end);
	}
}
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int t = read();
	for (int j = 0;j < t;++j)
	{
		int n = read();
		for (int i = 0;i < n;++i)
			a[i] = read();
		quick_sort(a, 0, n - 1);
		for (int i = 0;i < n;++i)
			cout << a[i] << ' ';
		cout << '\n';
	}
	cin.get();
	return 0;
}

1550

#include <iostream>
using namespace std;
 
int height[1001];
int leftH[1001] = { 0 };
 
int main()
{
	int n;
	cin >> n;
	char eat;
	for (int i = 1;i <= n;++i)
	{
		cin >> eat >> height[i];
		leftH[i] = (leftH[i - 1] < height[i] ? height[i] : leftH[i - 1]);
	}
	int rightH = height[n];
	int ans = 0;
	for (int i = n-1;i >= 1;--i)
	{
		int min = (leftH[i - 1] < rightH ? leftH[i - 1] : rightH);
		if (height[i] < min)
			ans += min - height[i];
		else
			rightH = height[i];
	}
	cout << ans << endl;
	cin.get();
	cin.get();
	cin.get();
	return 0;
}

1564

#include <iostream>
#include <cstring>
using namespace std;
 
int matrix[6][6];
int cost[5][6][6];
int ans = 0x7fffffff;
 
void dfs(int x1, int y1, int x2, int y2, int state)
{
	if (cost[state][x1][y1] >= ans)return;
	if (x1 == x2&&y1 == y2&&cost[state][x2][y2] < ans)
	{
		ans = cost[state][x2][y2];
		return;
	}
	if (x1 - 1 >= 0)
	{
		int newState = (matrix[x1 - 1][y1] * state) % 4 + 1;
		int tmp = cost[state][x1][y1] + matrix[x1 - 1][y1] * state;
		if (!cost[newState][x1 - 1][y1] || cost[newState][x1 - 1][y1] > tmp)
		{
			cost[newState][x1 - 1][y1] = tmp;
			dfs(x1 - 1, y1, x2, y2, newState);
		}
	}
	if (x1 + 1 < 6)
	{
		int newState = (matrix[x1 + 1][y1] * state) % 4 + 1;
		int tmp = cost[state][x1][y1] + matrix[x1 + 1][y1] * state;
		if (!cost[newState][x1 + 1][y1] || cost[newState][x1 + 1][y1] > tmp) 
		{
			cost[newState][x1 + 1][y1] = tmp;
			dfs(x1 + 1, y1, x2, y2, newState);
		}
	}
	if (y1 - 1 >= 0)
	{
		int newState = (matrix[x1][y1 - 1] * state) % 4 + 1;
		int tmp = cost[state][x1][y1] + matrix[x1][y1 - 1] * state;
		if (!cost[newState][x1][y1 - 1] || cost[newState][x1][y1 - 1] > tmp)
		{
			cost[newState][x1][y1 - 1] = tmp;
			dfs(x1, y1 - 1, x2, y2, newState);
		}
	}
	if (y1 + 1 < 6)
	{
		int newState = (matrix[x1][y1 + 1] * state) % 4 + 1;
		int tmp = cost[state][x1][y1] + matrix[x1][y1 + 1] * state;
		if (!cost[newState][x1][y1 + 1] || cost[newState][x1][y1 + 1] > tmp)
		{
			cost[newState][x1][y1 + 1] = tmp;
			dfs(x1, y1 + 1, x2, y2, newState);
		}
	}
}
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin >> n;
	for (int i = 0;i < n;++i)
	{
		for (int j = 0;j < 6;++j)
			for (int k = 0;k < 6;++k)
				cin >> matrix[j][k];
		int x1, x2, y1, y2;
		cin >> x1 >> y1 >> x2 >> y2;
		memset(cost, 0, sizeof(cost));
		dfs(x1, y1, x2, y2, 1);
		cout << ans << '\n';
	}
	cin.get();
	cin.get();
	return 0;
}

1569

#include <iostream>
using namespace std;
 
int N;
 
void add(int a[], int i, int p)
{
	for (int j = i;j <= N;j += j&(-j))a[j] += p;
}
 
int query(int a[], int r)
{
	int sum = 0;
	for (int i = r;i > 0;i -= i&(-i)) sum += a[i];
	return sum;
}
 
int main()
{
	cin >> N;
	int data, *a = new int[N+1]();
	for (int i = 0;i < N;++i)
	{
		cin >> data;
		add(a, i + 1, data);
	}
	char command[6];
	cin >> command;
	while (command[0] != 'e')
	{
		int x, y;
		switch (command[0])
		{
		case 'i':
			cin >> x >> y;
			add(a, x, y);
			break;
		case 'd':
			cin >> x >> y;
			add(a, x, -y);
			break;
		case 'q':
			cin >> x >> y;
			cout << query(a, y) - query(a, x - 1) << endl;
		}
		cin >> command;
	}
	delete[]a;
	cin.get();
	cin.get();
	return 0;
}

1570

#include <iostream>
using namespace std;
 
int arr[100005];
 
int solve(int num, int sz)
{
	int l = 0, r = sz - 1;
	while (l <= r)
	{
		int mid = l + r >> 1;
		if (arr[mid] <= num)l = mid + 1;
		else r = mid - 1;
	}
	return sz - l;
}
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int N, T;
	cin >> N >> T;
	for (int i = 0;i < N;++i) cin >> arr[i];
	int num;
	for (int i = 0;i < T;++i)
	{
		cin >> num;
		cout << solve(num, N) << '\n';
	}
	cin.get();
	cin.get();
	return 0;
}

1571

#include <iostream>
using namespace std;
int N;
 
struct node
{
	int origin, index;
};
 
void add(int arr[], int x, int y)
{
	for (int i = x;i <= N;i += i&(-i))arr[i] += y;
}
 
long long query(int arr[], int x)
{
	long long sum = 0;
	for (int i = x;i > 0;i -= i&(-i))sum += arr[i];
	return sum;
}
 
void insert_sort(node arr[], int start, int end)
{
	for (int i = start + 1;i <= end;++i)
	{
		if (arr[i].origin < arr[i - 1].origin)
		{
			node tmp = arr[i];
			int j;
			for (j = 0;arr[j].origin <= tmp.origin;++j);
			for (int k = i;k > j;--k)arr[k] = arr[k - 1];
			arr[j] = tmp;
		}
	}
}
 
int partition(node arr[], int start, int end)
{
	int pivot = arr[start].origin;
	node tmp = arr[start];
	while (start < end)
	{
		while (start < end)
		{
			if (arr[end].origin >= pivot)--end;
			else break;
		}
		arr[start] = arr[end];
		while (start < end)
		{
			if (arr[start].origin <= pivot)++start;
			else break;
		}
		arr[end] = arr[start];
	}
	arr[start] = tmp;
	return start;
}
 
void quick_sort(node arr[], int start, int end)
{
	if (end - start + 1 <= 3)
	{
		insert_sort(arr, start, end);
		return;
	}
	int pos = partition(arr, start, end);
	quick_sort(arr, start, pos);
	quick_sort(arr, pos + 1, end);
}
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> N;
	node *a = new node[N],*b=new node[N];
	for (int i = 0;i < N;++i)
	{
		cin >> a[i].origin;
		a[i].index = i;
	}
	quick_sort(a,0,N-1);
	for (int i = 0;i < N;++i)
	{
		cin >> b[i].origin;
		b[i].index = i;
	}
	quick_sort(b, 0, N - 1);
	int *rightPos = new int[N];
	for (int i = 0;i < N;++i)
	{
		rightPos[b[i].index] = a[i].index+1;
	}
	long long ans = 0;
	int *arr = new int[N + 1]();
	for (int i = N - 1;i >= 0;--i)
	{
		add(arr, rightPos[i], 1);
		ans += query(arr, rightPos[i] - 1);
	}
	ans = ans % 99999997;
	cout << ans << '\n';
	cin.get();
	cin.get();
	delete[]a;
	delete[]b;
	delete[]rightPos;
	delete[]arr;
	return 0;
}

1572

#include <iostream>
using namespace std;
int N;
 
void add(int arr[], int x, int y)
{
	for (int i = x;i <= N;i += i&(-i))arr[i] += y;
}
 
long long query(int arr[], int x)
{
	long long sum = 0;
	for (int i = x;i > 0;i -= i&(-i))sum += arr[i];
	return sum;
}
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> N;
	int *a = new int[N + 1](),*data=new int[N];
	long long ans = 0;
	for (int i = 0;i < N;++i)
		cin >> data[i];
	for (int i = N - 1;i>=0;--i)
	{
		add(a, data[i], 1);
		ans += query(a,data[i] - 1);
	}
	cout << ans << '\n';
	cin.get();
	cin.get();
	delete[]a;
	delete[]data;
	return 0;
}

1573

#include <iostream>
#include <cstring>
using namespace std;
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int m, n;
	cin >> n >> m;
	int result=0;
	int num[100000];
	int color[100000];
	int a[100001] = { 0 }, b[100001] = { 0 }, c[100001] = { 0 }, d[100001] = { 0 };
	int i;
	for (i = 0;i < n;++i)
	{
		cin >> num[i];
		num[i] = num[i] % 10007;
	}
	for (i = 0;i < n;++i)
	{
		cin >> color[i];
	}
	for (i = 0;i < n;i += 2)
	{
		a[color[i]] = (a[color[i]] + i + 1)%10007;
		b[color[i]] = (b[color[i]] + num[i])%10007;
		c[color[i]] = (c[color[i]]+(i + 1)*num[i])%10007;
		++d[color[i]];
	}
	for (i = 1;i <= m;++i)
		if (d[i]>1)
			result = (result + (a[i] * b[i])%10007 + (c[i] * (d[i] - 2))%10007) % 10007;
	memset(a, 0, sizeof(a));
	memset(b, 0, sizeof(b));
	memset(c, 0, sizeof(c));
	memset(d, 0, sizeof(d));
	for (i = 1;i < n;i += 2)
	{
		a[color[i]] = (a[color[i]] + i + 1) % 10007;
		b[color[i]] = (b[color[i]] + num[i]) % 10007;
		c[color[i]] = (c[color[i]] + (i + 1)*num[i]) % 10007;
		++d[color[i]];
	}
	for (i = 1;i <= m;++i)
		if(d[i]>1)
			result = (result + (a[i] * b[i]) % 10007 + (c[i] * (d[i] - 2)) % 10007) % 10007;
	cout << result << '\n';
	cin.get();
	cin.get();
	return 0;
}

1574

#include <iostream>
using namespace std;
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n, m, k, x;
	cin >> n >> m >> k >> x;
	int result = 1;
	int base = 10;
	while (k > 0)
	{
		if (k % 2 == 1)result = (result * base) % n;
		base = (base*base) % n;
		k /= 2;
	}
	result = ((result*m) % n + x) % n;
	cout << result << '\n';
	cin.get();
	cin.get();
	return 0;
}

1575

#include <iostream>
using namespace std;
 
int L, N, M;
int *arr;
 
int leastM(int n)
{
	if (n == 0)return 0;//防止超界。否则下面会用到arr[N+2];
	int m = 0,i=0;
	int start = 0;
	while (start <= L - n)
	{
		++i;
		if (arr[i] - start < n)++m;
		else start = arr[i];
	}
	m += N + 1 - i;
	return m;
}
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> L >> N >> M;
	arr = new int[N+2];
	for (int i = 1;i < N+1;++i)cin >> arr[i];
	arr[0] = 0;
	arr[N + 1] = L;
	int l = 0, r = L;
	while (l <= r)
	{
		if (leastM(l) > M)break;//使l为第一个满足了leastM(i)>M的i。(或者无这种情况,总之为leastM(l)==M的下一个i)
		int mid = (l + r) / 2;
		if (leastM(mid) <= M)l = mid + 1;
		else r = mid;
	}
	int ans = l - 1;
	cout << ans << '\n';
	cin.get();
	cin.get();
	delete[]arr;
	return 0;
}

1579

#include <iostream>
using namespace std;
 
int f[1001] = { 0 };
 
int main()
{
	char str1[1001], str2[1001];
	int n, m;
	cin >> n >> m >> str1 >> str2;
	for (int i = 1;i <= n;++i)
	{
		int bak = f[0];
		for (int j = 1;j <= m;++j)
		{
			if (str1[i - 1] == str2[j - 1])
			{
				int tmp = bak + 1;
				bak = f[j];
				f[j] = tmp;
			}
			else
			{
				bak = f[j];
				f[j] = (f[j - 1] > f[j] ? f[j - 1] : f[j]);
			}
		}
	}
	cout << f[m] << '\n';
	cin.get();
	cin.get();
	return 0;
}

1580

#include <iostream>
using namespace std;
 
int minLEnd[1000001];
 
int search(int len,int num)
{
	int start = 1, end = len;
	while (start <= end)
	{
		if (minLEnd[start] >= num)break;
		int mid = (start + end) / 2;
		if (minLEnd[mid] < num)start = mid + 1;
		else end = mid;
	}
	return start;
}
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin >> n;
	int len = 0;
	int num;
	for (int i = 0;i < n;++i)
	{
		cin >> num;
		int pos = search(len, num);
		minLEnd[pos] = num;
		if (pos > len)++len;
	}
	cout << len << '\n';
	cin.get();
	cin.get();
	return 0;
}
#include <iostream>
using namespace std;
 
int minLEnd[1000001];
 
int search(int len,int num)
{
	int start = 1, end = len;
	while (start <= end)
	{
		if (minLEnd[start] >= num)break;
		int mid = (start + end) / 2;
		if (minLEnd[mid] < num)start = mid + 1;
		else end = mid;
	}
	return start;
}
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin >> n;
	int len = 0;
	int num;
	for (int i = 0;i < n;++i)
	{
		cin >> num;
		int pos = search(len, num);
		minLEnd[pos] = num;
		if (pos > len)++len;
	}
	cout << len << '\n';
	cin.get();
	cin.get();
	return 0;
}

1588

#include <iostream>
using namespace std;
 
int decrease[1000010], increase[1000010];
 
int searchD(int len, int x)
{
	int begin = 1, end = len;
	while (begin <= end)
	{
		if (decrease[begin] < x)break;
		int mid = (begin + end) >> 1;
		if (decrease[mid] >= x)begin = mid + 1;
		else end = mid;
	}
	return begin;
}
 
int searchI(int len, int x)
{
	int begin = 1, end = len;
	while (begin <= end)
	{
		if (increase[begin] >= x)break;
		int mid = (begin + end) >> 1;
		if (increase[mid] < x)begin = mid + 1;
		else end = mid;
	}
	return begin;
}
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int N;
	cin >> N;
	int num;
	int maxN = 0, minNeed = 0;
	for (int i = 0;i < N;++i)
	{
		cin >> num;
		int pos1 = searchD(maxN, num);
		int pos2 = searchI(minNeed, num);
		decrease[pos1] = increase[pos2] = num;
		if (pos1 > maxN)++maxN;
		if (pos2 > minNeed)++minNeed;
	}
	cout << maxN << '\n';
	cout << minNeed << '\n';
	cin.get();
	cin.get();
	return 0;
}

1634

#include <cstdio>
using namespace std;
 
const int MAXN = 1000000;
 
class Heap {
private:
	int	value[MAXN << 1];
	int size;
	int count;
 
public:
	Heap() : size(0), count(0) {}
 
	void insert(int val) {
		int hole = ++size;
		for (; hole > 1 && (++count) && val < value[hole/2]; hole /= 2) {
			value[hole] = value[hole/2];
		}
		value[hole] = val;
	}
 
	int pop() {
		int top = value[1], hole, child, tmp = value[size--];
		for (hole = 1; hole*2 <= size; hole = child) {
			child = hole *2;
			if (child < size && (++count) && value[child ^ 1] < value[child]) child ^= 1;//奇数-1,偶数+1,保证value[child ^ 1] 、 value[child]都是value[hole]的左右儿子
			if ((++count) && value[child] < tmp) value[hole] = value[child]; else break;
		}
		value[hole] = tmp;
		return top;
	}
 
	int heap_sort(int* A, int n) {
		for (int i = 0; i < n; i++) insert(A[i]);
		for (int i = 0; i < n; i++) A[i] = pop();
		return count;
	}
} heap;
 
class Merge {
private:
	int count;
	int T[MAXN];
 
public:
	Merge() : count(0) {}
 
	void merge(int* A, int L, int R) {
		if (R - L < 2) return;
		int M = (L + R) >> 1;
		merge(A, L, M); merge(A, M, R);
		int i = L, j = M, k = L;
		while (i < M || j < R) {
			if (j == R || (i < M && (++count) && A[i] < A[j])) T[k++] = A[i++];
			else T[k++] = A[j++];
		}
		for (k = L; k < R; k++) A[k] = T[k];
	}
 
	int merge_sort(int* A, int n) {
		merge(A, 0, n);
		return count;
	}
} merge;
 
class Qsort {
private:
	int count;
 
public:
	Qsort() : count(0) {}
 
	void swap(int &x, int &y) { int z = x; x = y; y = z; }
 
	long long quick_sort(int* A, int n) {
		long long ans = 0;
		static int pre[MAXN], nxt[MAXN];
		for (int i = 1; i <= n; i++)
			pre[i] = i - 1, nxt[i] = i + 1;
 
		for (int i = n; i >= 1; i--) {
			int x = A[i];
			ans += nxt[x] - pre[x] - 1;
			nxt[pre[x]] = nxt[x];
			pre[nxt[x]] = pre[x];
		}
		return ans - n;
	}
} q_sort;
 
int A[MAXN];
 
int main() {
	int n, k;
	scanf("%d%d", &n, &k);
	for (int i = 1; i <= n; i++) scanf("%d", &A[i]);
 
	switch (k) {
	case 1: printf("%d\n", heap.heap_sort(A + 1, n)); break;
	case 2: printf("%d\n", merge.merge_sort(A + 1, n)); break;
	case 3: printf("%lld\n", q_sort.quick_sort(A, n)); break;
	}
	return 0;
}

1990

#include <iostream>
using namespace std;
 
struct node
{
	int i, love;
 
	node() {}
	node(int x, int y) :i(x), love(y) {}
};
 
int pos[2000001] = { 0 };
node dat[2000001];
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n, m;
	cin >> n >> m;
	for (int i = 1;i <= n;++i)
	{
		cin >> dat[i].love;
		pos[i] = dat[i].i = i;
	}
	int x, y;
	for (int i = 0;i < m;++i)
	{
		cin >> x >> y;
		dat[pos[x]].love += y;
		int l, r;
		if (y > 0)
		{
			l = pos[x]+1;
			r = n;
		}
		else
		{
			l = 1;
			r = pos[x]-1;
		}
		while (l <= r)
		{
			int mid = l + r >> 1;
			if (dat[mid].love < dat[pos[x]].love)l = mid + 1;
			else r = mid - 1;
		}
		if (r + 1 < pos[x])
		{
			node tmp = dat[pos[x]];
			for (int j = pos[x];j > r + 1;--j)
			{
				dat[j] = dat[j - 1];
				pos[dat[j].i] = j;
			}
			dat[r + 1] = tmp;
			pos[tmp.i] = r + 1;
		}
		else if (r > pos[x])
		{
			node tmp = dat[pos[x]];
			for (int j = pos[x];j < r;++j)
			{
				dat[j] = dat[j + 1];
				pos[dat[j].i] = j;
			}
			dat[r] = tmp;
			pos[tmp.i] = r;
		}
		cout << dat[1].i << '\n';
	}
	cin.get();
	cin.get();
	return 0;
}

1994

#include <iostream>
using namespace std;
 
int n, m;
bool map[500][500]={ false };
int ans = 0;
 
void dfs(int x,int y)
{
	map[x][y] = false;
	if (x > 0 && map[x - 1][y])dfs(x - 1, y);
	if (x < n - 1 && map[x + 1][y])dfs(x + 1, y);
	if (y > 0 && map[x][y - 1])dfs(x, y - 1);
	if (y < n - 1 && map[x][y + 1])dfs(x, y + 1);
}
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> n >> m;
	int tmp;
	for(int i=0;i<n;++i)
		for (int j = 0;j < m;++j)
		{
			cin >> tmp;
			if (!tmp)map[i][j] = true;
		}
	for(int i=0;i<n;++i)
		for (int j = 0;j < m;++j)
		{
			if (map[i][j])
			{
				++ans;
				dfs(i, j);
			}
		}
	cout << ans << '\n';
	cin.get();
	cin.get();
	return 0;
}

1998

#include <iostream>
#include <cmath>
using namespace std;
 
int main()
{
	int a[10001] = { 0 }, x, N;
	int i;
	cin >> N;
	for (i = 0;i < N;++i)
	{
		cin >> x;
		++a[x];
	}
	i = 0;
	int j;
	for (j = 0;j < 10001;++j)
	{
		if (a[j] > 0) i += a[j];
		if (i >= (N +1)/ 2) break;
	}
	int result = 0;
	for (i = 0;i < 10001;++i)
	{
		if (a[i] > 0) result += abs(j  - i)*a[i];
	}
	cout << result << endl;
	cin.get();
	cin.get();
	return 0;
}

3008


#include <iostream>
using namespace std;
 
struct node
{
	int x, y;
	node() {}
	node(int a, int b) :x(a), y(b) {}
};
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int dist[100][100];
	char map[100][100];
	int n, m;
	int x1, y1, x2, y2;
	node q[10000];
	int front = 0;
	int back = -1;
	cin >> n >> m >> x1 >> y1 >> x2 >> y2;
	for(int i=0;i<n;++i)
		for (int j = 0;j < m;++j)
		{
			cin >> map[i][j];
			dist[i][j] = 0x7fffffff;
		}
	q[++back] = node(x1-1, y1-1);
	dist[x1-1][y1-1] = 0;
	while (front <= back)
	{
		node current = q[front++];
		int x = current.x, y = current.y;
		int curDist = dist[x][y];
		switch (map[x][y])
		{
		case '.':
			if (x > 0 && dist[x - 1][y] > curDist + 1&&(map[x-1][y]=='|'||map[x-1][y]=='.'))
			{
				q[++back] = node(x - 1, y);
				dist[x - 1][y] = curDist + 1;
			}
			if (x < n - 1 && dist[x + 1][y]>curDist + 1&&(map[x+1][y]=='|'||map[x+1][y]=='.'))
			{
				q[++back] = node(x + 1, y);
				dist[x + 1][y] = curDist + 1;
			}
			if (y > 0 && dist[x][y - 1] > curDist + 1&&(map[x][y-1]=='-'||map[x][y-1]=='.'))
			{
				q[++back] = node(x, y - 1);
				dist[x][y - 1] = curDist + 1;
			}
			if (y<m - 1 && dist[x][y + 1]>curDist + 1 && (map[x][y + 1] == '-' || map[x][y + 1] == '.'))
			{
				q[++back] = node(x, y + 1);
				dist[x][y + 1] = curDist + 1;
			}
			break;
		case '|':
			if (x > 0 && dist[x - 1][y] > curDist + 1 && (map[x - 1][y] == '|' || map[x - 1][y] == '.'))
			{
				q[++back] = node(x - 1, y);
				dist[x - 1][y] = curDist + 1;
			}
			if (x < n - 1 && dist[x + 1][y]>curDist + 1 && (map[x + 1][y] == '|' || map[x + 1][y] == '.'))
			{
				q[++back] = node(x + 1, y);
				dist[x + 1][y] = curDist + 1;
			}
			break;
		case '-':
			if (y > 0 && dist[x][y - 1] > curDist + 1 && (map[x][y - 1] == '-' || map[x][y - 1] == '.'))
			{
				q[++back] = node(x, y - 1);
				dist[x][y - 1] = curDist + 1;
			}
			if (y<m - 1 && dist[x][y + 1]>curDist + 1 && (map[x][y + 1] == '-' || map[x][y + 1] == '.'))
			{
				q[++back] = node(x, y + 1);
				dist[x][y + 1] = curDist + 1;
			}
			break;
		}
		if (x+1 == x2&&y+1 == y2)break;
	}
	if (dist[x2 - 1][y2 - 1] == 0x7fffffff)cout << -1;
	else cout << dist[x2 - 1][y2 - 1];
	cout << '\n';
	cin.get();
	cin.get();
	return 0;
}

3022

#include <iostream>
#include <vector>
using namespace std;
//动态规划。
 
class hugeInt
{
	friend ostream& operator<<(ostream& os, const hugeInt& hugeNum)
	{
		for (int i = hugeNum.num.size() - 1;i >= 0;--i)os << hugeNum.num[i];
		return os;
	}
	friend hugeInt operator+(const hugeInt& num1, const hugeInt& num2)
	{
		hugeInt tmp;
		int i = 0;
		while (i < num1.num.size() && i < num2.num.size())
		{
			tmp.num.push_back(num1.num[i] + num2.num[i]);
			++i;
		}
		while (i < num1.num.size())
			tmp.num.push_back(num1.num[i++]);
		while (i < num2.num.size())
			tmp.num.push_back(num2.num[i++]);
		for (i = 0;i < tmp.num.size() - 1;++i)
		{
			tmp.num[i + 1] += tmp.num[i] / 10;
			tmp.num[i] = tmp.num[i] % 10;
		}
		if (tmp.num[i] > 10)
		{
			tmp.num.push_back(1);
			tmp.num[i] -= 10;
		}
		return tmp;
	}
private:
	vector<int> num;
public:
	hugeInt() {};
	hugeInt(int n)
	{
		while (n != 0)
		{
			num.push_back(n % 10);
			n /= 10;
		}
	}
};
 
int main()
{
	hugeInt state[10005];
	int n;
	cin >> n;
	state[1] = 1;
	state[2] = 2;
	state[3] = 4;
	for (int i = 4;i <= n+1;++i)
		state[i] = state[i - 1] + state[i - 2] + state[i - 3];
	cout << state[n+1] << endl;
	cin.get();
	cin.get();
	return 0;
}

3032

#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
 
class hugeInt
{
	friend hugeInt operator+(const hugeInt&n1, const hugeInt&n2)
	{
		hugeInt tmp;
		int i = 0;
		int carry = 0;
		while (i < n1.num.size() && i < n2.num.size())
		{
			tmp.num.push_back((n1.num[i] + n2.num[i] + carry) % 10);
			carry = (n1.num[i] + n2.num[i] + carry) / 10;
			++i;
		}
		while (i < n1.num.size())
		{
			tmp.num.push_back((n1.num[i] + carry) % 10);
			carry = (n1.num[i] + carry) / 10;
			++i;
		}
		while (i < n2.num.size())
		{
			tmp.num.push_back((n2.num[i] + carry) % 10);
			carry = (n2.num[i] + carry) / 10;
			++i;
		}
		if (carry == 1)tmp.num.push_back(1);
		return tmp;
	}
	friend hugeInt operator-(const hugeInt& n1, const hugeInt& n2)//只能n1>n2;
	{
		hugeInt tmp;
		int i;
		for (i = 0;i < n1.num.size() && i < n2.num.size();++i)
			tmp.num.push_back(n1.num[i] - n2.num[i]);
		for (;i < n1.num.size();++i)
			tmp.num.push_back(n1.num[i]);
		for(i=0;i<tmp.num.size();++i)
			if (tmp.num[i] < 0)
			{
				tmp.num[i] += 10;
				--tmp.num[i + 1];
			}
		return tmp;
	}
	friend ostream& operator<<(ostream& os, const hugeInt& n)
	{
		int i;
		for (i = n.num.size() - 1;i >= 1 && n.num[i] == 0;--i);
		for (;i >= 0;--i)
			os << n.num[i];
		return os;
	}
	friend istream& operator >> (istream& is, hugeInt& n)
	{
		n.num.clear();
		char str[1001];
		is >> str;
		for (int i = strlen(str) - 1;i >= 0;--i)
			n.num.push_back(str[i] - '0');
		return is;
	}
	friend bool operator==(const hugeInt&n1, const hugeInt& n2)
	{
		if (n1.num.size() != n2.num.size())return false;
		for (int i = 0;i < n1.num.size();++i)
		{
			if (n1.num[i] != n2.num[i])return false;
		}
		return true;
	}
	friend bool operator>(const hugeInt&n1, const hugeInt& n2)
	{
		if (n1.num.size() > n2.num.size())return true;
		else if (n1.num.size() < n2.num.size()) return false;
		for (int i = n1.num.size()-1;i >=0;--i)
		{
			if (n1.num[i] > n2.num[i])return true;
			else if (n1.num[i] < n2.num[i]) return false;
		}
		return false;
	}
private:
	vector<int> num;
public:
	hugeInt() {}
	hugeInt(long long n)
	{
		do
		{
			num.push_back(n % 10);
			n = n / 10;
		} while (n != 0);
	}
	void twoTimes()
	{
		*this = *this + *this;
	}
};
 
int main()
{
	hugeInt n, result;
	cin >> n;
	hugeInt tmp1(1), tmp2;
	while (!(tmp1 > n))
	{
		tmp2 = tmp1;
		tmp1.twoTimes();
	}
	if (tmp2 == n) result = hugeInt(1);
	else
	{
		result = n - tmp2;
		result.twoTimes();
		result = result + hugeInt(1);
	}
	cout << result << endl;
	cin.get();
	cin.get();
	return 0;
}

3034

#include <iostream>
#include <cmath>
using namespace std;
 
bool isPrime(int n)
{
	if (n <= 1)return false;
	for (int i = 2;i <= sqrt(n);++i)
		if (n%i == 0) return false;
	return true;
}
 
int main()
{
	int N;
	cin >> N;
	int i, j;
	for (i = N;!isPrime(i);--i);
	for (j = N;!isPrime(j);++j);
	cout << j - i << endl;
	cin.get();
	cin.get();
	return 0;
}

4003

#include <iostream>
using namespace std;
 
int main()
{
	int n, k;
	cin >> n >> k;
	int ans = 0;
	if(k<=n)
	{
		int c = 0;
		int ncpy = n;
		for (;ncpy;++c) ncpy -= ncpy&(-ncpy);
		while (k < c)
		{
			int tmp = n&(-n);
			ans += tmp;
			n += tmp;
			ncpy = n;
			c = 0;
			for (;ncpy;++c)ncpy -= ncpy&(-ncpy);
		}
	}
	cout << ans << endl;
	cin.get();
	cin.get();
	return 0;
}

4011

#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
 
class hugeInt
{
	friend hugeInt operator+(const hugeInt& n1, const hugeInt& n2)
	{
		hugeInt tmp;
		tmp.num.clear();
		int i = 0;
		for (;i < n1.num.size() && i < n2.num.size();++i)
			tmp.num.push_back(n1.num[i] + n2.num[i]);
		for (;i < n1.num.size();++i)
			tmp.num.push_back(n1.num[i]);
		for (;i < n2.num.size();++i)
			tmp.num.push_back(n2.num[i]);
		for(i=0;i<tmp.num.size()-1;++i)
			if (tmp.num[i] >= 10000)
			{
				tmp.num[i + 1] += tmp.num[i] / 10000;
				tmp.num[i] %= 10000;
			}
		if (tmp.num[i] >= 10000)
		{
			tmp.num.push_back(tmp.num[i] / 10000);
			tmp.num[i] %= 10000;
		}
		return tmp;
	}
	friend ostream& operator<<(ostream& os, const hugeInt& n)
	{
		os << n.num[n.num.size() - 1];
		os << setfill('0');//注意这里。
		for (int i = n.num.size() - 2;i >= 0;--i) os <<setw(4)<< n.num[i];
		return os;
	}
private:
	vector<int> num;
public:
	hugeInt() { num.push_back(0); }
	hugeInt(long long n)
	{
		while (n >= 10000)
		{
			num.push_back(n % 10000);
			n /= 10000;
		}
		num.push_back(n);
	}
	hugeInt& operator=(const hugeInt& n)
	{
		if (&n == this)return *this;
		num.clear();
		for (int i = 0;i < n.num.size();++i)
			num.push_back(n.num[i]);
		return *this;
	}
};
 
hugeInt level[51][51];
 
int main()
{
	int k, h;
	cin >> k >> h;
	level[1][0] = 1;
	for (int i = 2;i <= h;++i)
	{
		for (int j = k - 1;j >= 0;--j)
			level[i][j] = level[i][j + 1] + level[i - 1][k - 1 - j];
	}
	hugeInt ans = 0;
	for (int j = 0;j < k;++j)ans = ans + level[h][j];
	cout << ans << endl;
	cin.get();
	cin.get();
	return 0;
}

4012

#include <iostream>
using namespace std;
 
class heap
{
private:
	int *data;
	int size;
	int maxSize;
 
	void doubleSpace()
	{
		int *tmp = new int[maxSize * 2 + 1];
		for (int i = 1;i <= maxSize;++i) tmp[i] = data[i];
		maxSize *= 2;
		delete[]data;
		data = tmp;
	}
public:
	heap(int capacity = 10) :maxSize(capacity),size(0) { data = new int[maxSize+1]; }
	~heap() { delete[]data; }
	void insert(int n)
	{
		if (size == maxSize) doubleSpace();
		int hole = ++size;
		for (;hole > 1 && data[hole / 2] > n;hole /= 2)
			data[hole] = data[hole / 2];
		data[hole] = n;
	}
	int pop()
	{
		int child;
		if (size == 0) return 1 << 31;
		int min = data[1];
		int tmp = data[size--];
		int hole = 1;
		for (;hole * 2 <= size;hole = child)
		{
			child = hole * 2;
			if (child + 1 <= size&&data[child + 1] < data[child])++child;
			if (data[child] < tmp) data[hole] = data[child];
			else break;//这个地方不break就无法正确插入。
		}
		data[hole] = tmp;
		return min;
	}
	bool empty() const { return size == 0; }
	int cnt() const { return size; }
};
 
int main()
{
	int n, num;
	cin >> n;
	heap h(n);
	for (int i = 0;i < n;++i)
	{
		cin >> num;
		h.insert(num);
	}
	int sum = 0;
	int x, y;
	while (h.cnt() > 1)
	{
		x = h.pop();
		y = h.pop();
		h.insert(x + y);
		sum += x + y;
	}
	cout << sum << '\n';
	cin.get();
	cin.get();
	return 0;
}

4015

#include <iostream>
using namespace std;
 
int main()
{
	int a = 1, b = 1, c;
	int arr[7][7] = { 0 };
	arr[1][1] = 1;
	int A, B, n;
	cin >> A >> B >> n;
	int mark1, mark2;
	int i = 1;
	while (true)
	{
		c = (A*b + B*a) % 7;
		a = b;
		b = c;
		++i;
		if (arr[a][b])
		{
			mark1 = arr[a][b];
			mark2 = i;
			break;
		}
		else arr[a][b] = i;
	}
	if (n > mark2)
		n = (n - mark1) % (mark2 - mark1) + mark1;
	for (i = 2;i < n;++i)
	{
		c = (A*b + B*a) % 7;
		a = b;
		b = c;
	}
	cout << b << endl;
	cin.get();
	cin.get();
	return 0;
}

4021

#include <iostream>
using namespace std;
 
int minMAll[100001] = { 0 };
int minMSingle[100001] = { 0 };
int single[100];
int singleM[100];
int all[100];
int allM[100];
int hp[100];
int cnt1 = 0, cnt2 = 0;
 
int main()
{
	int N;
	cin >> N;
	int maxHp = 0;
	for (int i = 0;i < N;++i)
	{
		cin >> hp[i];
		if (hp[i] > maxHp)maxHp = hp[i];
	}
	int M;
	cin >> M;
	char str[31], type[10];
	int damage, mp;
	for (int i = 0;i < M;++i)
	{
		cin >> str >> mp >> type >> damage;
		if (type[0] == 'S')
		{
			single[cnt1] = damage;
			singleM[cnt1++] = mp;
		}
		else
		{
			all[cnt2] = damage;
			allM[cnt2++] = mp;
		}
	}
	for (int i = 1;i < maxHp + 1;++i) minMAll[i] = minMSingle[i] = 0x7fffffff;
	for (int i = 0;i < cnt1;++i)
	{
		int tmp;
		for (int j = 1;j <= maxHp;++j)
		{
			if (single[i] == 0)break;
			if (j - single[i] <= 0) tmp = singleM[i];
			else tmp = minMSingle[j - single[i]] + singleM[i];
			if (tmp < minMSingle[j]) minMSingle[j] = tmp;
		}
	}
	for (int i = 0;i < cnt2;++i)
	{
		int tmp;
		for (int j = 1;j <= maxHp;++j)
		{
			if (all[i] == 0) break;
			if (j - all[i] <= 0) tmp = allM[i];
			else tmp = minMAll[j - all[i]] + allM[i];
			if (tmp < minMAll[j]) minMAll[j] = tmp;
		}
	}
	int result = 0x7fffffff;
	for (int i = 0;i <= maxHp;++i)
	{
		long long sumMp = minMAll[i];						//防止该情况不能击杀的情况发生时的超出整型。
		for (int j = 0;j < N;++j)
			if (hp[j] - i>0)sumMp += minMSingle[hp[j] - i];
		if (sumMp < result) result = sumMp;
	}
	cout << result << '\n';
	cin.get();
	cin.get();
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值