A.简单题
/*************************************************************************
> File Name: A.cpp
> Created Time: 2015年05月20日 星期三 09时51分15秒
************************************************************************/
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;
int mat[110][110];
bool ok[110];
int main() {
int n;
while (cin >> n) {
for (int i = 1; i <= n; ++i) {
ok[i] = 1;
for (int j = 1; j <= n; ++j) {
cin >> mat[i][j];
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (mat[i][j] == -1 || mat[i][j] == 0) {
continue;
}
if (mat[i][j] == 1) {
ok[i] = 0;
}
else if (mat[i][j] == 2) {
ok[j] = 0;
}
else {
ok[i] = ok[j] = 0;
}
}
}
vector <int> ans;
ans.clear();
for (int i = 1; i <= n; ++i) {
if (ok[i]) {
ans.push_back(i);
}
}
int size = ans.size();
printf("%d\n", size);
if (size == 0) {
continue;
}
printf("%d", ans[0]);
for (int i = 1; i < size; ++i) {
printf(" %d", ans[i]);
}
printf("\n");
}
return 0;
}
B.简单题
/*************************************************************************
> File Name: B.cpp
> Author: ALex
> Mail: zchao1995@gmail.com
> Created Time: 2015年05月20日 星期三 09时57分00秒
************************************************************************/
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;
string s, t;
int main() {
while (cin >> s >> t) {
int len = s.length();
int cnt = 0;
for (int i = 0; i < len; ++i) {
if (s[i] != t[i]) {
++cnt;
}
}
if (cnt & 1) {
printf("impossible\n");
}
else {
bool flag = 0;
for (int i = 0; i < len; ++i) {
if (s[i] == t[i]) {
printf("%c", s[i]);
}
else {
if (!flag) {
printf("%c", s[i]);
}
else {
printf("%c", t[i]);
}
flag ^= 1;
}
}
printf("\n");
}
}
return 0;
}
C.简单dp, dp[i][0/1/2]表示不砍第i棵树;砍,往左边倒;砍,往右倒
/*************************************************************************
> File Name: C.cpp
> Author: ALex
> Mail: zchao1995@gmail.com
> Created Time: 2015年05月20日 星期三 10时03分06秒
************************************************************************/
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;
PLL sta[100100];
int dp[100100][3];
int main() {
int n;
while (~scanf("%d", &n)) {
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; ++i) {
scanf("%d%d", &sta[i].first, &sta[i].second);
}
dp[1][1] = 1;
if (sta[1].first + sta[1].second < sta[2].first) {
dp[1][2] = 1;
}
for (int i = 2; i <= n; ++i) {
dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]);
dp[i][0] = max(dp[i - 1][0], dp[i][0]);
if (sta[i].first - sta[i].second > sta[i - 1].first) {
dp[i][1] = max(dp[i][1], max(dp[i - 1][1], dp[i - 1][0]) + 1);
}
if (sta[i].first - sta[i].second > sta[i - 1].first + sta[i - 1].second) {
dp[i][1] = max(dp[i][1], dp[i - 1][2] + 1);
}
if (i < n && sta[i].first + sta[i].second < sta[i + 1].first || i == n) {
dp[i][2] = max(dp[i][2], max(dp[i - 1][1], dp[i - 1][0]) + 1);
if (sta[i - 1].first + sta[i - 1].second < sta[i].first) {
dp[i][2] = max(dp[i][2], dp[i - 1][2] + 1);
}
}
}
printf("%d\n", max(dp[n][0], max(dp[n][1], dp[n][2])));
}
return 0;
}
D.排序贪心就行
/*************************************************************************
> File Name: D.cpp
> Author: ALex
> Mail: zchao1995@gmail.com
> Created Time: 2015年05月20日 星期三 10时16分38秒
************************************************************************/
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;
int arr[100010];
int main() {
int n;
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; ++i) {
scanf("%d", &arr[i]);
}
sort(arr + 1, arr + n + 1);
int sum = 0;
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (sum <= arr[i]) {
++ans;
sum += arr[i];
}
}
printf("%d\n", ans);
}
return 0;
}
E.dijkstra算法的实现过程就是最小路径树的寻找过程
每次加入一个点放到当前求出最短路的集合里
考虑给每个点求一个最短的可以达到它的边,最短路相同时去边权小的边, 求的时候注意用long long ,一个地方没改wa了好多次也是哔了狗了
/*************************************************************************
> File Name: E.cpp
> Author: ALex
> Mail: zchao1995@gmail.com
> Created Time: 2015年05月20日 星期三 10时54分55秒
************************************************************************/
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <LL, int> PLL;
static const int N = 300100;
LL d[N];
bool flag[N];
bool vis[N];
int head[N];
int cost[N];
int tot;
int use[N];
vector <int> ans;
priority_queue < PLL, vector <PLL>, greater <PLL> > qu;
struct node {
int w;
int id;
int nxt;
int to;
}edge[N << 1];
void addedge(int u, int v, int w, int id) {
edge[tot].to = v;
edge[tot].w = w;
edge[tot].nxt = head[u];
edge[tot].id = id;
head[u] = tot++;
}
void Dijkstra(int s) {
d[s] = 0;
cost[s] = 0;
memset(flag, 0, sizeof(flag));
memset(vis, 0, sizeof(vis));
while (!qu.empty()) {
qu.pop();
}
qu.push(make_pair(d[s], s));
while (!qu.empty()) {
PLL tmp = qu.top();
qu.pop();
int u = tmp.second;
LL dist = tmp.first;
if (flag[u]) {
continue;
}
flag[u] = 1;
vis[use[u]] = 1;
for (int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].to;
int w = edge[i].w;
int id = edge[i].id;
if (d[v] > dist + w) {
use[v] = id;
d[v] = dist + w;
cost[v] = w;
qu.push(make_pair(d[v], v));
}
else if (d[v] == dist + w && cost[v] > w) {
cost[v] = w;
use[v] = id;
}
}
}
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
memset(head, -1, sizeof(head));
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; ++i) {
d[i] = (1LL << 60);
cost[i] = d[i];
}
tot = 0;
int u, v, w, s;
for (int i = 1; i <= m; ++i) {
scanf("%d%d%d", &u, &v, &w);
addedge(u, v, w, i);
addedge(v, u, w, i);
}
scanf("%d", &s);
Dijkstra(s);
LL sum = 0;
for (int i = 1; i <= n; ++i) {
for (int j = head[i]; ~j; j = edge[j].nxt) {
if (vis[edge[j].id]) {
sum += edge[j].w;
}
}
}
cout << sum / 2 << endl;
if (!sum) {
return 0;
}
for (int i = 1; i <= m; ++i) {
if (vis[i]) {
printf("%d ", i);
}
}
printf("\n");
return 0;
}