代码:
#include
using namespace std;
const int MAXN = 1010;
int arr[MAXN];
int main(){
int n, a;
cin >> n;
while(n --){
cin >> a;
cout << ++ arr[a] << " ";
}
return 0;
}
代码:
#include
using namespace std;
const int MAXN = 510;
int g[MAXN][MAXN], cnt = 1;
int main(){
int n;
bool op = false;
cin >> n;
for(int i = 0; i < n; i ++)
for(int j = 0; j < n; j ++)
cin >> g[i][j];
//遍历对角线,下标和为 [0 , 2*n - 1)
for(int i = 0; i < 2 * n - 1; i ++){
int r, c;
//从右上到左下扫描,首位置在第0行第i列
if(op)
r = 0, c = i;
//从左下到右上扫描,首位置在第0列第i行
else
r = i, c = 0;
//移动i次,这样子整个对角线都遍历了,不过有些坐标是在矩阵之外的,需要跳过
for(int j = 0; j <= i; j ++){
//在矩阵内才输出
if(r >= 0 && c >= 0 && r < n && c < n)
cout << g[r][c] << " ";
//从右上到左下,扫描时行 + 1 列 - 1
if(op)
r += 1, c -= 1;
//从左下到右上,扫描时行 - 1 列 + 1
else
r -= 1, c += 1;
}
//蛇形扫描
op = !op;
}
return 0;
}
代码:
#include
#include
#include
using namespace std;
//p为价格 op为1是buy 为0是sell num为数量
struct node{
double p;
int op, num;
//首先先按照价格从小到大排序,如果相等再按照先sell再bug排序(为了最优解,先考虑sell)
bool operator < (const node& t) const {
if(abs(p - t.p) > 1e-6)
return p < t.p;
else
return op < t.op;
}
};
const int MAXN = 5010;
node arr[MAXN], g[MAXN];
int main(){
int a, n = 1, j = 1;
long long ans = 0, sum1 = 0, sum2 = 0;
double t = 0;
string s;
while(cin >> s){
if(s == "buy")
cin >> arr[n].p >> arr[n].num, arr[n].op = 1;
else if(s == "sell")
cin >> arr[n].p >> arr[n].num, arr[n].op = 0;
else
cin >> a, arr[a].num = 0, arr[n].num = 0;
n ++;
}
for(int i = 1; i < n; i ++){
//去除那些要删除的数据
if(arr[i].num == 0)
continue;
g[j ++] = arr[i];
if(arr[i].op)
//首先统计当价格为无穷小时,高于价格的商品数量
sum1 += arr[i].num;
}
//按照价格从小到大排序
sort(g + 1, g + j);
for(int i = 1; i < j; i ++){
//当前价格是g[i].p,低于等于g[i].p数量要增加
if(!g[i].op){
sum2 += g[i].num;
}
long long s = min(sum1, sum2);
if(s >= ans){
ans = s;
t = g[i].p;
}
//和上面一个if分开的原因是为了开盘价正好取到g[i].p的时候,题目说明开盘价可以和低价 高价相等
//当前价格是g[i].p,在之后价格会变大,所以高于等于g[i].p数量会减少
if(g[i].op)
sum1 -= g[i].num;
}
printf("%.2lf %lld", t, ans);
return 0;
}
代码:
#include
#include
using namespace std;
const int MAXN = 100010;
struct node{
int a, b, c;
bool operator < (const node &t) const {
return c < t.c;
}
};
node arr[MAXN];
int pre[MAXN];
int find(int a){
return a == pre[a] ? a : pre[a] = find(pre[a]);
}
int main(){
int n, m, cnt = 0, pos = 0, ans = 0;
cin >> n >> m;
for(int i = 1; i <= n; i ++)
pre[i] = i;
for(int i = 0; i < m; i ++){
cin >> arr[i].a >> arr[i].b >> arr[i].c;
}
sort(arr, arr + m);
while(cnt < n - 1){
int a = find(arr[pos].a);
int b = find(arr[pos].b);
if(a != b){
pre[a] = b;
ans += arr[pos].c;
++ cnt;
}
++ pos;
}
cout << ans << endl;
return 0;
}
代码:
#include
#include
#include
using namespace std;
const int N = 710, M = (N * 3 + 500 * 7 * 2) * 2 + 10, INF = 0x3f3f3f3f;
int n, m, S, T;
int h[N], e[M], f[M], w[M], ne[M], idx;
int q[N], d[N], pre[N], incf[N];
bool st[N];
int get(int a, int b)
{
if (b == 8) b = 1;
return (a - 1) * 7 + b;
}
void add(int a, int b, int c, int d)
{
e[idx] = b, f[idx] = c, w[idx] = d, ne[idx] = h[a], h[a] = idx ++ ;
e[idx] = a, f[idx] = 0, w[idx] = -d, ne[idx] = h[b], h[b] = idx ++ ;
}
bool spfa()
{
int hh = 0, tt = 1;
memset(d, 0x3f, sizeof d);
memset(incf, 0, sizeof incf);
q[0] = S, d[S] = 0, incf[S] = INF;
while (hh != tt)
{
int t = q[hh ++ ];
if (hh == N) hh = 0;
st[t] = false;
for (int i = h[t]; ~i; i = ne[i])
{
int ver = e[i];
if (f[i] && d[ver] > d[t] + w[i])
{
d[ver] = d[t] + w[i];
pre[ver] = i;
incf[ver] = min(incf[t], f[i]);
if (!st[ver])
{
q[tt ++ ] = ver;
if (tt == N) tt = 0;
st[ver] = true;
}
}
}
}
return incf[T] > 0;
}
void EK(int& flow, int& cost)
{
flow = cost = 0;
while (spfa())
{
int t = incf[T];
flow += t, cost += t * d[T];
for (int i = T; i != S; i = e[pre[i] ^ 1])
{
f[pre[i]] -= t;
f[pre[i] ^ 1] += t;
}
}
}
int main()
{
cin >> n >> m;
S = 0, T = n * 7 + 1;
memset(h, -1, sizeof h);
for (int i = 1; i <= n; i ++ )
{
int c, d;
for (int j = 1; j <= 7; j ++ )
{
cin >> c;
add(S, get(i, j), c, 0);
}
for (int j = 1; j <= 7; j ++ )
{
cin >> c;
add(get(i, j), T, c, 0);
}
cin >> c >> d;
for (int j = 1; j <= 7; j ++ )
add(get(i, j), get(i, j + 1), c, d);
}
while (m -- )
{
int a, b, c;
cin >> a >> b >> c;
for (int i = 1; i <= 7; i ++ )
{
add(get(a, i), get(b, i), INF, c);
add(get(b, i), get(a, i), INF, c);
}
}
int flow, cost;
EK(flow, cost);
cout << cost << endl;
return 0;
}