Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21551 Accepted Submission(s): 12895
把英语单词分别讨论处理,注意输入格式。
#include
using namespace std;
const int maxn=30;
string ch;
char c1[maxn],c2[maxn],c3[maxn],c4[maxn];
int ok(char c1[])
{
int a=0;
if(c1[0]=='o')
a=1;
else if(c1[0]=='e')
a=8;
else if(c1[0]=='n')
a=9;
else if(c1[0]=='z')
a=0;
else if(c1[0]=='t')
{
if(c1[1]=='w')
a=2;
else
a=3;
}
else if(c1[0]=='f')
{
if(c1[1]=='o')
a=4;
else
a=5;
}
else if(c1[0]=='s')
{
if(c1[1]=='i')
a=6;
else
a=7;
}
return a;
}
int main()
{
while(true)
{
getline(cin,ch);//read
int len=ch.size();
int i=0;
int j1=0,j2=0,j3=0,j4=0;
int f1=0,f11=0,f21=0;
for(int i=0; i
Huge input, scanf is recommended.
相当于一个简单的动态规划,当和s<0,更新s和左右端点为ai,当s>0时更新右端点为ai,当s>maxx时,更新左右答案。
#include
#include
using namespace std;
const int maxn = 10000 + 66;
int a[maxn];
int main()
{
int k;
while (scanf("%d",&k)&&k)
{
for (int i = 1; i <= k; i++)
{
scanf("%d", &a[i]);
}
int s = 0;
int maxx = -99999999;
int l1 = 0, l2 = 0;
int ansa = 0, ansb = 0;
for (int i = 1; i <= k; i++)
{
if (s < 0)
{
s = a[i];
l1 = a[i];
l2 = a[i];
}
else
{
s += a[i];
l2 = a[i];
}
if (s > maxx)
{
maxx = s;
ansa = l1;
ansb = l2;
}
}
if(maxx>=0)
printf("%d %d %d\n", maxx,ansa,ansb);
else
{
printf("%d %d %d\n", 0, a[1], a[k]);
}
}
//getchar();
return 0;
}
简单的并查集应用,利用并查集缩点之后,形成n个联通块,应该加上n-1条路。
#include
#include
using namespace std;
const int maxn = 10000 + 66;
int fa[maxn];
int find(int x)
{
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
void init(int x, int y)
{
int fx = find(x);
int fy = find(y);
if (fx != fy)
{
fa[fx] = fy;
}
}
int main()
{
int n,m;
while (scanf("%d",&n)&&n)
{
for (int i = 1; i <= n; i++)fa[i] = i;
scanf("%d", &m);
while (m--)
{
int u, v;
scanf("%d %d", &u, &v);
init(u, v);
}
for (int i = 1; i <= n; i++)
{
find(i);
}
int ans = 0;
for (int i = 1; i <= n; i++)
{
if (i == fa[i])ans++;
}
printf("%d\n", ans - 1);
}
}
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22975 Accepted Submission(s): 11504
考察结构体排序,注意读入,排两遍序之后输出即可。
#include
#include
#include
#include
#include
using namespace std;
const int maxn = 1000 + 66;
struct student
{
char name[20];
int h, m, s;
int h1, m1, s1;
} a[maxn];
bool cmp1(student a,student b)
{
if (a.h < b.h)
return true;
else if (a.h > b.h)
return false;
else
{
if (a.m < b.m)
return true;
else if (a.m > b.m)
return false;
else
{
if (a.s < b.s)
return true;
else if (a.s > b.s)
return false;
}
}
return true;
}
bool cmp2(student a, student b)
{
if (a.h1 < b.h1)
return true;
else if (a.h1 > b.h1)
return false;
else
{
if (a.m1 < b.m1)
return true;
else if (a.m1> b.m1)
return false;
else
{
if (a.s1 < b.s1)
return true;
else if (a.s1 > b.s1)
return false;
}
}
return true;
}
int main()
{
int t;
while (scanf("%d",&t)!=EOF)
{
while(t--)
{
int m;
scanf("%d", &m);
int m1=m;
int id = 0;
while (m1--)
{
scanf("%s", a[++id].name);
scanf("%d:%d:%d ", &a[id].h, &a[id].m, &a[id].s);
scanf("%d:%d:%d", &a[id].h1, &a[id].m1, &a[id].s1);
}
sort(a + 1, a+m + 1,cmp1);
cout << a[1].name << " ";
sort(a + 1, a + m + 1, cmp2);
cout << a[m].name << endl;
}
}
}
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 32662 Accepted Submission(s): 11974
考察结构体排序,排好序选择输出即可。
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxn = 1000 + 66;
int grade[maxn];
struct student
{
char name[maxn];
int num;
int grades;
}a[maxn];
bool cmp1(const student a, const student b)
{
if (a.grades > b.grades)return true;
else if (a.grades < b.grades)return false;
else
{
return strcmp(a.name,b.name)<0;
}
return true;
}
int main()
{
int n, m, g;
while (scanf("%d %d %d", &n, &m, &g)&&n)
{
for (int i = 1; i <= m; i++)
{
scanf("%d", &grade[i]);
}
for (int i = 1; i <= n; i++)a[i].grades=0;
int ans = 0;
for (int i = 1; i <= n; i++)
{
scanf("%s %d", a[i].name, &a[i].num);
for (int j = 1; j <= a[i].num; j++)
{
int id;
scanf("%d", &id);
a[i].grades += grade[id];
}
if (a[i].grades >= g)
{
ans++;
}
}
printf("%d\n", ans);
//cout << n << endl;
sort(a + 1, a + n + 1, cmp1);
for (int i = 1; i <= n; i++)
{
if (a[i].grades >= g)printf("%s %d\n", a[i].name, a[i].grades);
}
}
return 0;
}